-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue_implementation.cpp
147 lines (128 loc) · 2.09 KB
/
Queue_implementation.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
using namespace std;
#define size 100
int queue[size];
int front = -1;
int rear = -1;
bool isFull()
{
if (rear == size - 1)
{
return true;
}
else
{
return false;
}
}
bool isEmpty()
{
if (front == 1 && rear == 1)
{
return true;
}
else
{
return false;
}
}
void Enqueue(int value)
{
if (isFull())
{
cout << "Queue is Full!!" << endl;
}
else
{
if (front == -1)
{
front = 0;
}
rear++;
queue[rear] = value;
}
}
int Dequeue()
{
int val;
if (isEmpty())
{
cout << "Queue is Empty!!" << endl;
}
else
val = queue[front];
front++;
return val;
}
void peek()
{
if (isEmpty())
{
cout << "Queue is Empty!!" << endl;
return;
}
int item = queue[front];
cout << "Peek Element is " << item << endl;
int item1 = queue[rear];
cout << "Last Element is " << item1 << endl;
}
void display()
{
if (isEmpty())
{
cout << "Queue is Empty!!" << endl;
}
else
{
for (int i = front; i <= rear; i++)
{
cout << queue[i] << " ";
}
}
}
int main()
{
int choice;
do
{
cout << "\nEnter the Choice of Operation : " << endl;
cin >> choice;
switch (choice)
{
case 1:
{
int push_ele;
cout << "Enter the Value to be inserted : " << endl;
cin >> push_ele;
Enqueue(push_ele);
break;
}
case 2:
{
cout << "Element is Deleted : " << Dequeue() << endl;
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
case 5:
{
exit(0);
break;
}
default:
{
cout << "Enter the Valid Choice !!" << endl;
break;
}
}
} while (choice != 5);
return 0;
}