Advertisement
pablosoares

Pilha

Apr 19th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node{
  5.     int valor;
  6.     struct Node *point;
  7.     };
  8.    
  9. typedef struct Node node;
  10.  int menu(){
  11.     int op;
  12.     printf(" ______________Menu_____________\n");
  13.     printf("|                               |\n");
  14.     printf("|0- Sair                        |\n");
  15.     printf("|1- Inserir                     |\n");
  16.     printf("|2- Remover                     |\n");
  17.     printf("|3- Mostrar                     |\n");
  18.     printf("|_______________________________|\n");
  19.     printf("\nInsira sua opcao: ");
  20.     scanf("%d",&op);
  21.     return op;
  22.     }
  23.     /*
  24. void push(node **top,int valor){
  25.    
  26.     node *temp = (node*) malloc(sizeof(node*));
  27.     temp->point=*top;
  28.     temp->valor=valor;
  29.     *top=temp;
  30.  
  31.     }
  32.    
  33. void mostrar(node **top) {
  34.    
  35.     node * temp = *top;
  36.     while(temp != NULL) {
  37.         printf("%d\n", temp->valor);
  38.         temp = temp->point;
  39.     }
  40.    
  41. }
  42.  
  43. int pop(node **top){
  44.     int x;
  45.     node *temp = *top;
  46.     x=temp->valor;
  47.     temp=temp->point;
  48.     *top=temp;
  49.      return x;
  50.     }
  51.     */
  52.    
  53. void push(node *top,int valor){
  54.    
  55.     node *novo = (node*) malloc(sizeof(node*));
  56.     node *aux = top->point;
  57.         novo->valor=valor;
  58.         novo->point=aux;
  59.         top->point=novo;
  60.     }
  61.    
  62.    
  63. int pop(node *top){
  64.    
  65.     node *temp = top;
  66.     node *aux;
  67.     aux=temp->point;
  68.     temp=aux;
  69.     int x;
  70.     x=temp->valor;
  71.     temp=temp->point;
  72.     free(temp);
  73.     return x;
  74.     }
  75.    
  76. void mostrar(node *top) {
  77.    
  78.     node * temp = top;
  79.     node *aux;
  80.     aux=temp->point;
  81.     temp=aux;
  82.     while(temp != NULL){
  83.         printf("%d\n", temp->valor);
  84.         temp = temp->point;
  85.     }
  86.    
  87. }
  88.  
  89. int main(){
  90.     node top;
  91.     top.point=NULL;
  92.     int valor,op;
  93.  
  94. do{
  95.         op=menu();
  96.         switch(op){
  97.             case 0: break;
  98.             case 1:
  99.                   printf("Digite o valor a inserir: ");
  100.                   scanf ("%d",&valor);
  101.                   push(&top,valor);
  102.                   break;
  103.              case 2:
  104.                   printf("%d\n",pop(&top));
  105.                    break;
  106.              case 3:
  107.                   mostrar(&top);
  108.                     break;
  109.  
  110.              default:
  111.                    printf("Opcao invalida\n");
  112.                     break;
  113.             }
  114.             system("pause");
  115.             system("cls");
  116.         }while(op!=0);
  117.     return 0;
  118.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement