Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define max 5
- struct pilha{
- int topo;
- int pilha[max];
- }p;
- void push (int x, struct pilha *p){
- if(p->topo == max-1)
- printf("a pilha esta cheia\n");
- else
- p->pilha[++p->topo] = x;
- }
- int pop (struct pilha *p){
- p->topo--;
- }
- void verificar(struct pilha *p){
- printf("o topo da pilha eh: %d\n", p->pilha[p->topo]);
- }
- void mostrar (struct pilha *p){
- int aux[max];
- int topoaux = -1;
- if(p->topo != -1){
- for(int i = 0; p->topo >= i; p->topo--,topoaux++){
- printf("%d\n", p->pilha[p->topo]);
- aux[topoaux] = p->pilha[p->topo];
- }
- for(int i = 0; topoaux >= 0; topoaux--){
- p->pilha[p->topo] = aux[topoaux];
- p->topo++;
- }
- }
- else
- printf("nao ha elementos a ser mostrado\n");
- }
- void main(){
- int n, x;
- p.topo = -1;
- do{
- printf("1 - Inserir elementos na pilha\n2 - Retirar elementos na pilha\n3 - Verificar o elemento que esta no topo da pilha\n4 - Mostrar todos elementos da pilha\n0 - Sair\n");
- scanf("%d", &n);
- switch(n){
- case 1:
- printf("digite o valor que deseja inserir na pilha\n");
- scanf("%d", &x);
- push(x, &p);
- break;
- case 2:
- pop(&p);
- break;
- case 3:
- verificar(&p);
- break;
- case 4:
- mostrar(&p);
- break;
- case 0:
- return;
- default:
- printf("valor invalido\n");
- }
- }while(n != 0);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement