forked from namishkhanna/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.cpp
More file actions
135 lines (122 loc) · 2.35 KB
/
Copy pathLinkedQueue.cpp
File metadata and controls
135 lines (122 loc) · 2.35 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
/*
C++ program : Queue implementation using linked list
*/
#include<bits/stdc++.h>
#include<conio.h> //for getch()
using namespace std;
struct node
{
int data;
node *next;
};
class lqueue
{
node *f, *r;
public:
void insertq(int);
void deleteq();
void displayq();
lqueue()
{
f = NULL;
r = NULL;
}
~lqueue();
};
void lqueue::insertq(int i)
{
node *ptr = NULL;
ptr = new node;
if(ptr == NULL)
{
cout<<"\nOverflow error";
return;
}
cout<<"\tEnter the value#"<<i+1<<": ";
cin>>ptr->data;
ptr->next = NULL;
if(f==NULL && r==NULL)
{
f = ptr;
r = ptr;
}
else
{
r->next = ptr;
r = ptr;
}
}
void lqueue::deleteq()
{
node *ptr=NULL;
if(f == NULL)
cout<<"\n\tUnderflow error";
else
{
ptr=f;
f = f->next;
delete ptr;
}
}
void lqueue::displayq()
{
if(f == NULL)
{
cout<<"\n\tThe queue is empty.";
return;
}
cout<<"\n\t";
node *ptr = f;
while(ptr != NULL)
{
cout<<ptr->data<<"-->";
ptr=ptr->next;
}
cout<<"NULL";
}
lqueue::~lqueue()
{
node *ptr=f;
while(ptr!=NULL)
{
f = f->next;
delete ptr;
ptr = f;
}
}
int main()
{
lqueue q;
int ch, n;
while(ch!=4)
{
system("cls"); //clears the screen
cout<<"\t\t**********MENU**********\n\tBasic Queue Operations:";
cout<<"\n\t1.Insert a node";
cout<<"\n\t2.Delete a node";
cout<<"\n\t3.Display queue";
cout<<"\n\t4.Exit";
cout<<"\n\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n\tHow many nodes do you want to enter: ";
cin>>n;
cout<<endl;
for(int i=0; i<n; i++)
q.insertq(i);
break;
case 2: cout<<"\n\tAfter deletion, the queue:\n";
q.deleteq();
q.displayq();
break;
case 3: cout<<"\n\tQueue contains:\n";
q.displayq();
break;
case 4: exit(0);
}
cout<<"\nPress any key to continue...";
getch();
}
return 0;
}