-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_LinkedList.c
167 lines (135 loc) · 3.63 KB
/
04_LinkedList.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "00_helper.c"
#include<stdlib.h>
struct LinkedList {
struct Node *front;
struct Node *tail;
};
struct Node {
int value;
unsigned int index;
struct Node *next;
};
void printDataNodo(const struct Node *node);
int isEmpty(const struct LinkedList *list) {
return list->front == NULL;
}
int getLastIndex(const struct LinkedList *list) {
return list->tail->index;
}
struct LinkedList* newLinkedList() {
struct LinkedList *temp = (struct LinkedList*) malloc(sizeof(struct LinkedList));
temp->front = NULL;
temp->tail = NULL;
return temp;
}
struct Node* newNode(const int value) {
struct Node *temp = (struct Node*) malloc(sizeof(struct Node));
temp->next = NULL;
temp->value = value;
return temp;
}
void enQueue(struct LinkedList *list, const int value) {
struct Node* node = newNode(value);
if (isEmpty(list)) {
node->index = 0;
list->front = node;
list->tail = node;
return;
}
node->index = getLastIndex(list) + 1;
list->tail->next = node;
list->tail = node;
}
struct Node* deQueue(struct LinkedList *list) {
if (isEmpty(list)) {
return NULL;
}
struct Node* firstElement = list->front;
list->front = firstElement->next;
// TODO: free.
// If it`s the end.
if (isEmpty(list)) {
list->tail = NULL;
}
return firstElement;
}
struct Node* peek(const struct LinkedList *list) {
if (isEmpty(list)) {
return NULL;
}
return list->front;
}
struct Node* getTail(const struct LinkedList *list) {
return list->tail;
}
// TODO: Podria controlarse el tamaño mediante enQueue y deQueue en una variable size.
int size(const struct LinkedList *list) {
if (isEmpty(list)) {
return 0;
}
int length = 0;
struct Node* elem = list->front;
do {
length++;
elem = elem->next;
} while(elem != NULL);
return length;
}
int main(int argc, char const *argv[]) {
struct LinkedList* list1 = newLinkedList();
int value = 0;
int index = 0;
do {
printf("--- Write the number Nº %d: ", ++index);
scanf("%d", &value);
if (value) {
enQueue(list1, value);
}
} while(value);
int opcion = 0;
do {
printf("Choose: \n");
printf("\t-00: exit\n");
printf("\t-01: peek\n");
printf("\t-02: deQueue\n");
printf("\t-03: enQueue\n");
printf("¿What do you want?:\t");
scanf("%d", &opcion);
switch (opcion) {
case 1:
printDataNodo(peek(list1));
break;
case 2:
printDataNodo(deQueue(list1));
break;
case 3:
printf("--- Write the number Nº %d: ", ++index);
scanf("%d", &value);
if (value) {
enQueue(list1, value);
}
break;
default:
break;
}
} while (opcion);
return 0;
}
void printDataNodo(const struct Node *node) {
if (node == NULL) {
return;
}
printf("\t| Valor: %d ", node->value);
printf("\t| Id: ");
if (node->index < 10) {
printf("0");
}
printf("%d", node->index);
printf("\t\t| Sgt: ");
if (node->next != NULL) {
printf(ANSI_COLOR_GREEN "True\t" ANSI_COLOR_RESET);
} else {
printf(ANSI_COLOR_RED "False\t" ANSI_COLOR_RESET);
}
printf("|\n");
}