-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue_usingArray.cpp
92 lines (89 loc) · 1.62 KB
/
Queue_usingArray.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Implement a class Queue Using Array as a circular list
Implement the following functions
1. IsFull:
2. IsEmpty:
3. Enqueue: Add an element to the queue.
4. Dequeue: Removes the front element from the queue.
5. Print: It will print all elements of the queue in FIFO order
*/
#include<iostream>
using namespace std;
class Queue {
int* arrQueue = 0;
int maxSize, currSize, front, rear;
public:
Queue(int maxsize = 10) {
maxSize = maxsize;
currSize = 0;
front = -1;
rear = -1;
arrQueue = new int[10];
}
~Queue() {
if (arrQueue != 0) {
delete[]arrQueue;
arrQueue = 0;
}
}
bool isFull() {
if (currSize == maxSize)
return true;
return false;
}
bool isEmpty() {
if (currSize == 0)
return true;
return false;
}
bool enQueue(int val) {
if (isFull())
return false;
else {
rear = (rear + 1) % maxSize;
arrQueue[rear] = val;
currSize++;
return true;
}
}
bool deQueue(int& val) {
if (isEmpty())
return false;
else {
front = (front + 1) % maxSize;
val = arrQueue[front];
currSize--;
return true;
}
}
void display() {
int j = front;
for (int i = 0; i < currSize; i++) {
j = (j + 1) % maxSize;
cout << arrQueue[j] << ",";
}
}
};
int main() {
int value;
Queue Q;
Q.enQueue(9);
Q.enQueue(8);
Q.enQueue(7);
Q.deQueue(value);
Q.enQueue(2);
Q.deQueue(value);
Q.enQueue(3);
Q.deQueue(value);
Q.enQueue(4);
Q.deQueue(value);
Q.enQueue(5);
Q.deQueue(value);
Q.deQueue(value);
Q.enQueue(50);
Q.enQueue(2);
Q.enQueue(10);
Q.display();
system("pause>nul");
return 0;
}