Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     #include <string.h>
  4.     struct lista
  5.     {
  6.         int item;
  7.         struct lista *next;
  8.     };
  9.     struct pilha
  10.     {
  11.         struct lista *array[50];
  12.     };
  13.     struct pilha *criar_pilhacomlista()
  14.     {
  15.         struct pilha *novapilha=malloc(sizeof(struct pilha));
  16.         int i;
  17.         for(i=0;i<50;i++)
  18.         {
  19.             novapilha->array[i]=NULL;
  20.         }
  21.  
  22.         return novapilha;
  23.  
  24.     }
  25.     struct lista* createdlinklist()
  26.     {
  27.         return NULL;
  28.     }
  29.     struct lista* add(int item)
  30.     {
  31.         struct lista *novoitem = malloc(sizeof(struct lista));
  32.         novoitem->item = item;
  33.         novoitem->next = NULL;
  34.         //pilha->array[indice] = novoitem;
  35.         return novoitem;
  36.        
  37.     }
  38.     void add_elemento(struct pilha *pilha,int item,int indice)
  39.     {
  40.         struct lista *aux = createdlinklist();
  41.         aux = add(item);
  42.         aux->next = pilha->array[indice];
  43.         pilha->array[indice] = aux;
  44.  
  45.     }
  46.     int is_empty(struct pilha *pilha)
  47.     {
  48.         if(pilha->array[0] == NULL)
  49.         {
  50.             return 1;
  51.         }
  52.         else
  53.         {
  54.             return 0;
  55.         }
  56.     }
  57.     void pop(struct pilha *pilha,int maior) // remove o topo
  58.     {
  59.         if(is_empty(pilha))
  60.         {
  61.             printf("STACK UNDERFLOW\n");
  62.             return ;
  63.         }
  64.         else
  65.         {
  66.             struct lista *aux = pilha->array[maior];
  67.             printfila(aux);
  68.             printf("\n");
  69.             pilha->array[maior]=NULL;
  70.            
  71.            
  72.         }
  73.     }
  74.     void printfila(struct lista *head)
  75.     {
  76.         if(head!=NULL)
  77.         {
  78.             printfila(head->next);
  79.             printf("%d ", head->item);
  80.         }
  81.     }
  82.     int main()
  83.     {
  84.         struct pilha *pilha = criar_pilhacomlista();
  85.         int number,indice=0;
  86.         char comando[10],espaco;
  87.         while(scanf("%s ", comando)!=EOF)
  88.         {
  89.  
  90.             if(strcmp(comando,"PUSH")==0)
  91.             {
  92.                 scanf("%d%c", &number, &espaco);
  93.                 while(espaco!='\n')
  94.                 {
  95.                     add_elemento(pilha,number,indice);
  96.                     scanf(" %d%c ", &number, &espaco);
  97.  
  98.                 }
  99.                 indice++;
  100.  
  101.             }
  102.             else if(strcmp(comando,"POP")==0)
  103.             {
  104.                 pop(pilha,--indice);
  105.                
  106.             }
  107.         }
  108.  
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement