Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. #define MAX_SIZE 256
  7.  
  8. typedef struct node node;
  9.  
  10. struct node {
  11.     int item;
  12.     struct node *next;
  13. };
  14.  
  15. typedef struct stack stack;
  16.  
  17. struct stack{
  18.     int current_size;
  19.     node *items[MAX_SIZE];
  20. };
  21.  
  22. node* create_list(int item)
  23. {
  24.     node *head = (node*) malloc(sizeof(node));
  25.     head->item = item;
  26.     head->next =  NULL;
  27.     return head;
  28. }
  29.  
  30. node* add_end(node *head, int item)
  31. {
  32.     node *new=(node*)malloc(sizeof(node));
  33.     new->item=item;
  34.     new->next=NULL;
  35.     if(head == NULL)
  36.     {
  37.         return new;
  38.     }
  39.     else
  40.     {
  41.         node *aux=head;
  42.         while(aux->next!=NULL)
  43.         {
  44.             aux=aux->next;
  45.         }
  46.         aux->next=new;
  47.     }
  48.     return head;
  49. }
  50.  
  51. void print_list(node *a)
  52. {
  53.     if(a!=NULL)
  54.     {
  55.         node* aux=a;
  56.         while(aux!=NULL)
  57.         {
  58.             printf("%d ", aux->item);
  59.             aux=aux->next;
  60.         }
  61.     }
  62.     else
  63.     {
  64.         printf("lista vazia\n");
  65.     }
  66. }
  67.  
  68. stack* create_stack()
  69. {
  70.     stack *new_stack = (stack*) malloc(sizeof(stack));
  71.     new_stack->current_size=0;
  72.     int i;
  73.     for (i = 0; i < MAX_SIZE; i++)
  74.     {
  75.         new_stack->items[i] = NULL;
  76.     }
  77.     return new_stack;
  78. }
  79.  
  80. int is_empty(stack *stack)
  81. {
  82.     if(stack->current_size==0)
  83.         return 1;
  84.     else
  85.         return 0;
  86. }
  87.  
  88. void push(stack *stack, node* item)
  89. {
  90.     if(stack->current_size == MAX_SIZE)
  91.     {
  92.         return;//printf("Stack Overflow\n");
  93.     }
  94.     else
  95.     {
  96.         stack->items[stack->current_size++]=item;
  97.     }
  98. }
  99.  
  100. node* pop(stack *stack)
  101. {
  102.     if(!is_empty(stack))
  103.     {
  104.         node* aux=stack->items[--stack->current_size];
  105.         stack->items[stack->current_size]=NULL;
  106.         return aux;
  107.     }
  108. }
  109.  
  110. int main()
  111. {
  112.     char c[10];
  113.     int n;
  114.     stack* pilha=create_stack();
  115.     node* head=NULL;
  116.     node* aux1= NULL;
  117.     while(scanf("%s", c) != EOF)
  118.     {
  119.         if(strcmp(c,"PUSH") == 0)
  120.         {
  121.             char a;
  122.             scanf("%d%c", &n, &a);
  123.             head=add_end(head,n);
  124.             push(pilha,head);
  125.             while(a == ' ')
  126.             {
  127.                 scanf("%d%c", &n, &a);
  128.                 head=add_end(head,n);
  129.                 push(pilha,head);
  130.             }
  131.             head = NULL;
  132.         }
  133.         else
  134.         {
  135.             aux1=pop(pilha);
  136.             print_list(aux1);
  137.             printf("\n");
  138.         }
  139.     }
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement