Lista Generica

gazpatxo
29 de Abril del 2004
wenas necesito el codigo en C++ de una lista generica con todos sus metodos principales, en ella voy a guardar una clase base normal y corriente.
Grazias!!

noel solw
29 de Abril del 2004
Te envio una lista generica.
Espero que sepas ligar a un programa principar.
Avisame si tienes problemas.


// program LinkdList.h - page 366 . . . . .
// Header File for Linked List.
// c++ exercices book - dr. gershon kagan - first edition : 2001
// written in Borland CPP ver 3.1

#include <conio.h>
#include <iostream.h>
#include <iomanip.h>

template <class T>
struct Node
{
T info;
Node<T> *next;
}; // STRUCT NODE

template <class T>
class LinkedList
{
private:
Node<T> *begin;
int len;
public:
LinkedList(int num = 0,T *data = NULL);
LinkedList(LinkedList &right);
~LinkedList();
int GetLen() {return len;}; // GET LEN
Node<T> *GetBegin() {return begin;} // GET BEGIN
void SetBegin(Node<T> *BEGIN) {begin = BEGIN;} // SET BEGIN
T operator [] (int index) const;
void FirstDelete(Node<T> *actual);
void InnerDelete(Node<T> *anterior);
void Remove(Node<T> *p);
void FirstInsert(Node<T> *pMin);
void Insert(Node<T> *p,Node<T> *pMin);
friend ostream &operator<<(ostream &,const LinkedList &);
}; // CLASS LINKED LIST

template <class T>
LinkedList<T>::LinkedList(int num = 0,T *data = NULL)
{
if(!data)
{
begin = NULL;
len = 0;
}
else
{
len = 1;
begin = new Node<T>;
Node<T> *p = begin;
p->info = data[0];
for(int i = 1;i < num;i++)
{
p->next = new Node<T>;
p = p->next;
p->info = data[i];
len++;
}
p->next = NULL;
}
} // LINKED LIST CONSTRUCTOR

template <class T>
LinkedList<T>::LinkedList(LinkedList<T> &right)
{
Node<T> *p = right.GetBegin(),*q;
if(!p)
{
begin = NULL;
}
else
{
this->begin = new Node<T>;
q = this->begin;
q->info = p->info;
p = p->next;
while(p)
{
q->next = new Node<T>;
q = q->next;
q->info = p->info;
p = p->next;
}
q->next = NULL;
len = right.len;
}
} // LINKED LIST COPY CONSTRUCTOR

template <class T>
LinkedList<T>::~LinkedList()
{
Node<T> *p = begin,*q;
while(p)
{
q = p;
p = p->next;
delete q;
}
} // LINKED LIST DESTRUCTOR

template <class T>
T LinkedList<T>::operator [] (int index) const
{
if(index < 0 || index >= len)
{
cout << index << " out of range index ";
return 0;
}
Node<T> *p = begin;
while(index)
{
index--;
p = p->next;
}
return p->info;
} // LINKED LIST OPERATOT []

template <class T>
void LinkedList<T>::FirstDelete(Node<T> *actual) // remode and delete node
{ // at the begining of list
cout << setw(30) << "first delete . . . . " << actual->info << endl;
begin = actual->next;
delete actual;
len--;
} // LINKED LIST FIRST DELETE

template <class T>
void LinkedList<T>::InnerDelete(Node<T> *anterior) // remode and delete nodr
{ // inside the list
Node<T> *actual = anterior->next;
cout << setw(30) << "delete . . . . " << actual->info << endl;
anterior->next = actual->next;
anterior->next = actual->next; // ***************
delete actual;
len--;
} // LINKED LIST INNER DELETE

template <class T>
void LinkedList<T>::Remove(Node<T> *p) // remove node inside the list
{ // without destruyed it
p->next = p->next->next;
} // REMOVE

template <class T>
void LinkedList<T>::FirstInsert(Node<T> *pMin) // insert existent node
{ // at the begining of list
pMin->next = begin;
begin = pMin;
} // FIRST INSERT

template <class T>
void LinkedList<T>::Insert(Node<T> *p,Node<T> *pMin) // insert existent node
{ // inside the list
pMin->next = p->next;
pMin->next = p->next;
p->next = pMin;
p->next = pMin;
} // INSERT

template <class T>
ostream &operator<<(ostream &out,const LinkedList <T> &LIST)
{
Node<T> *p = LIST.begin;
if(!p)
out << "{empty list}";
else
{
out << "{" << p->info;
p = p->next;
for(;p;p = p->next)
out << "," << p->info;
}
out << "}" << endl;
return out;
} // OPERATOR <<