-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcircular_queue_implementation.cpp
More file actions
179 lines (125 loc) · 2.3 KB
/
circular_queue_implementation.cpp
File metadata and controls
179 lines (125 loc) · 2.3 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
#include <iostream>
#include <cstdlib>
#define SIZE 10
using namespace std;
class queue
/*
objective: Create a class to implement Queue(circular) using dynamically created array
input parameters: none
output value: none
description: Class definition
approach: Class definition provides data member and member functions for the Queue class
*/
{
int *arr; // array to store queue elements
int capacity; // maximum capacity of the Q
int front; // front points to front element in the Q
int rear; // rear points to last element in the Q
int count; // current size of the Q
public:
queue(int size = SIZE) // constructor
{
arr = new int[size];
capacity = size;
front = 0;
rear = 0;
count = 0;
}
~queue() //destructor
{
delete []arr;
}
void dequeue();
void enqueue(int);
int peek(); // returns front element
int size(); // returns current size of Q
bool isEmpty();
bool isFull();
};
void queue::dequeue()
{
if(count==0)
cout<<"\nQueue Underflow!";
else
{
front=(front+1)%capacity;
count-=1;
}
}
void queue::enqueue(int ele)
{
if(count==capacity)
{
cout<<"\nQueue Overflow!";
}
else
{
arr[rear]=ele;
rear=(rear+1)%capacity;
count+=1;
}
}
int queue::peek()
{
if(count==0)
{ cout<<"\nQueue is Empty...";
return 0;
}
else
return arr[++front];
}
int queue::size()
{
return count;
}
bool queue::isFull()
{
if(count==capacity)
return true;
else
return false;
}
bool queue::isEmpty()
{
if(count==0)
return true;
else
return false;
}
int main()
{
queue q;
int n,value;
cout<<"\n Press \n 1. Push\n 2. Pop\n 3. Peek \n 4. Size\n 5. Is Empty?\n 6. Is Full?\t";
cin>>n;
switch(n)
{
case 1:
cout<<"\nEnter the value to be inserted:\t";
cin>>value;
q.enqueue(value);
break;
case 2:
cout<<"\nThe value that is popped is: "<<q.peek();
q.dequeue();
break;
case 3:
cout<<"\nThe Value at the top of the stack is: "<<q.peek();
break;
case 4:
cout<<"\nThe size of the stack is: "<<q.size();
break;
case 5:
if(q.isEmpty()==true)
cout<<"\nThe stack is empty. ";
else
cout<<"\nThe stack if not empty. ";
break;
case 6:
if(q.isFull()==true)
cout<<"\nThe stack is full. ";
else
cout<<"\nThe stack if not full. ";
break;
}
}