pablosoares

pilha par

Oct 22nd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define max 20
  4.  
  5. struct stack{
  6.    
  7.     int vet[max];
  8.     int top;
  9.    
  10.     }typedef node;
  11.    
  12. void iniciar(node *p) {
  13.     p->top=-1;
  14. }
  15.  
  16. void push(node *p,int x){
  17.      
  18.     if (p->top==19) printf("%s","\nStack Overflow\n"); 
  19.     else{
  20.     p->top++;
  21.     p->vet[p->top]=x;
  22.     }
  23.    
  24.     }
  25.    
  26.    
  27. int pop(node *p){
  28.    
  29.     int x;
  30.    
  31.     if(p->top==-1)printf("%s","\nStack Underflow\n");
  32.        
  33.     x=p->vet[p->top];
  34.     p->top--;
  35.    
  36.     return x;
  37.      
  38.     }
  39.    
  40.  
  41. void mostrar(node *p){
  42.    
  43.     int i;
  44.     if(p->top==-1)printf("%s","\nStack Underflow\n");
  45.     printf("\n");
  46.     for(i=p->top;i>=0;i--) printf("[%d]\n",p->vet[i]); 
  47.        
  48.     }
  49.    
  50. void menor100(node *p){
  51.    
  52.     printf("%d\n",p->vet[p->top]);
  53.    
  54.     }
  55.    
  56. int menu(){
  57.    
  58.     int opc;
  59.    
  60.     printf(" _________________________________\n");
  61.     printf("|_______________Menu______________|\n");
  62.     printf("|                                 |\n");
  63.     printf("|0-Sair                           |\n");
  64.     printf("|1-Inserir                        |\n");
  65.     printf("|2-Excluir.                       |\n");
  66.     printf("|3-Mostrar.                       |\n");
  67.     printf("|4-Topo                           |\n");
  68.     printf("|_________________________________|\n");
  69.    
  70.     printf("Digite uma opcao: ");
  71.     scanf("%d",&opc);
  72.    
  73.     return opc;
  74.    
  75.     }
  76.    
  77.        
  78. int main(){
  79.    
  80.    int x,opc;
  81.    node pilha;
  82.    iniciar(&pilha);
  83.    
  84.    do{
  85.        
  86.        opc=menu();
  87.        
  88.        switch(opc){
  89.            
  90.            case 0:
  91.            
  92.            
  93.                  
  94.                  break;
  95.                  
  96.            case 1:
  97.            
  98.                  printf("Digite um valor: ");
  99.                  scanf("%d",&x);        
  100.                  push(&pilha,x);
  101.            
  102.                  
  103.                  break;
  104.                  
  105.            case 2:
  106.                  
  107.                  printf("Excluido: %d\n",pop(&pilha));
  108.                  
  109.                  break;
  110.                  
  111.            case 3:
  112.            
  113.                 mostrar(&pilha);
  114.                
  115.                  break;
  116.                  
  117.            case 4:
  118.            
  119.                 menor100(&pilha);
  120.                
  121.                  break;
  122.                  
  123.            default:
  124.            
  125.                   printf("Opcao invalida\n");
  126.                  
  127.                   break;
  128.            
  129.            
  130.            }
  131.            
  132.            system("pause");
  133.            system("cls");
  134.        
  135.        }while(opc!=2);
  136.    
  137.  
  138.    
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment