-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathup_down.c
80 lines (70 loc) · 1.71 KB
/
up_down.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//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;
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
{
//printf("%s\n", item);
NODE *new_top = (NODE*) malloc(sizeof(NODE));//aloca espaço para um novo nó
strcpy(new_top->linha, item); //acessa o endereço de new_top->linha e escreve o item lá
new_top->next = stack->top;
stack->top = new_top;
}
int is_empty(STACK* stack)
{
if (stack->top == NULL)
{
return 1;
}
return 0;
}
NODE* pop(STACK *stack)
{
NODE *pooped = stack->top;
stack->top = stack->top->next; //atualizar o topo + remoção logica do topo
pooped->next = NULL;
return pooped;
}
int main()
{
char linha1[MAX_LENGHT];
STACK* stack = init_stack();
//printf("AAAAAa#\n");
int counter = 0;
while(1)
{
void* p = fgets(linha1,MAX_LENGHT,stdin);
if (!p)
{
break;
}
++counter;
push(stack, linha1);
}
strcat(stack->top->linha,"\n");
//printf("%s \t %s",stack->top->linha, stack->top->next->linha);
while (is_empty(stack)!= 1)
{
char out[MAX_LENGHT];
NODE* aux = pop(stack);
printf("%s",aux->linha);
free(aux);
}
return 0;
}