forked from nazirmohd2006/NewRepository
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueueBYLinkedList.cpp
More file actions
98 lines (89 loc) · 2.41 KB
/
QueueBYLinkedList.cpp
File metadata and controls
98 lines (89 loc) · 2.41 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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
};
class QueueBYLinkedList{
public:
Node *front, *rear;
QueueBYLinkedList(){
front = rear = NULL;
}
Node* createNode(int num){
Node *temp = new Node;
if(temp == NULL){
cout<<"Node creation failed"<<endl;
}
else{
temp->data = num;
temp->next = NULL;
}
return temp;
}
void addRequest(){
Node *temp;
int num;
cout<<"Enter any number"<<endl;
cin>>num;
temp = createNode(num);
if(rear == NULL && front == NULL){
rear = front = temp;
cout<<"First element inserted in queue"<<endl;
}
else{
rear->next = temp;
rear = temp;
cout<<rear->data<<" element inserted"<<endl;
}
}
void processRequest(){
Node *current;
if(front == NULL){
cout<<"Queue is underflow"<<endl;
return;
}
current = front;
front = front->next;
cout<<current->data<<" deleted from queue"<<endl;
delete(current);
}
void display(){
Node *temp;
if(rear == NULL && front == NULL){
cout<<"Queue underflow"<<endl;
return;
}
temp = front;
cout<<"FRONT <- ";
while(temp != NULL){
cout<<temp->data<<" -> ";
temp = temp->next;
}
cout<<"REAR"<<endl;
}
};
int main(){
QueueBYLinkedList qbl;
int opn;
char choice;
do{
cout<<"1. Add\n2. Delete\n3. Display"<<endl;
cout<<"Enter your choice"<<endl;
cin>>opn;
switch(opn){
case 1:
qbl.addRequest();
break;
case 2:
qbl.processRequest();
break;
case 3:
qbl.display();
break;
}
cout<<"Do you want to continue(y/n)"<<endl;
cin>>choice;
}while(choice == 'y' || choice == 'Y');
}