forked from anuragsawle/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueUsingLinkedList.cpp
More file actions
172 lines (152 loc) · 4.4 KB
/
queueUsingLinkedList.cpp
File metadata and controls
172 lines (152 loc) · 4.4 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(){
data=-1;
next=NULL;
}
Node(int val){
data=val;
next=NULL;
}
};
class Queue{
public:
Node *head=NULL;
bool isEmpty(){
if(head==NULL){
return true;
}
return false;
}
bool isFull(){
Node *ptr=new Node;
if(ptr==NULL){
return true;
}
delete ptr;
return false;
}
void enqueue(int val){
if(isFull()){
cout<<"\n\nQueue Overflow." <<endl;
}
else{
if(isEmpty()){
head=new Node(val);
}
else{
Node *temp, *ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
temp=new Node(val);
ptr->next=temp;
}
cout<<"\n\nNode with value " <<val <<" is Enqueued into the Queue." <<endl;
}
}
int dequeue(){
if(isEmpty()){
cout<<"\n\nQueue Underflow." <<endl;
return -1;
}
else{
int val=head->data;
Node *ptr=head->next;
delete head;
head=ptr;
cout<<"\n\nNode with value " <<val <<" is Dequeued from the Queue." <<endl;
return val;
}
}
int count(){
if(isEmpty()){
cout<<"\n\nQueue Underflow." <<endl;
return -1;
}
else{
Node *ptr=head;
int counter=0;
while(ptr!=NULL){
counter++;
ptr=ptr->next;
}
cout<<"\n\nThere are " <<counter <<" Node in Queue." <<endl;
return counter;
}
}
void display(){
if(isEmpty()){
cout<<"\n\nQueue Underflow." <<endl;
}
else{
Node *ptr=head;
cout<<"\n\nDisplaying all the elements of the Queue: " <<endl;
while(ptr!=NULL){
cout<<"\n\t>> " <<ptr->data;
ptr=ptr->next;
}
}
}
};
int main(){
system("cls");
cout<<"********** Welcome to Queue Operation with Linked List **************" <<endl;
int op;
Queue ds;
cout<<"\n\nSelect an option to continue: " <<endl;
while(1){
cout<<"\n\n\n\nPress '0' to Exit()" <<endl
<<"\nPress '1' to check if Queue is Empty" <<endl
<<"\nPress '2' to check if Queue is Full" <<endl
<<"\nPress '3' to Enqueue an element" <<endl
<<"\nPress '4' to Dequeue an element" <<endl
<<"\nPress '5' to Display all the elements" <<endl
<<"\nPress '6' to get Count of all the elements" <<endl
<<"\n\n\t\t>> ";
cin>>op;
switch(op){
case 0:
cout<<"\n\nHope to see you again!" <<endl;
exit(0);
break;
case 1:
if(ds.isEmpty()){
cout<<"\n\nQueue is Empty \nYou reached the end of the World." <<endl;
}
else{
cout<<"\n\nQueue has some space Occupied." <<endl;
}
break;
case 2:
if(ds.isFull()){
cout<<"\n\nQueue is Completely Full" <<endl;
}
else{
cout<<"\n\nQueue has some space Vacant." <<endl;
}
break;
case 3:
cout<<"\nEnter the value to be Pushed into the Queue: ";
cin>>op;
ds.enqueue(op);
break;
case 4:
ds.dequeue();
break;
case 5:
ds.display();
break;
case 6:
ds.count();
break;
default:
cout<<"\n\nPlease enter a valid option 0-5" <<endl;
}
}
return 0;
}