forked from sahilbansalweb/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue_using_Arrays.cpp
122 lines (117 loc) · 1.8 KB
/
Queue_using_Arrays.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
#include <iostream>
using namespace std;
// #define n 100
class queues
{
int q[100];
int f = -1;
int r = -1;
int n;
public:
void enqueue(int ele);
int dequeue();
void display();
bool isfull();
bool isempty();
int peek();
};
int main()
{
queues q;
int n;
cout << "Enter the number of elements to be inserted: ";
cin >> n;
for (int i = 0; i < n; i++)
{
q.enqueue(n);
}
q.display();
int del;
cout << "Enter the number of elements to be deleted: ";
cin >> del;
for (int i = 0; i < del; i++)
{
q.dequeue();
}
q.display();
// cout<<"The element at peek is: "<<q.peek();
}
void queues::enqueue(int size)
{
int ele;
cout << "Enter the element of the queue: ";
cin >> ele;
if (isfull())
{
cout << "Overflow" << endl;
return;
}
else
{
if (isempty())
{
f = 0;
}
r++;
q[r] = ele;
}
}
int queues::dequeue()
{
int temp;
if (isempty())
{
cout << "Underflow" << endl;
}
else
{
temp = q[f];
if (f >= r)
{
f = r = -1;
}
else
{
f++;
}
}
cout << "Deleted element is: " << temp<<endl;
}
void queues::display()
{
if (isempty())
{
cout << "underflow";
}
else
{
cout<<"The queue is: ";
for (int i = f; i <= r; i++)
{
cout << q[i] << " ";
}
cout << endl;
}
}
bool queues ::isempty()
{
if (f == -1 && r == -1)
{
return true;
}
else
return false;
}
bool queues::isfull()
{
if (r == n - 1)
{
return true;
}
else
return false;
}
int queues::peek()
{
return q[f];
}