-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular_Queue_Array.cpp
100 lines (89 loc) · 2.45 KB
/
Circular_Queue_Array.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
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<iostream>
using namespace std;
#define size 4 // Macro representation
int arr[size];
int front=-1,rear=-1; // Initializing two variables with -1 index
void enqueue(int val)
{
if((rear+1) % size == front) // Overflow Condition
{
cout<<"OVERFLOW!";
}
else if(front==-1 && rear==-1) // If you are inserting the very first element
{
front++; // Incrementing front
rear++; // Incrementing rare
arr[rear]=val; // Insert the value at 0th position
}
else
{
rear = (rear+1) % size; // Incrementing rare with taking modulus
arr[rear]=val; // Insert the value at 0th position
}
}
void dequeue()
{
if(front==-1 && rear==-1)
{
cout<<"UNDERFLOW!";
return ;
}
else if(front == rear)
{
cout<<"The deleted element is : "<<arr[front];
front =-1;
rear =-1;
}
else
{
cout<<"The deleted element is : "<<arr[front];
front = (front+1) % size;
}
}
void display()
{
if(front==-1 && rear==-1)
{
cout<<"UNDERFLOW!";
}
else
{
for(int i=front;i!=rear;i=(i+1)%size)
{
cout<<" "<<arr[i];
}
cout<<" "<<arr[rear];
cout<<"\n";
}
}
int main()
{
int choice,val;
cout<<" MENU DRIVEN PROGRAM ";
cout<<"\n 1.Enqueue\n 2.Dequeue\n 3.Display\n 4.Exit";
while(1)
{
cout<<"\n Enter the choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter the value to be inserted: ";
cin>>val;
enqueue(val);
break;
case 2: dequeue();
break;
case 3: cout<<"Queue Elements : ";
display();
break;
case 4: exit(0);
default: cout<<"\n Invalid choice!.........";
}
}
return 0;
}