Ejercicio de pilas en c++

Páginas: 2 (292 palabras) Publicado: 28 de junio de 2011
#include<stdio.h>
#define STACKMAX 100
#include<stdlib.h>
typedef char Element_type;
// definición de pila
// Contiene top referenciando el elemento top
// Contiene una pila dearreglo
typedef struct stacker {
int top;
Element_type stack[STACKMAX];
} Stack_type;

// La función prototipo para las operaciones de pila
void newStack(Stack_type *stack);
voidpush(Stack_type *st_ptr, Element_type elem);
Element_type pop(Stack_type *st_ptr);
Element_type top(Stack_type *st_ptr);
int isEmpty(Stack_type *st_ptr);
int isFull(Stack_type *st_ptr);

int main(){Stack_type *sptr;
char element;
int i, k=0;
sptr=(Stack_type *)malloc(sizeof(Stack_type));

newStack(sptr);
printf("Ingrese la cadena a invertir:\n");
scanf("%c", &element);
while(element!='\n'){
push(sptr, element);
k++;
scanf("%c", &element);

}

printf("Los elementos de la pila son:\n");
for(i=0; i<k; i++)printf("%c->", sptr->stack[i]);
getch();
printf("\nla cadena invertida es :\n");

while (!isEmpty(sptr)){
element=pop(sptr);

printf("%c",(char) element);
getch();
}printf("\n");
}
/* New inicializa la pila al estado vacío */
void newStack(Stack_type *st_ptr) {
st_ptr->top = 0;
}
/* La Operación Push */
void push(Stack_type *st_ptr, Element_type elem){if(st_ptr==NULL)
return;
if (isFull(st_ptr))
return;
else {
st_ptr->stack[st_ptr->top++]= elem;
return;
}
}

/* La Operación Pop */

Element_type pop(Stack_type*st_ptr) {
if (isEmpty(st_ptr))
return -1;
else
return st_ptr->stack[--st_ptr->top];
}
/* La Operación Top */
Element_type top(Stack_type *st_ptr) {
if (isEmpty(st_ptr))return -1;
else
return st_ptr->stack[st_ptr->top-1];
}
/* Para ver si la pila es vacía */
int isEmpty(Stack_type *st_ptr) {
if(st_ptr == NULL)
return (-1);
if(st_ptr->top ==...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • PILAS EN C
  • Pila C
  • PILAS C#
  • Pilas c++
  • ejercicios c++
  • Ejercicios c++
  • ejercicio C
  • Ejercicios En C++

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS