-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathQueueUsingArray.cpp
55 lines (52 loc) Β· 1.29 KB
/
QueueUsingArray.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
#include<iostream>
using namespace std;
int queue[50], n=50, front=0, rear=-1;
void Insert(){
int item;
if(rear== n-1) //Condition of Queue Overflow
{cout<<"Queue Overflow"<<endl;}
else{
cout<<"Enter the item to insert: "<<endl;
cin>>item;
rear++;
queue[rear]=item;
}}
void Delete(){
if((front==0 && rear==-1) || front==rear+1){ //Condition of Queue underflow
cout<<"Queue Underflow ";
}
else{ cout<<"Item deleted from queue is : "<< queue[front]<<endl;
front++;
}
}
void Display(){int i;
if((front==0 && rear==-1) || front==rear+1)
{ cout<<"Queue is empty"<<endl; }
else{
cout<<"Items of the queue are : ";
for(i=front;i<=rear;i++)
{cout<<queue[i]<<" ";
cout<<endl;}
}
}
int main(){
int choice;
cout<<"1. Insert an element to queue"<<endl;
cout<<"2. Delete an element from queue"<<endl;
cout<<"3. Display all elements of queue"<<endl;
cout<<"4. Exit"<<endl;
do{cout<<"Enter choice: "<<endl;
cin>>choice;
switch(choice){
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default:cout<<"Enter valid choice"<<endl;
}
}while(choice!=4);
}