-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue_desc.c
190 lines (166 loc) · 2.63 KB
/
queue_desc.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
/* AUTHOR: M Jake Palanker */
/* Terrible basic queue */
#include <stdlib.h>
#include <stdio.h>
typedef struct queue_entry queue_entry;
typedef struct
{
size_t size;
queue_entry *head;
queue_entry *tail;
} queue;
struct queue_entry
{
int data;
queue_entry *next;
};
int queue_size(const queue *q)
{
return q->size;
}
int queue_push(int data, queue *q)
{
queue_entry *new;
queue_entry *last;
queue_entry *next;
new = malloc(sizeof(struct queue_entry));
new->data = data;
new->next = NULL;
if (queue_size(q) == 0)
{
q->head = new;
q->tail = new;
}
else if (q->head->data < data)
{
new->next = q->head;
q->head = new;
}
else
{
next = q->head;
last = next;
while ((next != NULL) && (next->data > data))
{
last = next;
next = next->next;
}
last->next = new;
new->next = next;
}
q->size = q->size + 1;
return 0;
}
int queue_pop(queue *q)
{
queue_entry *temp;
int retval;
if (q->size == 0)
{
printf("THIS QUEUE IS EMPTY, WHY DID YOU POP?\n");
return -1;
}
else if (q->size == 1)
{
q->size = 0;
retval = q->head->data;
free(q->head);
q->head = NULL;
q->tail = NULL;
}
else
{
q->size = q->size -1;
retval = q->head->data;
temp = q->head->next;
free(q->head);
q->head = temp;
}
return retval;
}
queue new_queue()
{
queue new;
new.size = 0;
new.head = NULL;
new.tail = NULL;
return new;
}
int queue_destroy(queue *q)
{
while (queue_size(q) > 0)
{
queue_pop(q);
}
q->head = NULL;
q->tail = NULL;
return 0;
}
int is_in(int data, const queue *q)
{
struct queue_entry *ptr;
ptr = q->head;
while(ptr != NULL)
{
if (data == ptr->data)
return 1;
ptr = ptr->next;
}
return 0;
}
int queue_remove(int data, queue *q)
{
struct queue_entry *last;
struct queue_entry *next;
if (queue_size(q) == 0)
{
return -1;
}
next = q->head;
if (next->data == data)
{
queue_pop(q);
return 0;
}
last = next;
next = next->next;
while (next != NULL)
{
if (next->data == data)
{
last->next = next->next;
free(next);
q->size -= 1;
return 0;
}
last = next;
next = next->next;
}
return -1;
}
int queue_save(int *buffer, queue *q)
{
int size;
int i;
queue_entry *ptr;
size = (q->size);
ptr = q->head;
buffer[0] = size;
for (i = 0; i < size; i++)
{
buffer[i+1] = ptr->data;
ptr = ptr->next;
}
return size;
}
int queue_load(int *buffer, queue *q)
{
int size;
int i;
size = buffer[0];
for (i = 0; i < size; i ++)
{
queue_push(buffer[i+1], q);
}
return size;
}