-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue using array.cpp
101 lines (94 loc) · 1.96 KB
/
queue using 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
101
#include <iostream>
using namespace std;
#define MAX 5
int arr[MAX];
int front=-1;
int rear=-1;
void enQueue ()
{
int item;
if (rear==MAX-1) //Queue is full
cout<<"Queue is Full."<<endl;
else if (front==-1 && rear==-1) //Queue is empty
{
front=0;
rear=0;
cout<<"Inset the element in queue : ";
cin>>item;
arr[rear]=item;
}
else
{
rear++;
cout<<"Inset the element in queue : ";
cin>>item;
arr[rear]=item;
}
}
void deQueue ()
{
if (front==-1 && rear==-1) //Queue is empty
cout<<"Queue is Empty."<<endl;
else if (front==rear) // on;y one item present
{
cout<<"Deleted item: "<<arr[front]<<endl;
front=-1;
rear=-1;
}
else
{
cout<<"Deleted item: "<<arr[front]<<endl;
front++;
}
}
void peek ()
{
if (front==-1 && rear==-1)
cout<<"Queue is Empty."<<endl;
else
cout<<"Peeked value: "<<arr[front]<<endl;
}
void display ()
{
int i;
if (front==-1 && rear==-1)
cout<<"Queue is Empty."<<endl;
else
{
cout<<"Queue is:"<<endl;
for (i=front;i<=rear;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
}
int main()
{
int choice;
do
{
cout<<"1.EnQueue"<<endl;
cout<<"2.DeQueue"<<endl;
cout<<"3.Peek"<<endl;
cout<<"4.Display"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1:
enQueue();
break;
case 2:
deQueue();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
cout<<"Wrong choice.\n";
}
}while (choice<5);
return 0;
}