-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverter.c
82 lines (74 loc) · 1.51 KB
/
inverter.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
81
82
//https://thehuxley.com/problem/264?quizId=6096
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG if(1)
typedef struct node
{
int data;
struct node* previous;
struct node* next;
}Node;
Node* init()
{
return NULL;
}
int isEmpty(Node* head)
{
return (head == NULL);
}
Node* add(Node* head, int data) //insere na cabeça, sempre :)
{
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->data = data;
new_node->next = head;
new_node->previous = NULL; //como o novo nó sempre vai estar na cabeça, seu anterior é sempre nulo!
if (head != NULL)
{
head->previous = new_node;
}
return new_node;
}
void printer(Node* head, int fluxo)
{
if (fluxo == 1)
{
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
}
Node* getLastNode(Node* node)
{
if(node==NULL)
{
return NULL;
}
else if(node->next ==NULL)
{
return node;
}
else
{
return getLastNode(node->next);
}
}
int main()
{
int n;
Node* lista = init();
Node* tail;
int i = 0;
while (scanf("%d",&n) != EOF)
{
lista = add(lista,n);
//DEBUG printf("Elemento %d adicionado\n",n);
}
//DEBUG printf("Começando a imprimir a lista normal!\n");
printer(lista,1);//fluxo normal
//DEBUG printf("Começando a imprimir a lista reversa!\n");
//printer(lista,-1);//fluxo reverso
}