-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.h
96 lines (85 loc) · 1.98 KB
/
queue.h
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
#pragma once
#include <iostream>
class Queue
{
private:
int *queue;
int front, rear;
int size;
public :
Queue();
Queue(int s);
~Queue();
void enqueue(int n);
int dequeue();
void displayItems();
};
Queue::Queue(){
front = 0;
rear = 0;
size = 5;
queue = new int[size];
}
Queue::Queue(int s){
front = 0;
rear = 0;
size = s;
queue = new int[size];
}
Queue::~Queue(){
delete[] queue;
}
void Queue::enqueue(int n){
using namespace std;
if (rear == size)
{
// cout << "Queue if Full" << endl;
if ((rear-front) < size){
int j = 0;
for (int i = front; i < rear; i++)
{
queue[j++] = queue[i];
}
front = 0;
rear = j;
}
else
{
// int sizeAdd;
// do
// {
// cout << "Enter the additional size (positive integer): " << endl;
// cin >> sizeAdd;
// } while (sizeAdd <= 0);
size += size;
int *queueNew = new int[size];
for (int i = front; i <= rear; i++)
{
queueNew[i] = queue[i];
}
delete[]queue;
queue = queueNew;
}
}
queue[rear++] = n;
}
int Queue::dequeue(){
using namespace std;
if (front == rear) {
cout << "Empty Queue... Returning 0" << endl;
return 0;
}
return queue[++front];
}
void Queue::displayItems(){
using namespace std;
cout << "Queue \n------------------" << endl;
cout << "Size: " << size << endl;
cout << "No of Elements: " << rear-front << endl;
cout << "Queue Elements \n------------------" << endl;
for (int i = front; i < rear; i++)
{
cout << queue[i] << " ";
}
cout << "\n------------------\n\n" << endl;
}