-
Notifications
You must be signed in to change notification settings - Fork 0
/
ada_and_queue.c
260 lines (219 loc) · 5.43 KB
/
ada_and_queue.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* iagorrr ;)
Double linked list implementation.
- Constructor
- Empty
- set first
- set last
- move next
- move previous
- remove current.
- push back
- pop back
- push front
- pop front
*/
#include <stdio.h>
#include <stdlib.h>
#define Item int
#define queue linked_list_root_t
typedef struct linked_list_node_t
{
Item item;
struct linked_list_node_t *next;
struct linked_list_node_t *previous;
} linked_list_node_t;
typedef struct linked_list_root_t
{
linked_list_node_t *first;
linked_list_node_t *last;
linked_list_node_t *current;
int size;
} linked_list_root_t;
int linked_list_constructor(linked_list_root_t *x)
{
x->first = NULL;
x->last = NULL;
x->current = NULL;
x->size = 0;
return 1;
}
int linked_list_empty(linked_list_root_t *x)
{
return x->size == 0;
}
void linked_list_set_first(linked_list_root_t *x)
{
x->current = x->first;
}
void linked_list_set_last(linked_list_root_t *x)
{
x->current = x->last;
}
void linked_list_move_next(linked_list_root_t *x)
{
x->current = x->current->next;
}
void linked_list_move_previous(linked_list_root_t *x)
{
x->current = x->current->previous;
}
/*
Remove the current element we are looking at and points the current to next.
*/
void linked_list_remove_current(linked_list_root_t *x)
{
linked_list_node_t *t = x->current;
if(x->current->previous == NULL && x->current->next == NULL) // single element.
{
x->first = NULL;
x->last = NULL;
}
else if(x->current->previous == NULL) // is the first element.
{
x->first = x->current->next;
x->first->previous = NULL;
}
else if(x->current->next == NULL) // is the last element
{
x->last = x->current->previous;
x->last->next = NULL;
}
else // what ever element in the middle.
{
x->current->next->previous = x->current->previous;
x->current->previous->next = x->current->next;
}
x->size--;
x->current = x->current->next;
free(t);
}
Item linked_list_front(linked_list_root_t *x)
{
return x->first->item;
}
int linked_list_push_front(linked_list_root_t *x, Item i)
{
linked_list_node_t *new_node = malloc(sizeof(linked_list_node_t));
if(new_node == NULL) return 0;
new_node->previous = NULL; // as it gonna be the first one.
new_node->item = i;
if(x->size == 0)
{
new_node->next = NULL;
x->first = new_node;
x->last = new_node;
}
else
{
x->first->previous = new_node;
new_node->next = x->first;
x->first = new_node;
}
x->size++;
return 1;
}
int linked_list_pop_front(linked_list_root_t *x)
{
// store a pointer to ther first so we can free it later.
linked_list_node_t *t = x->first;
// now the first is the one after the first '-'
x->first = x->first->next;
if(x->size == 1)
{
x->last = NULL;
}
// :P
x->size--;
free(t);
return 1;
}
Item linked_list_back(linked_list_root_t *x)
{
return x->last->item;
}
int linked_list_push_back(linked_list_root_t *x, Item i)
{
linked_list_node_t *new_node = malloc(sizeof(linked_list_node_t));
if(new_node == NULL) return 0;
new_node->next = NULL;
new_node->item = i;
if(x->size == 0)
{
x->first = new_node;
x->last = new_node;
new_node->previous = NULL;
}
else
{
x->last->next = new_node;
new_node->previous = x->last;
x->last = new_node;
}
x->size++;
return 1;
}
int linked_list_pop_back(linked_list_root_t *x)
{
// store the last element memory address to we can free it later.
linked_list_node_t *t = x->last;
// our last element now is the previous to the old last.
x->last = x->last->previous;
// as he is the last one now there is no next after '''him'''.
if(x->last != NULL)
x->last->next = NULL;
else {
x->first = NULL;
}
// :)
x->size--;
free(t);
return 1;
}
int main()
{
int n; scanf("%d", &n);
int reverse = 0;
linked_list_root_t adamae; linked_list_constructor(&adamae);
for(int i = 0; i < n; ++i)
{
char *str = malloc(sizeof(char)*20);
scanf("%s", str);
int temp;
switch(str[0]){
case ('t'):
scanf("%d", &temp);
if(reverse) linked_list_push_back(&adamae, temp);
else linked_list_push_front(&adamae, temp);
break;
case ('f'):
if(linked_list_empty(&adamae))
{
printf("No job for Ada?\n");
break;
}
if(reverse){ temp = linked_list_back(&adamae); linked_list_pop_back(&adamae);}
else{ temp = linked_list_front(&adamae); linked_list_pop_front(&adamae);}
printf("%d\n", temp);
break;
case ('b'):
if(linked_list_empty(&adamae))
{
printf("No job for Ada?\n");
break;
}
if(!reverse){ temp = linked_list_back(&adamae); linked_list_pop_back(&adamae);}
else{ temp = linked_list_front(&adamae); linked_list_pop_front(&adamae);}
printf("%d\n", temp);
break;
case ('r'):
reverse = reverse == 1 ? 0 : 1;
break;
case ('p'):
scanf("%d", &temp);
if(!reverse) linked_list_push_back(&adamae, temp);
else linked_list_push_front(&adamae, temp);
break;
}
}
}
// AC