-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue_array_cpp.cpp
More file actions
82 lines (71 loc) · 1.13 KB
/
Copy pathqueue_array_cpp.cpp
File metadata and controls
82 lines (71 loc) · 1.13 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
#include <iostream>
using namespace std;
class Queue
{
private:
int Size;
int Front;
int Rear;
int *Q;
public:
Queue() // default
{
Front = Rear = -1;
Size = 10;
Q = new int[Size];
}
Queue(int size) // parameterized
{
Front = Rear = -1;
Size = size;
Q = new int[Size];
}
void Enqueue(int x);
int Dequeue();
void Display();
};
void Queue ::Enqueue(int x)
{
if (Rear == Size - 1)
printf("Queue is Full\n");
else
{
Rear++;
Q[Rear] = x;
}
}
int Queue ::Dequeue()
{
int x = -1;
if (Rear == Front)
printf("Queue is Empty\n");
else
{
Front++;
x = Q[Front + 1];
}
return x;
}
void Queue ::Display()
{
for (int i = Front + 1; i <= Rear; i++)
printf("%d ", Q[i]);
printf("\n");
}
int main()
{
Queue q(5);
q.Enqueue(10);
q.Enqueue(20);
q.Enqueue(30);
q.Enqueue(40);
q.Enqueue(50);
q.Display();
printf("Dequeued : %d\n", q.Dequeue());
q.Display();
return 0;
}
// Output
// 10 20 30 40 50
// Dequeued : 20
// 20 30 40 50