-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion and deletion in DEQ.cpp
More file actions
135 lines (135 loc) · 2.12 KB
/
insertion and deletion in DEQ.cpp
File metadata and controls
135 lines (135 loc) · 2.12 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
#include<iostream>
using namespace std;
class CLASS{
private:
int F,R,size,DEQ[];
public:
CLASS();
int InsertFront(int);
int InsertRear(int);
int DeleteFront();
int DeleteRear();
int display();
};
CLASS::CLASS(){
F=R=-1;
cout<<"Enter Size :\n";
cin>>size;
}
int CLASS::InsertFront(int n){
if((F==0 && R==size-1) || (F==R+1)){
cout<<"OVERFLOW\n";
return 0;
}
if(F==-1){
F=R=0;
}
else if(F==0 && R!=size-1){
F=size-1;
}
else{
F--;
}
DEQ[F]=n;
return 0;
}
int CLASS::InsertRear(int n){
if((F==0 && R==size-1) || (F==R+1)){
cout<<"OVERFLOW\n";
return 0;
}
if(F==-1){
F=R=0;
}
else if(R==size-1 && F!=0){
R=0;
}
else{
R++;
}
DEQ[R]=n;
return 0;
}
int CLASS::DeleteFront(){
int deletedItem;
if(F==-1){
cout<<"UNDERFLOW\n";
return 0;
}
deletedItem=DEQ[F];
if(F==R){
F=R=-1;
}
else if(F==size-1){
F=0;
}
else{
F++;
}
return 0;
}
int CLASS::DeleteRear(){
int deletedItem;
if(F==-1){
cout<<"UNDERFLOW\n";
return 0;
}
deletedItem=DEQ[R];
if(F==R){
F=R=-1;
}
else if(R==0){
R=size-1;
}
else{
R--;
}
return 0;
}
int CLASS::display(){
if(F==-1){
cout<<"DEQ is Empty. \n";
return 0;
}
else{
for(int i=F; i<=R; i++){
cout<<DEQ[i]<<"\t";
}
}
return 0;
}
int main(){
CLASS obj;
int choice,num;
while(choice!=5){
cout<<"\nEnter 1 for Front Insertion :";
cout<<"\nEnter 2 for Rear Insertion :";
cout<<"\nEnter 3 for Front Deletion :";
cout<<"\nEnter 4 for Rear Deletion :";
cout<<"\nEnter 5 to Exit :";
cin>>choice;
switch(choice){
case 1:
cout<<"\nEnter value to Insert :";
cin>>num;
obj.InsertFront(num);
break;
case 2:
cout<<"\nEnter value to Insert :";
cin>>num;
obj.InsertRear(num);
break;
case 3:
obj.DeleteFront();
cout<<"\nDEQ after Front Deletion :";
obj.display();
break;
case 4:
obj.DeleteRear();
cout<<"\nDEQ after Rear Deletion :";
obj.display();
break;
}
}
return 0;
}