-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.cc
99 lines (83 loc) · 1.37 KB
/
queue.cc
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
#include "queue.h"
queue::queue()
{
first = last = 0;
}
void
queue::add( pitcher *new_entry )
{
if ( this->last ) {
item *temp = new item;
temp->entry = new_entry;
temp->next = 0;
this->last->next = temp;
this->last = temp;
}
else {
item *temp = new item;
temp->entry = new_entry;
temp->next = 0;
this->last = this->first = temp;
}
}
pitcher*
queue::dequeue()
{
if ( queue_empty() ) {
fprintf( stderr, "attempting to dequeue non-existent runner!\n" );
exit(1);
}
if ( this->first == 0 )
return(0);
else {
item *temp1 = this->first;
pitcher *temp2 = this->first->entry;
if ( this->first == this->last )
this->first = this->last = 0;
else
this->first = this->first->next;
delete(temp1);
return(temp2);
}
}
void
queue::dump()
{
item *curr = this->first;
if ( curr ) {
fprintf( stderr, "queue: " );
while ( curr ) {
fprintf( stderr, "%s ", curr->entry->nout() );
curr = curr->next;
}
fprintf( stderr, "\n" );
}
else {
fprintf( stderr, "queue:\n" );
}
}
int
queue::queue_empty()
{
if ( this->first == 0 )
return(1);
else
return(0);
}
void
queue::clear()
{
item *curr,
*prev;
curr = prev = this->first;
while( curr ) {
curr = prev->next;
delete(prev);
prev = curr;
}
this->first = this->last = 0;
}
queue::~queue()
{
clear();
}