dificil lo se haber quien me ayuda
nesecito ayuda urgente quien me puede decir como dar vuelta una lista enlazada de tal manera que el ultimo valor quede en el primero yo se que es difcil pero creo que se la podran
Este es el programa de la lista enlazada.
// program k18a8.cpp - page 368
// Linked List : reverse list.
// c++ exercices book - dr. gershon kagan - first edition : 2001
//written in Borland CPP ver 3.1
#include "LinkList.h"
#include <string.h>
void Reverse(LinkedList<char> &a)
{
Node<char> *current,*previous,*holder;
current = a.GetBegin();
previous = NULL;
while(current)
{
holder = current->next;
current->next = previous; // without this line, the program
current->next = previous; // don't works ! why ?
previous = current;
current = holder;
}
a.SetBegin(previous);
} // REVERSE
void Process()
{
char *A = "abcdefgh";
LinkedList <char> a(strlen(A),A);
cout << setw(30) << "given list : " << a;
Reverse(a);
cout << setw(30) << "reverse list : " << a;
} // PROCESS
void main()
{
clrscr();
cout << "Linked List : reverse listn";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
/*
Linked List : reverse list
------------------------------------------------------------------------------
given list : {a,b,c,d,e,f,g,h}
reverse list : {h,g,f,e,d,c,b,a}
------------------------------------------------------------------------------
end of program - good bye ! ! !
*//
// 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<T> &);
}; // 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 <<
// program k18a8.cpp - page 368
// Linked List : reverse list.
// c++ exercices book - dr. gershon kagan - first edition : 2001
//written in Borland CPP ver 3.1
#include "LinkList.h"
#include <string.h>
void Reverse(LinkedList<char> &a)
{
Node<char> *current,*previous,*holder;
current = a.GetBegin();
previous = NULL;
while(current)
{
holder = current->next;
current->next = previous; // without this line, the program
current->next = previous; // don't works ! why ?
previous = current;
current = holder;
}
a.SetBegin(previous);
} // REVERSE
void Process()
{
char *A = "abcdefgh";
LinkedList <char> a(strlen(A),A);
cout << setw(30) << "given list : " << a;
Reverse(a);
cout << setw(30) << "reverse list : " << a;
} // PROCESS
void main()
{
clrscr();
cout << "Linked List : reverse listn";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
/*
Linked List : reverse list
------------------------------------------------------------------------------
given list : {a,b,c,d,e,f,g,h}
reverse list : {h,g,f,e,d,c,b,a}
------------------------------------------------------------------------------
end of program - good bye ! ! !
*//
// 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<T> &);
}; // 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 <<
