-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_queue.cpp
229 lines (197 loc) · 2.75 KB
/
link_queue.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include<iostream>
using namespace std;
//node class
class node
{
public:
int data;
node* next;
};
//queue class
class queue :public node
{
private:
node *front,*rear;
public:
queue() //default constructor
{
front =rear=0;
}
//function declarations
void insert(int );
void display();
int delet();
int isempty();
int isfull();
int first();
int last();
};
//first fun def
int queue:: first()
{
int x=-1;
if( isempty() )
cout<<"Empty Queue";
else
{
x=front->data;
}
return x;
}
//last fun def
int queue:: last()
{
int x=-1;
if( isempty() )
cout<<"Empty Queue";
else
{
node *temp=front;
while(temp->next)
{
temp=temp->next;
}
x=temp->data;
}
return x;
}
//isempty fun def
int queue::isempty()
{
if(front ==0 && rear==0)
{
return 1;
}
else
return 0;
}
//isfull fun def
int queue::isfull()
{
node *temp=new node;
if(temp == 0 )
{
return 1;
}
else
return 0;
}
///delete fun def
int queue::delet()
{
int x=-1;
if(front ==0 && rear==0)
{
cout<<"Empty Queue";
}
else
{
if(front == rear)
{
node *temp=front;
front=front->next;
rear=rear->next;
x=temp->data;
delete temp;
}
else
{
node *temp=front;
front=front->next;
x=temp->data;
delete temp;
}
}
cout<<"\nDelete :"<<x<<endl;
return x;
}
//display fun def
void queue::display()
{
cout<<" # The Queue is : ";
node *temp=front;
while(temp !=0)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
//insert fun def
void queue::insert(int x)
{
node *temp=new node;
if(temp ==0)
cout<<"Heap is full";
else
{
temp->data=x;
temp->next=0;
if(rear==0 && front==0)
{
front=rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
}
//main fun
main()
{
int n,x;
queue q;
do
{
cout<<"\n # Enter your choice of operation :\n 1)insert\n 2)display\n 3)delete\n 4)fiirst-element\n 5)last-element\n 6)exit\n\n choice : ";
cin>>n;
switch(n)
{
//insert
case 1:
cout<<"\n # How many elements to enter n = ";
cin>>n;
cout<<" # Enter the Data elements = ";
for(int i=0;i<n;i++)
{
cin>>x;
q.insert(x);
}
cout<<endl;
break;
//display
case 2:
q.display();
cout<<endl;
break;
//delete
case 3:
cout<<"\n # How many elements to delete : ";
cin>>n;
for(int i=0;i<n;i++)
{
q.delet();
}
cout<<endl;
break;
//first
case 4:
cout<<" \n # The First element is : "<<q.first();
cout<<endl;
break;
//last
case 5:
cout<<" \n # The Last element is : "<<q.last();
cout<<endl;
break;
//last
case 6:
exit(0);
break;
//last
default:
cout<<"Wrong choice";
}
}while(1);
}