tad de una lista circular

robin
11 de Mayo del 2004
porfa favor nesecito hacer un tad de una lista circular que contenga: mostrar cola, ingresar elemento, eliminar elemento, mostrar punteros y salir lo nrsecito urgente porfa. Gracias

noel solw
11 de Mayo del 2004
Te envio, como ejemplo, un programa que une dos listas circulares en una sola lista.
Espero que te sea util.

-------------------------------------------------------------------------

// program k18b1.cpp - page 375
// Circular Linked List : joint.
// c++ exercices book - dr. gershon kagan - first edition : 2001
// written in Borland CPP ver 3.1

#include "CircList.h"
#include <string.h>

void JointProcess(CircularList<char> &a,CircularList<char> &b)
{
Node<char> *pa = a.GetBegin(),*firsta = pa,
*pb = b.GetBegin(),*firstb = pb;
while(pa->next != firsta)
pa = pa->next;
while(pb->next != firstb)
pb = pb->next;
pa->next = firstb;
pb->next = firsta;
} // JOINT PROCESS

void Process()
{
char *data1 = "abcdefgh",
*data2 = "ABCD";
CircularList <char> a(strlen(data1),data1),
b(strlen(data2),data2);
cout << setw(30) << "first giving list : " << a;
cout << setw(30) << "second giving list : " << b;
JointProcess(a,b);
cout << setw(30) << "jointed list : " << a;
JointProcess(a,b); // backs to source state, to avoid destructor fail
} // PROCESS

void main()
{
clrscr();
cout << "Circular Linked List : joint.n";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN

-------------------------------------------------------------------------

// program CircList.h - page 370
// Header File for Circular 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 CircularList
{
private:
Node<T> *begin;
public:
CircularList(int num = 0,T *data = NULL);
CircularList(CircularList &right);
~CircularList();
Node<T> *GetBegin() {return begin;} // GET BEGIN
void SetBegin(Node<T> *BEGIN) {begin = BEGIN;} // SET BEGIN
void Delete(Node<T> *anterior);
friend ostream &operator<<(ostream &,const CircularList &);
}; // CLASS CIRCULAR LIST

template <class T>
CircularList<T>::CircularList(int num = 0,T *data = NULL)
{
if(!data)
{
begin = NULL;
}
else
{
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];
}
p->next = begin;
}
} // CIRCULAR LIST CONSTRUCTOR

template <class T>
CircularList<T>::CircularList(CircularList<T> &right)
{
Node<T> *p = right.GetBegin(),*q;
if(!p)
{
begin = NULL;
}
else
{
begin = new Node<T>;
q = 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 = begin;
}
} // CIRCULAR LIST COPY CONSTRUCTOR

template <class T>
CircularList<T>::~CircularList()
{
Node<T> *p = begin,*q;
for(;;)
{
q = p;
p = p->next;
delete q;
if(p == begin)
break;
}
} // CIRCULAR LIST DESTRUCTOR

template <class T>
void CircularList<T>::Delete(Node<T> *anterior)
{
Node<T> *actual = anterior->next;
cout << setw(30) << "delete . . . . " << actual->info;
anterior->next = actual->next;
if(actual == begin)
begin = actual->next;
delete actual;
} // CIRCULAR LIST DELETE

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