pilas

gissela
15 de Abril del 2005
nesecito hacer un programa con pilas, que ingrese una palabra y esta a su vez imprima alrevez ejemplo:
hola (aloh) ayudita

Noel Solw
15 de Abril del 2005
[a] : Header File con las funciones necesarias para manejar un stack. Contiene mas de lo imprescindible para tu programa.

// program Stack.h - page 393 . . . . .
// Header File for Stack class.
// 22/9/2002
// 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 Stack
{
private:
Node<T> *top;
unsigned int len;
public:
Stack(int num = 0,T *data = NULL);
Stack(Stack<T> &right);
~Stack();
void Push(T);
T Pop();
int EmptyStack();
T StackTop();
unsigned int StackLen();
Node<T> *GetTopPointer() {return top;}
void operator=(Stack<T> &);
friend ostream &operator<<(ostream &,const Stack &);
}; // CLASS STACK
// -------------------------------------------------------------------------
template <class T>
Stack<T>::Stack(int num = 0,T *data = NULL)
{
top = NULL;
len = 0;
for(int i = num-1;i >= 0;i--)
Push(data[i]);
} // STACK CONSTRUCTOR

template <class T>
Stack<T>::Stack(Stack<T> &right)
{
top = NULL;
len = 0;
Transfer(*this,right.GetTopPointer());
} // COPY CONSTRUCTOR

template <class T>
Stack<T>::~Stack()
{
while(!this->EmptyStack())
Pop();
} // STACK DESTRUCTOR

template <class T>
void Stack<T>::Push(T INFO)
{
Node<T> *holder = top;
top = new Node<T>;
top->info = INFO;
top->next = holder;
len++;
} // STACK PUSH

template <class T>
T Stack<T>::Pop()
{
if(EmptyStack())
{
cout << "error : empty stack pop" << endl;
return 0;
}
T ret = top->info;
Node<T> *holder = top;
top = top->next;
delete(holder);
len--;
return ret;
} // STACK POP

template <class T>
int Stack<T>::EmptyStack()
{
return top == NULL;
} // EMPTY STACK

template <class T>
T Stack<T>::StackTop()
{
return top->info;
} // STACK TOP

template <class T>
unsigned int Stack<T>::StackLen()
{
return len;
} // STACK LEN

template <class T>
void Transfer(Stack<T> &a,Node<T> *p)
{
if(p->next)
Transfer(a,p->next);
a.Push(p->info);
} // TRANSFER

template <class T>
void Stack<T>::operator=(Stack<T> &right)
{
while(!this->EmptyStack())
Pop();
top = NULL;
len = 0;
Transfer(*this,right.GetTopPointer());
} // STACK OPERATOR =

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


***********************************************************************


[b] : el programa que realiza la inversion de una cadena


// program invertir.cpp
// invertir una palabra usando stack.
// 15/4/2005
// written in Borland CPP ver 3.1

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

void Reverse(char *data)
{
Stack<char> a;
int len = strlen(data);
for(int i = 0;i < len;i++)
a.Push(data[i]);
for(i = 0;i < len;i++)
data[i] = a.Pop();
} // REVERSE

void Process()
{
char *data = "hola amigos";
Stack<char> a(strlen(data),data);
cout << setw(30) << "giving data : " << data << endl;
Reverse(data);
cout << setw(30) << "reverse string : " << data << endl;
} // PROCESS

void main()
{
clrscr();
cout << "invertir una palabra usando stack..n";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN