Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://thehuxley.com/problem/2133?quizId=6095
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MAX_LENGHT 1000
- typedef struct node
- {
- char linha[MAX_LENGHT];
- struct node *next;
- }NODE;
- typedef struct stack
- {
- NODE* top;
- }STACK;
- typedef struct string
- {
- char str[MAX_LENGHT];
- }STR;
- STACK* init_stack()//é chamado sem argumentos e cria um stack
- {
- STACK* new_stack = (STACK*) malloc(sizeof(STACK));
- new_stack->top = NULL;
- return new_stack; //retorna um novo_stack para ser usado
- }
- void push(STACK *stack, char *item)//como o topo é unico para todo o stack, só preciso mostrar o topo
- {
- NODE *new_top = (NODE*) malloc(sizeof(NODE)); //aloca espaço para um novo nó
- strcpy(stack->top->linha, item);
- new_top->next = stack->top;
- stack->top = new_top;
- }
- int is_empty(STACK* stack)
- {
- if (stack->top == NULL)
- {
- return 1;
- }
- return 0;
- }
- STR* pop(STACK *stack)
- {
- STR *pooped = (STR*) malloc(sizeof(STR));
- strcpy(pooped->str, stack->top->linha);
- stack->top = stack->top->next;
- free(stack->top);
- return pooped;
- }
- int main()
- {
- char linha1[MAX_LENGHT];
- int p = scanf("%s", linha1);
- STACK* stack = init_stack();
- printf("AAAAAa#\n");
- while (p != EOF)
- {
- push(stack, linha1);
- }
- printf("BBBBBBB#\n");
- while (is_empty(stack)!= 1)
- {
- char out[MAX_LENGHT];
- STR* temp;
- temp = pop(stack);
- printf("CCCCCCCC#\n");
- strcpy(linha1, temp->str);
- fputs(out,stdout);
- }
- printf("CCCCCCCC#\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment