Repository navigation
Expand file tree
/
Copy pathqueue.c
More file actions
executable file
·188 lines (167 loc) · 5.72 KB
/
Copy pathqueue.c
File metadata and controls
executable file
·188 lines (167 loc) · 5.72 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
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
/******************************************************************************
* Module: queue.c *
* *
* Author: Rob Jones *
* *
* Purpose: CSci 726 Discrete-Event Simulation: PROJECT #3 *
* Provides the queue structure and operations for the simulator. *
* *
* Version: 5-01-97 *
*******************************************************************************/
#include <stdlib.h>
#include "queue.h"
#include "misc.h"
#ifdef SJF_QUEUE
static list *free_nodes = nullptr; /* recycled list nodes */
static list *New(void)
/******************************************************************************
Returns a new list structure (node). For more efficient memory management,
a list of previously freed nodes is kept and used to later return new nodes.
If the free nodes list is depleted then a new node is allocated in memory.
*******************************************************************************/
{
list *ptr;
if (free_nodes == nullptr) {
ptr = (list *)malloc(sizeof(list));
if (ptr == nullptr) {
error(MEM_ERR);
}
} else {
ptr = free_nodes;
free_nodes = free_nodes->next;
}
ptr->item = (item_type)0;
ptr->next = nullptr;
return(ptr);
}
static void Free(list *ptr)
/******************************************************************************
Frees the node pointed to by 'ptr' and stores it in the free nodes list
for later used.
*******************************************************************************/
{
ptr->next = free_nodes;
free_nodes = ptr;
}
void CreateQueue(queue_type *q)
/******************************************************************************
Initializes the queue structure to an empty state.
*******************************************************************************/
{
q->contents = nullptr;
q->number = 0;
}
void DeleteQueue(queue_type *q)
/******************************************************************************
Deletes the queue contents and returns it to an empty state.
The free nodes list is also deallocated and returned to the heap.
*******************************************************************************/
{
list *ptr, *nxt;
ptr = First(q);
while (ptr != nullptr) {
nxt = Next(ptr);
free(ptr);
ptr = nxt;
}
ptr = free_nodes;
while (ptr != nullptr) {
nxt = Next(ptr);
free(ptr);
ptr = nxt;
}
q->contents = nullptr;
free_nodes = nullptr;
q->number = 0;
}
void FlushQueue(queue_type *q)
/******************************************************************************
Empties the queue and returns it to an empty state.
The freed contents are stored in the free nodes list for later use.
*******************************************************************************/
{
list *ptr, *nxt;
ptr = First(q);
while (ptr != nullptr) {
nxt = Next(ptr);
Free(ptr);
ptr = nxt;
}
q->contents = nullptr;
q->number = 0;
}
bool IsEmpty(queue_type *q)
/******************************************************************************
Returns TRUE if queue is empty, FALSE otherwise.
*******************************************************************************/
{
if (q->number == 0) {
return(true);
} else {
return(false);
}
}
long Number(queue_type *q)
/******************************************************************************
Returns the number in the queue.
*******************************************************************************/
{
return(q->number);
}
void Insert(queue_type *q, item_type v)
/******************************************************************************
Inserts the item 'v' into the queue so that the queue is sorted in
increasing order of 'v'. Thus the contents of this queue are removed
with smallest 'v' first.
*******************************************************************************/
{
list *ptr, *nxt;
list *new_entry = New();
new_entry->item = v;
if (q->contents == nullptr) {
q->contents = new_entry;
} else if (v <= q->contents->item) {
new_entry->next = q->contents;
q->contents = new_entry;
} else {
for (ptr = q->contents; ptr != nullptr; ptr = nxt) {
nxt = ptr->next;
if (nxt == nullptr) {
ptr->next = new_entry;
} else if (v <= nxt->item) {
new_entry->next = nxt;
ptr->next = new_entry;
break;
}
}
}
q->number++;
}
void Remove(queue_type *q, item_type *v)
/******************************************************************************
Removes the first entry in the queue and returns it in 'v'.
*******************************************************************************/
{
list *ptr = q->contents;
if (q->contents != nullptr) {
(*v) = q->contents->item;
q->contents = q->contents->next;
q->number--;
Free(ptr);
}
}
list *First(queue_type *q)
/******************************************************************************
Returns a pointer to the first entry in the queue.
*******************************************************************************/
{
return(q->contents);
}
list *Next(list *l)
/******************************************************************************
Returns a pointer to the entry following the 'l' entry pointer.
*******************************************************************************/
{
return(l->next);
}
#endif