-
Notifications
You must be signed in to change notification settings - Fork 156
/
dequeue.cpp
127 lines (126 loc) · 2.73 KB
/
dequeue.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
/*
insert_at_beg(): inserts an item at the front of Dequeue.
insert_at_end(): inserts an item at the rear of Dequeue.
delete_fr_beg(): Deletes an item from front of Dequeue.
delete_fr_rear(): Deletes an item from rear of Dequeue.
*/
#include<iostream>
using namespace std;
#define SIZE 10
class dequeue {
int a[20],f,r;
public:
dequeue();
void insert_at_beg(int);
void insert_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void show();
};
dequeue::dequeue() {
f=-1;
r=-1;
}
void dequeue::insert_at_end(int i) {
if(r>=SIZE-1) {
cout<<"\n insertion is not possible, overflow!!!!";
} else {
if(f==-1) {
f++;
r++;
} else {
r=r+1;
}
a[r]=i;
cout<<"\nInserted item is"<<a[r];
}
}
void dequeue::insert_at_beg(int i) {
if(f==-1) {
f=0;
a[++r]=i;
cout<<"\n inserted element is:"<<i;
} else if(f!=0) {
a[--f]=i;
cout<<"\n inserted element is:"<<i;
} else {
cout<<"\n insertion is not possible, overflow!!!";
}
}
void dequeue::delete_fr_front() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty";
return;
}
else {
cout<<"the deleted element is:"<<a[f];
if(f==r) {
f=r=-1;
return;
} else
f=f+1;
}
}
void dequeue::delete_fr_rear() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty";
return;
}
else {
cout<<"the deleted element is:"<<a[r];
if(f==r) {
f=r=-1;
} else
r=r-1;
}
}
void dequeue::show() {
if(f==-1) {
cout<<"Dequeue is empty";
} else {
for(int i=f;i<=r;i++) {
cout<<a[i]<<" ";
}
}
}
int main() {
int c,i;
dequeue d;
Do//perform switch opeartion {
cout<<"\n 1.insert at beginning";
cout<<"\n 2.insert at end";
cout<<"\n 3.show";
cout<<"\n 4.deletion from front";
cout<<"\n 5.deletion from rear";
cout<<"\n 6.exit";
cout<<"\n enter your choice:";
cin>>c;
switch(c) {
case 1:
cout<<"enter the element to be inserted";
cin>>i;
d.insert_at_beg(i);
break;
case 2:
cout<<"enter the element to be inserted";
cin>>i;
d.insert_at_end(i);
break;
case 3:
d.show();
break;
case 4:
d.delete_fr_front();
break;
case 5:
d.delete_fr_rear();
break;
case 6:
exit(1);
break;
default:
cout<<"invalid choice";
break;
}
} while(c!=7);
}