-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
114 lines (87 loc) · 1.85 KB
/
queue.c
File metadata and controls
114 lines (87 loc) · 1.85 KB
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
#include "queue.h"
pQueue createQueue() {
pQueue q = (pQueue)malloc(sizeof(Queue));
q->top = NULL;
q->tail = NULL;
q->size = 0;
return q;
}
int enQueue(pQueue q, char data[]) {
pQueueNode node = (pQueueNode)malloc(sizeof(QueueNode));
strncpy(node->data, data, DATA_SIZE);
node->data[DATA_SIZE] = '\0';
node->isDone = 0;
node->next = NULL;
node->prev = NULL;
if( ! q->top ) {
q->top = node;
q->tail = node;
q->size++;
return 0;
}
node->next = q->tail;
q->tail->prev = node;
q->tail = node;
q->size++;
return 0;
}
pQueueNode deQueue(pQueue q) {
if(q->size == 0) {
// printf("queue is empty!\n");
return NULL;
}
pQueueNode rm = q->top;
if(q->size == 1) {
q->top = NULL;
q->tail = NULL;
q->size--;
return rm;
}
q->top = q->top->prev;
q->top->next = NULL;
q->size--;
return rm;
}
int size(pQueue q) {
return q->size;
}
int isEmpty(pQueue q) {
if(q->size == 0) {
return 1;
}
return 0;
}
int printQueue(pQueue q) {
// pQueueNode curr = q->tail;
// while( curr ) {
// for(int i = 0; i < DATA_SIZE; i++) {
// printf("%c", curr->data[i]);
// }
// if(curr->next) {
// printf(" -> ");
// }
// curr = curr->next;
// }
pQueueNode curr = q->top;
while( curr ) {
for(int i = 0; i < DATA_SIZE; i++) {
printf("%c", curr->data[i]);
}
if(curr->prev) {
printf(" -> ");
}
curr = curr->prev;
}
printf("\nend of queue\n");
return 0;
}
int freeQueue(pQueue q) {
pQueueNode del = NULL;
while( q->top ) {
del = q->top;
q->top = q->top->prev;
free(del);
}
free(q);
return 0;
}