Advertisement
mantertius

up_down.c

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