Control de Palindromes con Pilas en C++
Esto es lo que necesito
 un programa que reciba por teclado una palabra y, utilizando una pila implementada con una lista enlazada lineal, verifique si es palíndromo o no.
El programa va con esta base
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
struct nodopila
{
char caracter;
struct nodopila *sig;
};
void Push(struct nodopila **pila, char caracter);
char Pop(struct nodopila **pila);
void main()
{
struct nodopila *pila;
clrscr();
pila=NULL;
Push(&pila, 'h');
Push(&pila, 'o');
Push(&pila, 'l');
Push(&pila, 'a');
printf("%c", Pop(&pila));
printf("%c", Pop(&pila));
printf("%c", Pop(&pila));
printf("%c", Pop(&pila));
getch();
}
void Push(struct nodopila **pila, char caracter)
{
struct nodopila *nuevo;
nuevo=(struct nodopila*) malloc(sizeof(struct nodopila));
if(nuevo == NULL)
return;
nuevo->caracter = caracter;
nuevo->sig = *pila;
*pila = nuevo;
}
char Pop(struct nodopila **pila)
{
if(*pila==NULL)
return 0;
struct nodopila *auxiliar;
auxiliar=*pila;
char c=0;
c=auxiliar->caracter;
*pila=auxiliar->sig;
free(auxiliar);
return c;
}
Lo necedito lo antes posible, desde ya muchas gracias
 un programa que reciba por teclado una palabra y, utilizando una pila implementada con una lista enlazada lineal, verifique si es palíndromo o no.
El programa va con esta base
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
struct nodopila
{
char caracter;
struct nodopila *sig;
};
void Push(struct nodopila **pila, char caracter);
char Pop(struct nodopila **pila);
void main()
{
struct nodopila *pila;
clrscr();
pila=NULL;
Push(&pila, 'h');
Push(&pila, 'o');
Push(&pila, 'l');
Push(&pila, 'a');
printf("%c", Pop(&pila));
printf("%c", Pop(&pila));
printf("%c", Pop(&pila));
printf("%c", Pop(&pila));
getch();
}
void Push(struct nodopila **pila, char caracter)
{
struct nodopila *nuevo;
nuevo=(struct nodopila*) malloc(sizeof(struct nodopila));
if(nuevo == NULL)
return;
nuevo->caracter = caracter;
nuevo->sig = *pila;
*pila = nuevo;
}
char Pop(struct nodopila **pila)
{
if(*pila==NULL)
return 0;
struct nodopila *auxiliar;
auxiliar=*pila;
char c=0;
c=auxiliar->caracter;
*pila=auxiliar->sig;
free(auxiliar);
return c;
}
Lo necedito lo antes posible, desde ya muchas gracias
Te envio un programa que revisa si una pila es palindromica.
El programa trabaja con un header file, stack.h, que define una pila generica.
// program k19a6.cpp - page 396
// check palindromic stack.
// c++ exercices book - dr. gershon kagan - first edition : 2001
// written in Borland CPP ver 3.1
#include "Stack.h"
#include <string.h>
int Check(Stack<char> a)
{
Stack<char> b;
int len = (a.StackLen())/2;
for(int i = 0;i < len;i++)
b.Push(a.Pop());
if(a.StackLen() > b.StackLen())
a.Pop();
while(!a.EmptyStack())
if(a.Pop() != b.Pop())
return 0;
return 1;
} // CHECK
void Process(char *data)
{
Stack<char> a(strlen(data),data);
char *msg[2] = {"not palindromic stack","palindromic stack"};
cout << setw(30) << "giving stack : " << a;
cout << setw(27) << msg[Check(a)] << endl;
cout << "-----------------------------------------------------"
"-------------------------n";
} // PROCESS
void main()
{
clrscr();
cout << "check palindromic stack.n";
cout << "-----------------------------------------------------"
"-------------------------n";
char *data[4] = {"abcdcba","abcdba","noel solw","noxinnixon"};
for(int i = 0;i < 4;i++)
Process(data[i]);
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// program Stack.h - page 393 . . . . .
// Header File for Stack class.
// 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 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 <<
// -------------------------------------------------------------------------
El programa trabaja con un header file, stack.h, que define una pila generica.
// program k19a6.cpp - page 396
// check palindromic stack.
// c++ exercices book - dr. gershon kagan - first edition : 2001
// written in Borland CPP ver 3.1
#include "Stack.h"
#include <string.h>
int Check(Stack<char> a)
{
Stack<char> b;
int len = (a.StackLen())/2;
for(int i = 0;i < len;i++)
b.Push(a.Pop());
if(a.StackLen() > b.StackLen())
a.Pop();
while(!a.EmptyStack())
if(a.Pop() != b.Pop())
return 0;
return 1;
} // CHECK
void Process(char *data)
{
Stack<char> a(strlen(data),data);
char *msg[2] = {"not palindromic stack","palindromic stack"};
cout << setw(30) << "giving stack : " << a;
cout << setw(27) << msg[Check(a)] << endl;
cout << "-----------------------------------------------------"
"-------------------------n";
} // PROCESS
void main()
{
clrscr();
cout << "check palindromic stack.n";
cout << "-----------------------------------------------------"
"-------------------------n";
char *data[4] = {"abcdcba","abcdba","noel solw","noxinnixon"};
for(int i = 0;i < 4;i++)
Process(data[i]);
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// program Stack.h - page 393 . . . . .
// Header File for Stack class.
// 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 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 <<
// -------------------------------------------------------------------------
