Clase Lista simple para C++

Tal como publiqué hace algunos días la Clase String Simple para C++ como un ejercicio de aprendizaje de C++, ahora estoy liberando una Clase Lista simple para C++.

La librería standard tiene una clase list. Pero, mientras se está aprendiendo, es una buena idea saber cómo poder desarrollar tu propia clase list.

Esa es la razón por la que hice la clase List. No está pensada para proyectos profesionales (para ellos, deberías utilizar la list de la librería standard), sino como una ayuda para aprender 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);

Este es un proyecto de ejemplo hecho para explicar la utilización de la clase List.

#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;
}

Y esta su salida:

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

Este proyecto también utiliza la Clase String simple para C++ para mostrar a la clase List trabajando con clases personales. El mismo código podría utilizarse perfectamente reemplazando “String” por “string” e incluyendo los headers de la versión standard de string.

El código es completamente portable.

Ha sido desarrollado, compilado y testeado utilizando wxDev-C++ para Windows con el compilador MinGW (incluído en el paquete). En Linux, fue compilado utilizando el compilador GNU GCC.

GNU GPL v3 String está liberado bajo la licencia GNU GPL v3 (attached)…



También probé el proyecto para verificar la ausencia de fugas de memoria utilizando el cppMemDbg – Librería fácil de usar de detección de fugas de memoria para C++ y no encontró ningún tipo de problema…

Podés descargar la salida de la librería y el proyecto adaptado para cppMemDbg aquí:



Ahora, finalmente, los links de descarga:

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:



3 Responses to “Clase Lista simple para C++”


Leave a Reply