-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue.c
180 lines (143 loc) · 3.06 KB
/
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
/*
* Basic queue example. Doesn't alter the queue elements' position value.
* This queue is not limited to a fix number of maximum amount of elements.
*
* @author J. Alvarez
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_STR_SIZE 50
typedef char String[MAX_STR_SIZE];
typedef struct Node {
String info;
int pos;
struct Node * next;
} Node;
typedef struct Queue {
Node * front;
Node * rear;
int size;
} Queue;
Node * createNode(String data);
void printNode(Node * node);
void freeNodeMemory(Node * node);
Queue * createQueue();
void enqueue(Queue ** queue, Node * node);
Node * dequeue(Queue ** queue); //Please free that node's memory after used
Node * peek(Queue * queue);
void printQueue(Queue * queue);
void freeQueueMemory(Queue * queue);
int main() {
Node * n1 = createNode("Pepe");
Node * n2 = createNode("Andrea");
Node * n3 = createNode("Steven");
Node * n4 = createNode("Darla");
Queue * q = createQueue();
enqueue(&q,n1);
enqueue(&q,n2);
enqueue(&q,n3);
enqueue(&q,n4);
printQueue(q);
printf("The front of the queue is: ");
printNode(peek(q));
Node * nextInQ = dequeue(&q);
printf("I have dequeued:");
printNode(nextInQ);
printf("Disposing it for good...\n");
freeNodeMemory(nextInQ);
printf("So the new front is: ");
printNode(peek(q));
printf("And now the queue is...\n");
printQueue(q);
freeQueueMemory(q);
return 0;
}
Node * createNode(String data) {
Node * node = malloc(sizeof(Node));
strcpy(node->info,data);
node->pos = -1;
node->next = NULL;
}
void printNode(Node * node) {
if(!node)
printf("NULL\n");
else
printf("[%d]: %s\n",node->pos,node->info);
}
void freeNodeMemory(Node * node) {
if(node)
free(node);
}
Queue * createQueue() {
Queue * queue = malloc(sizeof(Queue));
queue->front = NULL;
queue->rear = NULL;
}
void enqueue(Queue ** queue, Node * node) {
if(!((*queue)->rear)) {
node->pos = 0;
(*queue)->rear = node;
(*queue)->front = node;
(*queue)->size = 1;
}
else {
printNode(node);
node->pos = (*queue)->rear->pos + 1;
(*queue)->rear->next = node;
(*queue)->rear = node;
(*queue)->size++;
}
}
Node * dequeue(Queue ** queue) {
if(!(*queue))
return NULL;
else {
Node * head = (*queue)->front;
if(!head->next) {
(*queue)->front = NULL;
(*queue)->rear = NULL;
}
else
(*queue)->front = head->next;
(*queue)->size--;
return head;
}
}
Node * peek(Queue * queue) {
if(!queue)
return NULL;
else
return queue->front;
}
void printQueue(Queue * queue) {
Node * aux = queue->front;
if(!queue->front)
printf("The queue is empty\n");
else {
char * plural = queue->size == 1 ? "element" : "elements";
printf("The queue has %d %s and is as follows:\n",queue->size, plural);
do {
printNode(aux);
aux = aux->next;
} while(aux->next);
printNode(aux);
}
}
void freeQueueMemory(Queue * queue) {
if(queue) {
if(!(queue->front)) {
free(queue);
}
else {
Node * initial = queue->front;
while(initial->next) {
Node * aux = initial;
initial = aux->next;
free(aux);
}
free(initial);
free(queue);
}
}
}