-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_Array.cpp
More file actions
99 lines (88 loc) · 2.36 KB
/
Queue_Array.cpp
File metadata and controls
99 lines (88 loc) · 2.36 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
/******************************************************************************
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==size-1)
{
cout<<"OVERFLOW!"; // Overflow Condition
}
else if(front==-1 && rear==-1) // BY this We can easily define our underflow condition
{
front++; // Incrementing front
rear++; // Incrementing rare
arr[rear]=val; // Insert the value at 0th position
}
else
{
rear++; // Incrementing rare
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++;
}
}
void display()
{
if(front==-1 && rear==-1)
{
cout<<"UNDERFLOW!";
}
else
{
for(int i=front;i<=rear;i++)
{
cout<<" "<<arr[i];
}
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;
}