listas

criss
16 de Mayo del 2004
he buscado el cosigo de listas generalizadas con nodo deencabezamiento y no lo he encontrado, me podrian dar un ejemplo de como recorrer este tipo de listas

noel solw
16 de Mayo del 2004
Te envio el codigo para listas genericas con nodo de encabezamiento.
Primero va el codigo en c++ y depues el header file.
Puedes preguntar por cualquier duda que se te presente.

##################################################
// program k18a14a.cpp - page 372
// Linked List with Header Node :
// a list contains the letters of a giving string, but one of the letters
// isn't in the right place. Rearrange the list to correct this missmatch.
// written in Borland CPP ver 3.1

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

void Process(char *data,char *string,char *msg)
{
cout << "processing mistmach " << msg << " : " << endl;
HeaderList <char> a(strlen(data),data);
cout << setw(30) << "giving string : " << string << endl;
cout << setw(30) << "giving list : " << a;
Node<char> *p = a.GetBegin(),
*first = NULL, // points one node before target place
*second = NULL, // points one node befores source place
*temp;
int index = 0;
for(;p->next && !second;)
{
if(p->next->info != string[index++])
{
if(!first)
first = p;
else
second = p;
}
else
p = p->next;
}
temp = second->next;
second->next = second->next->next;
temp->next = first->next;
first->next = NULL; // doble assignament ! ! !
first->next = temp; // i don't know why ? ? ?
cout << setw(30) << "arranged list : " << a;
cout << "-----------------------------------------------------"
"-------------------------n";
} // PROCESS

void main()
{
clrscr();
cout << "Linked List with Header Node : " << endl;
cout << "a list contains the letters of a giving string, but one of the "
<< "lettersnisn't in the right place. "
<< "Rearrange the list to correct this missmatch." << endl;
cout << "-----------------------------------------------------"
"-------------------------n";
char *data1 = "abrcadaabra",
*data2 = "bracadaabra",
*data3 = "abracadabar",
*string = "abracadabra";
Process(data1,string,"in the middle of list");
Process(data2,string,"at the begin of list");
Process(data3,string,"at the end of list");
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN

/*
Linked List with Header Node :
a list contains the letters of a giving string, but one of the letters
isn't in the right place. Rearrange the list to correct this missmatch.
------------------------------------------------------------------------------
processing mistmach in the middle of list :
giving string : abracadabra
giving list : {a,b,r,c,a,d,a,a,b,r,a}
arranged list : {a,b,r,a,c,a,d,a,b,r,a}
------------------------------------------------------------------------------
processing mistmach at the begin of list :
giving string : abracadabra
giving list : {b,r,a,c,a,d,a,a,b,r,a}
arranged list : {a,b,r,a,c,a,d,a,b,r,a}
------------------------------------------------------------------------------
processing mistmach at the end of list :
giving string : abracadabra
giving list : {a,b,r,a,c,a,d,a,b,a,r}
arranged list : {a,b,r,a,c,a,d,a,b,r,a}
------------------------------------------------------------------------------
end of program - good bye ! ! !
*/

################################################

// program HeadList.h - page 370.
// Header File for Linked List with header first node.
// 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;
Node() {next = NULL;} // NODE CONSTRUCTOR
}; // STRUCT NODE

template <class T>
class HeaderList
{
private:
Node<T> *begin;
public:
HeaderList(int num = 0,T *data = NULL);
~HeaderList();
Node<T> *GetBegin() {return begin;} // GET BEGIN
friend ostream &operator<<(ostream &,const HeaderList &);
}; // CLASS HEADER LIST

template <class T>
HeaderList<T>::HeaderList(int num = 0,T *data = NULL)
{
begin = new Node<T>;
{
Node<T> *p = begin;
for(int i = 0;i < num;i++)
{
p->next = new Node<T>;
p = p->next;
p->info = data[i];
}
}
} // HEADER LIST CONSTRUCTOR

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

template <class T>
ostream &operator<<(ostream &out,const HeaderList <T> &LIST)
{
Node<T> *p = LIST.begin->next;
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 <<

###############################################