-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_88.cpp
135 lines (115 loc) · 2.78 KB
/
Day_88.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
/*
DAY 88: Queue using circular array.
QUESTION : Given size of a queue and Q query. The task is to perform operations according to the
type of query. Queries can be of following types:
1) 1 element: This means push the element into the queue (allowed only when queue is not full).
2) 2: This means pop the element at front from the queue (allowed only when queue is not empty).
Constraints:
1 <= size <= 10^4
1 <= Q <= 10^3
*/
#include <iostream>
using namespace std;
class Queue
{
int front ;
int rear;
public:
Queue()
{
front = -1;
rear = -1;
}
bool IsEmpty()
{
return (front == -1 && rear == -1);
}
bool IsFull(int size)
{
return ((rear+1) % size == front) ;
}
void Enqueue(int cq[], int size ,int x);
void Dequeue(int cq[], int size);
void Display(int cq[], int size);
};
void Queue :: Enqueue(int cq[], int size , int x)
{
cout<<"Enqueuing: "<<endl;
if (IsFull(size))
{
cout<<"Queue Full"<<endl;
}
else if (IsEmpty())
{
front = rear = 0;
cq[rear] = x;
}
else
{
rear = (rear + 1) % size;
cq[rear] = x;
}
}
void Queue :: Dequeue(int cq[], int size)
{
int item;
cout<<"Dequeuing: "<<endl;
if(IsEmpty())
{
cout<<"Queue is Empty"<<endl;
}
else if(front == rear)
{
item = cq[front];
front = rear = -1;
cout<<"Deleted Element is: "<<item;
}
else
{
item = cq[front];
front = (front + 1) % size;
cout<<"Deleted Element is: "<<item;
}
}
void Queue :: Display(int cq[], int size)
{
cout<<"\nElements: ";
for(int i= front; i != rear; i = (i+1)%size )
{
cout<<cq[i]<<endl;
}
cout<<cq[rear];
}
int main()
{
Queue Q;
int MAX, exit=0;
cout<<"Enter the Size of Queue: ";
cin>>MAX;
int A[MAX];
do{
cout<<"\n\n1) Display"<<endl;
cout<<"2) Add."<<endl;
cout<<"3) Delete."<<endl;
cout<<"4) Exit."<<endl;
int ch;
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{ case 1: Q.Display(A, MAX);
break;
case 2: int val;
cout<<"Enter the Element you want to insert: ";
cin>>val;
Q.Enqueue(A, MAX, val);
break;
case 3: int del;
Q.Dequeue(A, MAX);
break;
case 4: cout<<"\nExiting!!!";
exit = 1;
break;
default: cout<<"!!!\n\tInvalid Option!!!";
}
}while(exit != 1);
}