-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
111 lines (101 loc) · 1.6 KB
/
Queue.cpp
File metadata and controls
111 lines (101 loc) · 1.6 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
101
102
103
104
105
106
107
108
109
110
111
#include<iostream>
#define Max 10
using namespace std;
class Queue
{
int Front, Rear,Q[Max];
public:
Queue();
bool isFull();
bool isEmpty();
void enqueue(int n);
int dequeue();
void display();
};
Queue::Queue()
{
Front = -1;
Rear = -1;
}
bool Queue::isFull()
{
if(Rear == Max-1)
{
return true;
}
else
{
return false;
}
}
bool Queue::isEmpty()
{
if(Front == -1 && Rear == -1)
{
return true;
}
else
{
return false;
}
}
void Queue::enqueue(int n)
{
if(isFull())
{
cout<<"Queue is Full ";
return ;
}
else if (Rear == -1)
{
Front = Rear = 0;
Q[Rear]=n;
}
else
{
Rear = Rear + 1;
Q[Rear] = n;
}
}
int Queue::dequeue()
{
if(isEmpty())
{
cout<<"Queue is Empty ";
return 0;
}
else if (Front == Rear)
{
Front = Rear = -1;
}
else
{
cout<<"\nElement Deleted from Queue is "<<Q[Front]<<endl;
Front= Front + 1;
}
}
void Queue::display()
{
if (Front == - 1)
{
cout<<"Queue is empty"<<endl;
}
else
{
cout<<"Queue Elements are: ";
for(int i = Front; i <= Rear; i++)
{
cout<<Q[i]<<" ";
}
}
}
int main()
{
Queue obj;
obj.enqueue(10);
obj.enqueue(20);
obj.enqueue(30);
obj.display();
obj.dequeue();
obj.display();
}