-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueues_CircularArray.cpp
More file actions
146 lines (118 loc) · 2.32 KB
/
Copy pathQueues_CircularArray.cpp
File metadata and controls
146 lines (118 loc) · 2.32 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
/* Name : Annie Bhalla
Roll No. : 19HCS4009
Course : BSC (H) Computer Science
Semester : 3
Subject : Data Structures
Title : Implementation of Queues ( circular arrays )
*/
#include<iostream>
using namespace std;
typedef int Elem;
class Queue
{
int actual_len;
int n;
Elem *arr;
int front;
int rear;
public:
Queue(int N=0) // constructor
{
actual_len=N;
arr = new Elem[N];
front=-1;
rear=-1;
n=0;
}
int size() const; // 1
bool empty() const; // 2
const Elem& frontelement()const; // 3
void enqueue(const Elem& e); // 4
void dequeue(); // 5
void display(); // 6
};
// (1) to return the size of the queue
int Queue::size()const
{
return n;
}
// (2) to check whether queue is empty or not
bool Queue::empty()const
{
return (n==0);
}
// (3) to return the first element in the queue
const Elem& Queue::frontelement() const
{
if(empty()) throw " Function Front() : Empty Queue";
return arr[front];
}
// (4) enqueue
void Queue::enqueue(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;
n++;
}
// (5) dequeue
void Queue::dequeue()
{
if(empty())
{
front=-1;
rear=-1;
throw " Function dequeue() : Empty List , Cannot delete more elements";
}
front = (front+1)%actual_len;
n= n-1;
}
// (6) display queue
void Queue::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 num;
cout<<"\n Enter the size of the queue : ";
cin>>num;
try
{
Queue Q(num);
cout<<"\n is empty? "<<Q.empty()<<endl;
Q.enqueue(60);
Q.display();
Q.enqueue(70);
Q.enqueue(90);
Q.enqueue(100);
Q.enqueue(110);
Q.display();
Q.enqueue(190);
Q.display();
Q.display();
Q.dequeue();
Q.display();
Q.dequeue();
Q.dequeue();
Q.dequeue();
Q.display();
cout<<"\n NO. of elements "<<Q.size();
}
catch(const char* str)
{
cout<<"\n Exception Found at : "<<str;
}
return 0;
}