Simple C++ List Class

Just as I published some days ago the Simple C++ String Class as a C++ learning exercise, now I am freeing a Simple C++ List Class.

The standard library has a list class. But, while learning, it’s a good idea to know how to develop your own list class.

That’s why I made the List class. It’s not intended for professional projects (for them, you should use the standard library’s list), but as help to learn C++.

template <class TYPE>
class List
{
	/* ... */

public:
	//Construction and destruction
	List() { /* ... */ }
	~List() { /* ... */ }
	
	List(const List& rlList) { /* ... */ }
	
	//Assignment operator
	List& operator=(const List& rlList);

	//Information
	int Length() { /* ... */ }
	bool Empty() { /* ... */ }

	//Element managing
	int Add(TYPE& rtData);
	TYPE* Elem(int nPos);
	bool Delete(int nPos);
	void DeleteAll();
	
	//Search
	int Find(TYPE& rItem, int nStartAt = 0);

	//Operadores
	TYPE& operator[](int nPos) { /* ... */ }	//Elem
	int operator<<(TYPE& rdData) { /* ... */ }	//Add

protected:
	void FreeList();
	void Init() { /* ... */ }
};

//Output
template <class TYPE>
std::ostream& operator<<(std::ostream& oStream, List<TYPE>& rlList);

This is a sample project made to explain the List class usage.

#include <iostream>
	using std::cout;
	using std::cin;
	using std::endl;

#include "List.h"
#include "String.h"

int main(int argc, char *argv[])
{
	cout << "List sample project" << endl;
	cout << "-------------------" << endl << endl;


	//-----------------------------
	cout << "> Creating a list of strings... An empty string finishes the list..." << endl << endl;

	List<String> lStrings;
	String sTmp;
	
	do
	{
		cout << "> String " << lStrings.Length()+1 << ": ";
		cin >> sTmp;
		
		if (sTmp.Length())
			lStrings.Add(sTmp);
	} while (sTmp.Length());
	
	cout << endl << "> Entry finished. " << lStrings.Length() << " string(s) loaded." << endl << endl;
	
	cout << "> Printing list..." << endl << endl;
	
	cout << lStrings;

	cout << endl;
	//-----------------------------
	
	
	//-----------------------------
	do
	{
		cout << "> Type a string to find in the list (exact match) [empty = end]: ";
		cin >> sTmp;

		if (sTmp.Length())
		{
			int nFound = lStrings.Find(sTmp);
			
			if (nFound != -1)
			{
				lStrings.Delete(nFound);

				cout << "> String \"" << sTmp << "\" found at position " << nFound+1 << " and removed." << endl << endl;
				
				cout << "> Printing list..." << endl << endl;

				if (!lStrings.Empty())
					cout << lStrings;
				else
				{
					cout << "[Empty list]" << endl << endl;
					break;
				}
			}
			else
				cout << "> String \"" << sTmp << "\" not found." << endl;
		}
		
		cout << endl;
	} while (sTmp.Length());
	
	cout << endl;
	//-----------------------------

	//-----------------------------
	cout << "> Now getting a bit more complex... Let's create a list of lists of strings... An empty string finishes the list and an empty first string finishes the list of lists..." << endl << endl;

	List< List<String> > lLists;
	List<String> lTmpList;
	int nStrings = 0;

	do
	{
		lTmpList.DeleteAll();
		
		cout << "> Loading list " << lLists.Length()+1 << "..." << endl;
		
		do
		{
			cout << "\t> String " << lTmpList.Length()+1 << ": ";
			cin >> sTmp;
			
			if (sTmp.Length())
			{
				lTmpList.Add(sTmp);
				nStrings++;
			}
		} while (sTmp.Length());
		
		if (lTmpList.Length())
			lLists.Add(lTmpList);
	} while (lTmpList.Length());
	
	cout << endl << "> Entry finished. " << lLists.Length() << " list(s) loaded, " << nStrings << " string(s) loaded." << endl << endl << endl;

	cout << "> Printing lists (standard method)..." << endl << endl;

	cout << lLists;

	cout << endl;
	
	cout << "> Printing lists (custom method)..." << endl << endl;
	
	for (int i = 0; i < lLists.Length(); i++)
	{
		List<String> *plList = lLists.Elem(i);

		if (plList)
		{
			cout << "- List " << i << endl << endl;
			cout << *plList;
			cout << endl;
		}
	}
	//-----------------------------

    return 0;
}

And this is its output:

List sample project
-------------------

> Creating a list of strings... An empty string finishes the list...

> String 1: Test 1
> String 2: Test 2
> String 3: Test 3, a little bit longer
> String 4:

> Entry finished. 3 string(s) loaded.

> Printing list...

Test 1
Test 2
Test 3, a little bit longer

> Type a string to find in the list (exact match) [empty = end]: Hello
> String "Hello" not found.

> Type a string to find in the list (exact match) [empty = end]: Test 1
> String "Test 1" found at position 1 and removed.

> Printing list...

Test 2
Test 3, a little bit longer

> Type a string to find in the list (exact match) [empty = end]: Test 1
> String "Test 1" not found.

> Type a string to find in the list (exact match) [empty = end]: test 2
> String "test 2" not found.

> Type a string to find in the list (exact match) [empty = end]: Test 2
> String "Test 2" found at position 1 and removed.

> Printing list...

Test 3, a little bit longer

> Type a string to find in the list (exact match) [empty = end]: Test 3, a little bit longer
> String "Test 3, a little bit longer" found at position 1 and removed.

> Printing list...

[Empty list]


> Now getting a bit more complex... Let's create a list of lists of strings... An empty string finishes the list and an empty first string finishes the list of lists...

> Loading list 1...
        > String 1: Test 1a
        > String 2: Test 1b
        > String 3: Test 1c
        > String 4:
> Loading list 2...
        > String 1: Test 2a
        > String 2: Test 2b
        > String 3: Test 2c
        > String 4: Test 2d
        > String 5:
> Loading list 3...
        > String 1: Test 3a
        > String 2: Test 3b
        > String 3: This is a looooooooooooooooooooooong string
        > String 4:
> Loading list 4...
        > String 1:

> Entry finished. 3 list(s) loaded, 10 string(s) loaded.


> Printing lists (standard method)...

Test 1a
Test 1b
Test 1c

Test 2a
Test 2b
Test 2c
Test 2d

Test 3a
Test 3b
This is a looooooooooooooooooooooong string


> Printing lists (custom method)...

- List 0

Test 1a
Test 1b
Test 1c

- List 1

Test 2a
Test 2b
Test 2c
Test 2d

- List 2

Test 3a
Test 3b
This is a looooooooooooooooooooooong string

This project also uses the Simple C++ String Class to show the List class working with custom classes. Exactly the same code could be without problem by simply replacing “String” with “string” and including the standard library’s string header.

The code completely is portable.

It’s been developed, compiled and tested using wxDev-C++ for Windows with the MinGW compiler (included in the bundle).

GNU GPL v3 List is licensed under the GNU GPL v3 (attached)…



I’ve also tested the project for memory leaks using the cppMemDbg – Easy to use C++ memory leak detection library and it found no problems at all…

You can download the library output and the cppMemDbg adapted project here:



Now, finally, the download links:

Support appreciated!

All the content offered in this website is, except noted otherwise, of free nature. This means you can share it wherever you want if you do it freely and stating its source.

If it was useful for you and you’d like to contribute, you can make a donation or, at least, visit one of our advertisers of your choice; they are all around the site.

Incoming search terms for the article:



0 Response to “Simple C++ List Class”


Comments are currently closed.