-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplement_a_Queue.cpp
59 lines (53 loc) · 1.17 KB
/
Implement_a_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
class Queue
{
// Using Array
int *arr;
int qFront;
int qRear;
int size;
public:
Queue()
{
// Implement the Constructor
size = 10001;
arr = new int[size];
qFront = qRear = 0;
}
/*----------------- Public Functions of Queue -----------------*/
bool isEmpty()
{
// Implement the isEmpty() function
return qFront == qRear;
}
void enqueue(int data)
{
// Implement the enqueue() function
if (qRear == size)
return;
arr[qRear] = data;
qRear++;
// because we enqeue from the back
}
int dequeue()
{
// Implement the dequeue() function
if (qFront == qRear)
return -1;
else
{
int dequeued = arr[qFront]; // because queue if FIFO so the first elelemt will be dequeud
arr[qFront] = -1;
qFront++;
if (qFront == qRear)
qFront = qRear = 0;
return dequeued;
}
}
int front()
{
// Implement the front() function
if (qFront == qRear)
return -1;
return arr[qFront];
}
};