-
Notifications
You must be signed in to change notification settings - Fork 18
/
Queue_LL-Imp.cpp
150 lines (125 loc) · 2.59 KB
/
Queue_LL-Imp.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
using namespace std;
struct node{
int val;
node* next;
node(int x){
val = x;
next = NULL;
}
};
node *front = NULL;
node *rear = NULL;
int qsize = 0;
bool isEmpty(){
return front == NULL;
}
void enqueue(int val){
node* temp;
temp = new node(val);
qsize++;
cout << val << " enqueued!" << endl;
if (isEmpty()){
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
void dequeue(){
if (isEmpty()){
cout << "Queue underflow!" << endl;
return;
}
node* temp = front;
int data = temp->val;
front = front -> next;
if (front == NULL){
rear = NULL;
}
qsize--;
cout << data << " dequeued!" << endl;
}
void frontElement(){
if (isEmpty()){
cout << "Queue underflow!" << endl;
return;
}
cout << "Front: " << front->val << endl;
}
void rearElement(){
if (isEmpty()){
cout << "Queue underflow!" << endl;
return;
}
cout << "Rear: " << rear->val << endl;
}
void queueSize(){
cout << "Size: " << qsize << endl;
}
void display(){
cout << "Queue: ";
node* temp = front;
if (temp == NULL){
cout << "NULL!" << endl;
return;
}
while(temp){
int data = temp->val;
cout << data << " ";
temp = temp->next;
}
cout << endl;
}
int main(){
int q = 1;
while(q != 7){
cout << endl;
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Front" << endl;
cout << "4. Rear" << endl;
cout << "5. Size" << endl;
cout << "6. Display" << endl;
cout << "7. Exit" << endl;
cout << "Enter your query: ";
cin >> q;
switch(q){
case 1:{
int val;
cout << "Enter the value: ";
cin >> val;
enqueue(val);
break;
}
case 2:{
dequeue();
break;
}
case 3:{
frontElement();
break;
}
case 4:{
rearElement();
break;
}
case 5:{
queueSize();
break;
}
case 6:{
display();
break;
}
case 7:{
exit(0);
break;
}
default:{
cout << "Enter a valid operation!" << endl;
}
}
}
return 0;
}