-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.cpp
More file actions
84 lines (70 loc) · 1.5 KB
/
q1.cpp
File metadata and controls
84 lines (70 loc) · 1.5 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
#include <iostream>
#include <cstdlib>
#include <cstddef>
#include "q1.h"
using namespace std;
CustomerQueue::CustomerQueue(): front(NULL), back(NULL)
{
}
CustomerQueue::CustomerQueue(const CustomerQueue& q): front(NULL), back(NULL)
{
QueueNodePtr Ptr1 = q.front;
while (Ptr1 != NULL)
{
enter(Ptr1->ticketNum, Ptr1->time);
Ptr1 = Ptr1->link;
}
}
CustomerQueue::~CustomerQueue()
{
QueueNodePtr tempPtr = front;
while (tempPtr != NULL)
{
QueueNodePtr next = tempPtr->link;
delete tempPtr;
tempPtr = next;
}
}
bool CustomerQueue::emptyQueue() const
{
return (front == NULL);
}
void CustomerQueue::enter(int num, long enterTime)
{
if(emptyQueue())
{
front = new QueueNode;
front->ticketNum = num;
front->time = enterTime;
front->link = NULL;
back = front;
}
else
{
QueueNodePtr tempPtr;
tempPtr = new QueueNode;
tempPtr->ticketNum = num;
tempPtr->time = enterTime;
tempPtr->link = NULL;
back->link = tempPtr;
back = tempPtr;
}
}
void CustomerQueue::leave(int &customerNum, long &t)
{
if(emptyQueue())
{
cout << "ERROR: Cannot remove item from empty queue" << endl;
exit(1);
}
customerNum = front->ticketNum;
t = front->time;
QueueNodePtr leavePtr;
leavePtr = front;
front = front->link;
if(front == NULL)
{
back = NULL;
}
delete leavePtr;
}