-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleEndedQueue_CircleArray.cpp
More file actions
159 lines (134 loc) · 2.6 KB
/
Copy pathDoubleEndedQueue_CircleArray.cpp
File metadata and controls
159 lines (134 loc) · 2.6 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
/* Name : Annie Bhalla
Roll No. : 19HCS4009
Course : BSC (H) Computer Science
Semester : 3
Subject : Data Structures
Title : Implementation of Double Ended Queues ( Circular Array )
*/
#include<iostream>
using namespace std;
typedef int Elem;
class DQueue
{
Elem *arr;
int actual_len;
int front;
int rear;
int ctr;
public:
DQueue(int ); // 1
bool empty()const; // 2
int size()const; // 3
void addFront(const Elem& e); // 4
void addBack(const Elem& e); // 5
void eraseFront(); // 6
void eraseBack(); // 7
void display();
};
// (1) Constructor
DQueue::DQueue(int n=0)
{
actual_len=n;
arr= new Elem[n];
front=-1;
rear=-1;
ctr=0;
}
// (2) to check whether the DQ is empty
bool DQueue::empty()const
{
return (ctr==0);
}
// (3) to return the size of the queue
int DQueue::size()const
{
return ctr;
}
// (4) to add an element in the front
void DQueue::addFront(const Elem& e)
{
if(size()==actual_len) throw " Function enqueue() : Queue Full ";
if(rear==-1)
rear= actual_len-1;
if(front==-1 || front==0)
front = actual_len-1;
else
front= front-1;
arr[front] = e;
ctr++;
}
// (5) to add element at the back
void DQueue::addBack(const Elem& e)
{
if(size()==actual_len) throw " Function enqueue() : Queue Full ";
if(front==-1)
front=0;
rear= (rear+1)%actual_len;
arr[rear] = e;
ctr++;
}
// (6) to remove element from front
void DQueue::eraseFront()
{
if(empty())
{
front=-1;
rear=-1;
throw " Function dequeue() : Empty List , Cannot delete more elements";
}
front = (front+1)%actual_len;
ctr= ctr-1;
}
// (7) to remove element from back
void DQueue::eraseBack()
{
if(empty())
{
front=-1;
rear=-1;
throw " Function dequeue() : Empty List , Cannot delete more elements";
}
if(rear==0)
rear = actual_len-1;
rear--;
ctr=ctr-1;
}
// (8) display queue
void DQueue::display()
{
if(empty()) throw " Function Display() : Empty List ";
int i=front;
while( i!= rear)
{
cout<<arr[i]<<" -> ";
i = (i+1)% actual_len;
}
cout<<arr[rear];
cout<<endl;
}
// driver code
int main()
{
int N;
cout<<"\n Enter the size of the queue :";
cin>>N;
try
{
DQueue D(N);
D.addFront(5);
D.addBack(10);
D.addBack(15);
D.addBack(20);
D.display();
D.eraseFront();
D.display();
D.eraseBack();
D.display();
cout<<"\n Number of elements : "<<D.size();
}
catch(const char* str)
{
cout<<"\n Exception Found at : "<<str;
}
return 0;
}