-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path64th_circular_queue.cpp
More file actions
103 lines (96 loc) · 1.68 KB
/
64th_circular_queue.cpp
File metadata and controls
103 lines (96 loc) · 1.68 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
//In this code I have tried to implement circular queue using array
#include<iostream>
using namespace std;
struct queue
{
int front;
int rear;
int size;
int *arr;
queue(int siz)
{
front=0;
rear=0;
size=siz+1;//as one space will remain vacant in queue
arr=new int[size];
}
};
int isEmpty(queue q)
{
if(q.front==q.rear)
return 1;
else
return 0;
}
int isFull(queue q)
{
if((q.rear+1)%q.size==q.front)
return 1;
else
return 0;
}
void enqueue(queue &q,int x)
{
if(!isFull(q))
{
q.rear=(q.rear+1)%q.size;
q.arr[q.rear]=x;
}
else
cout<<"\nQueue is FULL.\n";
}
void dequeue(queue &q)
{
int x;
if(!isEmpty(q))
{
q.front=(q.front+1)%q.size;
x=q.arr[q.front];
//cout<<x<<" ";
cout<<"\n"<<x<<" dequeued succesfully.\n";
}
else
cout<<"\nQueue is Empty.\n";
}
void display(queue q)
{
while(q.front!=q.rear)
{
q.front=(q.front+1)%q.size;
cout<<q.arr[q.front]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cout<<"Enter size of queue : ";
cin>>n;
queue q(n);
for(int i=1;i<=n;i++)
{
int x;
cout<<"Enter value to enqueue : ";
cin>>x;
enqueue(q,x);
}
while(1)
{
int ch;
cout<<"Press : ";
cout<<"1 to dequeue , and\n2 to enqueue : \nany other to exit\n: ";
cin>>ch;
if(ch==1)
dequeue(q);
else if(ch==2)
{
int x;
cout<<"Enter element to enqueue. : ";
cin>>x;
enqueue(q,x);
}
else
break;
display(q);
}
}