-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_Queue_Deque.cpp
More file actions
184 lines (163 loc) · 3.8 KB
/
a_Queue_Deque.cpp
File metadata and controls
184 lines (163 loc) · 3.8 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
177
178
179
180
181
182
183
184
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
//using array
class Deque{
private:
int capacity;
int *ptr;
int rear,front;
public:
Deque(){
capacity=0;
rear=-1;
front=-1;
ptr=NULL;
}
void CreateDeque(int cap){
capacity=cap;
ptr=new int[cap];
}
void Insertatrear(int data){
if(ptr==NULL){
cout<<"\n Array does not exists ! ";
}
else if(front==0 && rear+1==capacity || rear+1==front){
cout<<"\n Overflow ";
}
else if(rear==-1){ // for empty queue
rear=0;
ptr[rear]=data;
}
else{
rear++;
ptr[rear]=data;
}
}
void Insertatfront(int data){
if(ptr==NULL){
cout<<"\n Deque does not exists !";
}
else if(front==0 && rear+1==capacity || rear+1==front){
cout<<"\n Overflow !";
}
else if(rear==-1){
rear=0;
front=0;
ptr[front]=data;
}
else{
for(int i=rear;i>=front;i--){
ptr[i+1]=ptr[i];
}
ptr[front]=data;
rear++;
}
}
void deletionatfront(){
if(ptr==NULL){
cout<<"\n Deque does not exists ! ";
}
else if(front == rear){
front =-1;
rear=-1;
}
else if(front ==capacity-1){
front=0;
}
else{
front ++;
}
}
void deletionatrear(){
if(ptr==NULL){
cout<<"\n Deque does not exists ! ";
}
else if(front == rear){ // when only one element is present
front =-1;
rear=-1;
}
else if(rear==0){
rear=capacity-1;
}
else{
rear--;
}
}
int frontElementView(){
if(rear==-1){
cout<<"\n UnderFlow ! ";
}
return ptr[front];
}
int RearElementView(){
if(rear==-1){
cout<<"\n UnderFlow ! ";
}
return ptr[rear];
}
~Deque(){
while(front!=-1){
deletionatfront();
}
}
};
int driver(){
int choice;
cout<<"\n OPERATIONS : ";
cout<<"\n 1.Create a queue ";
cout<<"\n 2.Insertion from Rear ";
cout<<"\n 3. Deletion from front ";
cout<<"\n 4.Insertion from front ";
cout<<"\n 5. Deletion from Rear ";
cout<<"\n 6. To view Front Element in Deque";
cout<<"\n 7. To view Last Element in Deque";
cout<<"\n Press 0 for exit ";
cout<<"\n Please Enter your choice : ";
cin>>choice;
return choice;
}
int main(){
int capacity,data;
Deque obj;
while(1){
switch (driver())
{
case 1:
cout<<"\n Enter capacity : ";
cin>>capacity;
obj.CreateDeque(capacity);
break;
case 2:
cout<<"\n Enter data : ";
cin>>data;
obj.Insertatrear(data);
break;
case 3:
obj.deletionatfront();
break;
case 4:
cout<<"\n Enter data : ";
cin>>data;
obj.Insertatfront(data);
break;
case 5:
obj.deletionatrear();
break;
case 6:
cout<<obj.frontElementView();
break;
case 7:
cout<<obj.frontElementView();
break;
case 0:
exit(0);
break;
default:
cout<<"Please enter valid choice ,thank you !";
break;
}
}
return 0;
}