forked from Ayu-hack/Hacktoberfest2024-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque_implementation.cpp
164 lines (118 loc) · 2.78 KB
/
deque_implementation.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
#include<bits/stdc++.h>
using namespace std;
struct Deque{
int size, cap;
int *arr;
Deque(int c){
cap = c;
size = 0;
arr = new int[cap];
}
// this function help us insert the element at the start of the deque
void insertFront(int x){
if(isFull()){
cout<<"Deque is already Full!"<<endl;
return;
}
else{
for(int i = size-1; i >= 0; i--){
arr[i+1] = arr[i];
}
arr[0] = x;
size++;
}
}
// this function would help us insert the element at the end of the deque
void insertRear(int x){
if(isFull()){
cout<<"Deque is already Full!"<<endl;
return;
}
else{
arr[size] = x;
size++;
}
}
// this function would remove the first element of the deque upon calling
void deleteFront(){
if(isEmpty()){
cout<<"Deque is already empty!"<<endl;
return;
}
else{
for(int i = 0; i < size-1; i++){
arr[i] = arr[i+1];
}
size--;
}
}
// this function would delete the last element of the deque on calling
void deleteRear(){
if(isEmpty()){
cout<<"Deque is already empty!"<<endl;
return;
}
else{
size--;
}
}
// This function would tell if the deque is full or not
bool isFull(){
return (size == cap);
}
// This function would tell us weather the deque is empty or not
bool isEmpty(){
return (size == 0);
}
// this function would give us the first element of the deque
int front(){
if(isEmpty()){
return -1;
}
else{
return arr[0];
}
}
// This function would return us the last element of the deque
int back(){
if(isEmpty()){
return -1;
}
else{
return arr[size-1];
}
}
// this function would print the whole deque
void Print(){
if(isEmpty()){
cout<<"Deque is already empty!"<<endl;
}
else{
for(int i = 0; i < size; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
}
// this function would return us the size of the deque
int getSize(){
return size;
}
};
int main(){
struct Deque d(10);
d.insertFront(23);
d.insertFront(39);
d.insertRear(56);
d.insertRear(91);
d.Print();
cout<<d.getSize()<<endl;
d.deleteFront();
d.deleteRear();
d.Print();
cout<<d.getSize()<<endl;
d.deleteFront();
d.Print();
d.deleteFront();
d.Print();
}