-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircularQueueUsingLinkedList.cpp
More file actions
112 lines (109 loc) · 2.28 KB
/
CircularQueueUsingLinkedList.cpp
File metadata and controls
112 lines (109 loc) · 2.28 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
next = nullptr;
}
};
class CircularQueue {
private:
Node* front;
Node* rear;
int size;
int capacity;
public:
CircularQueue(int capacity) {
front = nullptr;
rear = nullptr;
size = 0;
this->capacity = capacity;
}
bool isFull() {
return size == capacity;
}
bool isEmpty() {
return size == 0;
}
void enqueue(int data) {
if (isFull()) {
cout << "Queue is full\n";
return;
}
Node* newNode = new Node(data);
if (isEmpty()) {
front = newNode;
}
else {
rear->next = newNode;
}
rear = newNode;
rear->next = front;
size++;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is empty\n";
return;
}
Node* temp = front;
if (front == rear) {
front = nullptr;
rear = nullptr;
}
else {
front = front->next;
rear->next = front;
}
delete temp;
size--;
}
void display() {
if (isEmpty()) {
cout << "Queue is empty\n";
return;
}
Node* temp = front;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != front);
cout << endl;
}
};
int main() {
int capacity;
cout << "Enter capacity of circular queue: ";
cin >> capacity;
CircularQueue q(capacity);
int choice, data;
while (true) {
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display\n";
cout << "4. Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter data to enqueue: ";
cin >> data;
q.enqueue(data);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
exit(0);
default:
cout << "Invalid choice" << '\n';
}
}
return 0;
}