-
Notifications
You must be signed in to change notification settings - Fork 4
/
circularqueue.cpp
94 lines (84 loc) · 1.4 KB
/
circularqueue.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
#include<iostream>
#define size 5
using namespace std;
int queue[size];
int front=-1, rear=-1;
//insert any element
void enqueue (int element)
{
if((rear+1)%size==front)
{
cout<<"\nQueue overflow"<<endl;
}
else if(front==-1 && rear==-1)
{
cout<<"\nyour element "<<element<<" is added into queue"<<endl;
front=rear=0;
queue[rear]=element;
}
else
{
cout<<"\nyour element"<<element<<" is added into queue."<<endl;
rear=(rear+1)%size;
queue[rear]=element;
}
}
//to delete any element
void dequeue (void)
{
if(front==-1 && rear==-1)
{
cout<<"\nQueue underflow"<<endl;
}
else if(front==rear)
{
cout<<"\nyour element "<<queue[front]<<" is deleted"<<endl;
front=rear=-1;
}
else
{
cout<<"\nyour element "<<queue[front]<<" is deleted"<<endl;
front=(front+1)%size;
}
}
//display elements of queue
void display (void)
{
int i;
cout<<"\ndisplay: ";
if(front==-1 && rear==-1)
{
cout<<"\nQueue is empty"<<endl;
}
else
{
for(i=front; i<=rear; i++)
{
cout<<"\t"<<queue[i];
}
}
}
//to see front element
void peek (void)
{
if(front==-1 && rear==-1)
{
cout<<"\nQueue is empty"<<endl;
}
else
{
cout<<"\nyour front element is "<<queue[front]<<endl;
}
}
int main()
{
enqueue(10);
enqueue(20);
enqueue(-5);
display();
peek();
dequeue();
peek();
display();
return 0;
}