-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinkedQueue.cpp
More file actions
176 lines (164 loc) · 2.17 KB
/
Copy pathlinkedQueue.cpp
File metadata and controls
176 lines (164 loc) · 2.17 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
173
174
175
176
#include<iostream>
using namespace std;
template <class T>
class Queue
{
public:
class Node
{
public:
T info;
Node *next;
}*f,*r,*p;
Queue()
{
f=r=p=NULL;
}
void push(T);
bool insertAfter(T,T);
bool insertBefore(T,T);
void display()
{
T t;
p=f;
while(p!=NULL)
{
t=p->info;
p=p->next;
cout<<t<<" ";
}
}
T pop()
{
T t;
p=f;
t=f->info;
f=f->next;
delete p;
return t;
}
bool isEmpty()
{
if(f==NULL)
{
return 1;
}
else
{
return 0;
}
}
};
template <class T>
bool Queue<T>::insertAfter(T target,T in)
{
p=f;
while(p!=NULL)
{
if(p->info==target)
{
break;
}
p=p->next;
}
if(p==NULL)
{
return 0;
}
Node *t=new Node;
t->info=in;
t->next=p->next;
p->next=t;
return true;
}
template <class T>
void Queue<T>::push(T el)
{
p=new Node;
p->info=el;
p->next=NULL;
if(f==NULL)
{
f=r=p;
r->next=NULL;
}
else
{
r->next=p;
r=p;
}
}
int main()
{
Queue<int> ob;
char ch,c;
int e1,target,in;
do{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. isEmpty";
cout<<"\n4. Display";
cout<<"\n5. Insert After";
cout<<"\nEnter Your Choice :";
cin>>ch;
switch(ch)
{
case '1':
{
cout<<"\n Enter Element to push : ";
cin>>e1;
ob.push(e1);
break;
}
case '2':
{
if(ob.isEmpty())
{
cout<<"\n The Queue is Empty ";
}
else
{
e1=ob.pop();
cout<<"\n Element poped is : "<<e1;
}
break;
}
case '3':
{
if(ob.isEmpty())
{
cout<<"\n Queue is Empty";
}
else
{
cout<<"\n Queue is not Empty";
}
break;
}
case '4':
{
ob.display();
break;
}
case '5':
{
cout<<"\n Enter the target value: ";
cin>>target;
cout<<"\n Enter the Element to enter :";
cin>>in;
if(ob.insertAfter(target, in))
{
cout<<"\n Element Entered at its position";
}
else
{
cout<<"\n Target Element not Found";
}
break;
}
default: cout<<"\n Wrong Choice";
}
cout<<"\nGoto Menu Again(y/n)? ";
cin>>c;
}while(c=='y' || c=='Y');
}