listas utilizando clases

truan
30 de Abril del 2005
porfa si alguien tiene un ejemplo de listas en c++ utilizando clases seria tan amable de pasarmelo.
gracias

algien
30 de Abril del 2005
//---------------------------------------------------------------------------
// Nodo de la Lista Enlazada
//---------------------------------------------------------------------------
class TNode
{
private:
void* aInfo;
TNode* aNext;
protected:
public:
TNode( void* pInfo) : aInfo(pInfo), aNext(NULL){}
~TNode(){}
void* Info() const { return aInfo; }
void Info(void* pInfo) { aInfo = pInfo; }
TNode* Next() const { return aNext; }
void Next(TNode* pNext) { aNext = pNext; }
};
//---------------------------------------------------------------------------
// Lista Enlazada
//---------------------------------------------------------------------------
class TLinkedList
{
private:
protected:
TNode* aFirst;
public:
TLinkedList() : aFirst(NULL){}
~TLinkedList();
void Add(void*);
bool Empty() const { return !aFirst; }
virtual int Length()const;
};
//---------------------------------------------------------------------------
// Lista General Enlazada
//---------------------------------------------------------------------------
class TGLinkedList : public TLinkedList
{
private:
protected:
public:
TGLinkedList() : TLinkedList(){}
TGLinkedList(TGLinkedList*);
void* Delete(int);
void Insert(void*, int);
void* Item(int) const;
};

//----------------------------------------------------------------------------
//implementacion
//----------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Lista Enlazada
//---------------------------------------------------------------------------
TLinkedList::~TLinkedList()
{
TNode* cursor;
while(aFirst)
{
cursor = aFirst;
aFirst = aFirst->Next();
delete cursor;
}
}

void TLinkedList::Add(void* pItem)
{
TNode* newNode = new TNode(pItem);
if (Empty())
aFirst = newNode;
else
{
TNode* cursor = aFirst;
while (cursor->Next())
cursor = cursor->Next();
cursor->Next(newNode);
}
}

int TLinkedList::Length() const
{
int length = 0;
TNode* cursor = aFirst;
while (cursor)
{
length++;
cursor = cursor->Next();
}
return length;
}
//--------------------------------------------------------------
// Lista General Enlazada
//--------------------------------------------------------------
TGLinkedList::TGLinkedList(TGLinkedList* pList)
{
int length = pList->Length();
aFirst = new TNode(pList->Item(0)); //Obtener al primer elemento
TNode * cursor = aFirst;
for(int i=1; i < length; i++)
{
TNode* newNode = new TNode(pList->Item(i)); //Obtener al próximo elemento
cursor->Next(newNode);
cursor = newNode;
}
}

void* TGLinkedList::Delete(int pos)
{
if ((pos < 0) || (pos >= Length())) throw EListOutOfRange(); //Posición fuera de rango
TNode* cursor = aFirst;
if (pos != 0)
{
for (int i = 0; i < pos - 1; i++)
cursor = cursor->Next();
// obtener una referencia al nodo
TNode* deletedNode = cursor->Next();
// sacar el nodo de la lista
cursor->Next(deletedNode->Next());
cursor = deletedNode;
}
else
// Eliminar el primer nodo
aFirst = aFirst->Next();
void* deleted = cursor->Info();
// destruir el nodo
delete cursor;
return deleted;
}

void TGLinkedList::Insert(void* pItem, int pos)
{
if ((pos < 0) || (pos >= Length())) throw EListOutOfRange(); //Posición fuera de rango
TNode* newNode = new TNode(pItem);
if (pos != 0)
{
TNode* cursor = aFirst;
int count = 1;
while (count < pos)
{
// Posicionarse en el nodo anterior
cursor = cursor->Next();
count++;
}
newNode->Next(cursor->Next());
cursor->Next(newNode);
}
else
{
// Insertar en la primera posición
newNode->Next(aFirst);
aFirst = newNode;
}
}

void* TGLinkedList::Item(int pos) const
{
if ((pos < 0) || (pos >= Length())) throw EListOutOfRange(); //Posición fuera de rango
TNode* cursor = aFirst;
for (int i = 0; i < pos; i++)
cursor = cursor->Next();
return cursor->Info();
}