-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic-queue.cpp
101 lines (86 loc) · 1.61 KB
/
static-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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// circular static queue
#include <iostream>
using namespace std;
const int capacity = 10;
class Node {
int data;
Node* next;
};
class Queue {
private:
int arr[capacity], forward, rear, size;
public:
Queue() {
forward = -1;
rear = -1;
size = 0;
}
void Insert(int value);
void Delete();
bool isEmpty();
bool isFull();
void print();
};
void Queue::Insert(int value) {
if (isFull()) {
return;
}
else {
forward++;
forward = forward % capacity;
arr[forward] = value;
}
}
void Queue::Delete() {
if (isEmpty()) {
return;
}
else {
rear++;
rear = rear % capacity;
}
}
bool Queue::isEmpty() {
if (rear == -1 && forward == -1)
return true;
return false;
}
bool Queue::isFull() {
if ((forward + 1) % capacity == rear % capacity)
return true;
return false;
}
void Queue::print() {
for (int i = forward; i > rear; i--) {
if (i == -1) {
i = capacity - 1;
continue;
}
cout << arr[i] << endl;
}
}
int main() {
Queue q;
if (q.isEmpty())
cout << "Queue is empty" << endl;
else
cout << "Queue is not empty " << endl;
q.Insert(10);
q.Insert(20);
q.Insert(30);
q.Delete();
q.Insert(40);
q.Insert(50);
q.Insert(60);
q.Insert(70);
q.Insert(80);
q.Insert(90);
q.Insert(100);
q.Insert(110);
q.print();
if (q.isFull())
cout << "Queue is full" << endl;
else
cout << "Queue is not full " << endl;
return 0;
}