-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
77 lines (69 loc) · 1 KB
/
queue.cpp
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
// queue.cpp -- Queue and Customer methods
#include "queue.h"
#include <cstdlib>
Queue::Queue( int qs ): qsize(qs)
{
front = rear = NULL;
items = 0;
}
Queue::~Queue()
{
Node * temp;
while (front != NULL) {
temp = front;
front = front->next;
delete temp;
}
}
bool Queue::isempty() const
{
return items == 0;
}
bool Queue::isfull() const
{
return items == qsize;
}
int Queue::queuecount() const
{
return items;
}
bool Queue::enqueue (const Item & item)
{
if (isfull()) {
return false;
}
Node * add = new Node;
if (add == NULL) {
return false;
}
add->item = item;
add->next = NULL;
items++;
if (front == NULL) {
front = add;
} else {
rear->next = add;
}
rear = add;
return true;
}
bool Queue::dequeue ( Item & item )
{
if (front == NULL) {
return false;
}
item = front->item;
items--;
Node * temp = front;
front = front->next;
delete temp;
if (items == 0) {
rear = NULL;
}
return true;
}
void Customer::set (long when)
{
processtime = std::rand()%3 + 1;
arrive = when;
}