mantertius

up_down2.c

Apr 6th, 2021 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. //https://thehuxley.com/problem/2133?quizId=6095
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #define MAX_LENGHT 1000
  7.  
  8. typedef struct node
  9. {
  10.     char linha[MAX_LENGHT];
  11.     struct node *next;  
  12. }NODE;
  13.  
  14. typedef struct stack
  15. {
  16.     NODE* top;
  17.  
  18. }STACK;
  19.  
  20. typedef struct string
  21. {
  22.     char str[MAX_LENGHT];
  23.  
  24. }STR;
  25.  
  26. STACK* init_stack()//é chamado sem argumentos e cria um stack
  27. {
  28.     STACK* new_stack = (STACK*) malloc(sizeof(STACK));
  29.     new_stack->top = NULL;  
  30.     return new_stack;   //retorna um novo_stack para ser usado
  31. }
  32.  
  33. void push(STACK *stack, char *item)//como o topo é unico para todo o stack, só preciso mostrar o topo
  34. {
  35.     NODE *new_top = (NODE*) malloc(sizeof(NODE)); //aloca espaço para um novo nó
  36.     strcpy(stack->top->linha, item);
  37.     new_top->next = stack->top;
  38.     stack->top = new_top;
  39. }
  40.  
  41. int is_empty(STACK* stack)
  42. {
  43.     if (stack->top == NULL)
  44.     {
  45.         return 1;
  46.     }
  47.     return 0;
  48. }
  49.  
  50. STR* pop(STACK *stack)
  51. {
  52.     STR *pooped = (STR*) malloc(sizeof(STR));
  53.     strcpy(pooped->str, stack->top->linha);
  54.     stack->top = stack->top->next;
  55.     free(stack->top);  
  56.     return pooped;
  57. }
  58.  
  59. int main()
  60. {
  61.     char linha1[MAX_LENGHT];
  62.     int p = scanf("%s", linha1);
  63.     STACK* stack = init_stack();
  64.     printf("AAAAAa#\n");
  65.     while (p != EOF)
  66.     {
  67.         push(stack, linha1);
  68.     }
  69.     printf("BBBBBBB#\n");
  70.     while (is_empty(stack)!= 1)
  71.     {
  72.         char out[MAX_LENGHT];
  73.         STR* temp;
  74.         temp = pop(stack);
  75.         printf("CCCCCCCC#\n");
  76.         strcpy(linha1, temp->str);
  77.         fputs(out,stdout);
  78.     }
  79.     printf("CCCCCCCC#\n");
  80.  
  81.    
  82.     return 0;
  83.    
  84. }
Add Comment
Please, Sign In to add comment