forked from asaurav22/DSA_Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
100 lines (91 loc) · 1.48 KB
/
queue.cpp
File metadata and controls
100 lines (91 loc) · 1.48 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
100
// ARRAY IMPLEMENTATION OF QUEUES //
#include <bits/stdc++.h>
using namespace std;
#define n 100
/*
Operations :
1. Enqueue(x)
2. Dequeue;
3.peek()
4.empty()
*/
class Queue
{
int *arr;
int front, back;
public:
Queue()
{
arr = new int[n];
front = -1;
back = -1;
}
void Enqueue(int x)
{
if (back == n - 1)
{
cout << "\nQueue is full\n";
return;
}
back++;
arr[back] = x;
if (front = -1)
{
front++;
}
}
void Dequeue()
{
if (front == -1)
{
cout << "\nQueue is empty\n";
return;
}
else if (front > back)
{
cout << "\nQueue is empty\n";
return;
}
front++;
}
int peek()
{
if (front > back || front == -1)
{
return -1;
}
return arr[front];
}
bool Empty()
{
if (front > back || front == -1)
{
return true;
}
return false;
}
};
int main()
{
Queue q;
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);
q.Enqueue(6);
q.Enqueue(7);
cout<<q.peek()<<endl;
q.Dequeue();
q.Dequeue();
cout<<q.peek()<<endl;
q.Dequeue();
q.Dequeue();
q.Dequeue();
q.Dequeue();
q.Dequeue();
cout<<q.peek()<<endl;
cout<<q.Empty()<<endl;
q.Dequeue();
return 0;
}