-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.h
201 lines (163 loc) · 3.6 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//--------------------------------------------------------------------------------------------------------------------------------
// Hassan Shahzad & Azka Khurram
// 18i-0441 & 18i-0461
// Algo-Project
// FAST NUCES
// Queue.h
//--------------------------------------------------------------------------------------------------------------------------------
//================================================================================================================================
// 1: This file contains a Queue data structure with all its functionalities and some adjusted according to our requirements
//================================================================================================================================
#pragma once
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
struct node
{
char data;
int freq = 0;
node* left;
node* right;
node() : data('0'), freq(0), left(NULL), right(NULL) {}
node(char d, int f) : data(d), freq(f), left(NULL), right(NULL) {}
};
class Queue
{
node* queueArray;
int queueSize;
int front;
int rear;
int numItems;
public:
Queue()
{
queueArray = NULL;
queueSize = 0;
front = -1;
rear = -1;
numItems = 0;
}
Queue(int size)
{
queueArray = new node[size];
queueSize = size;
front = -1;
rear = -1;
numItems = 0;
}
void resize() { // doubles the queue size
node* temp = new node[queueSize];
for (int i = 0; i < queueSize; i++)
temp[i] = queueArray[i];
delete[] queueArray;
queueSize *= 2;
queueArray = new node[queueSize];
for (int i = 0; i < numItems; i++)
queueArray[i] = temp[i];
delete[] temp;
}
void initializeQueue(int size)
{
if (queueArray != NULL)
delete[]queueArray;
queueArray = new node[size];
queueSize = size;
front = -1;
rear = -1;
numItems = 0;
}
~Queue()
{
delete[]queueArray;
}
bool isFull()
{
if (numItems < queueSize)
return false;
return true;
}
int returnIndex(char c)
{
for (int i = 0; i < numItems; i++)
{
if (queueArray[i].data == c)
return i;
}
return -1;
}
bool incFreq(int index)
{
if (queueArray != NULL and index >= 0 and index < numItems)
{
queueArray[index].freq++;
return true;
}
return false;
}
void sort()
{
if (isEmpty())
return;
int i, j;
for (i = front + 1; i < front + numItems; i++)
{
node temp = queueArray[i];
for (j = i; j > 0 && temp.freq < queueArray[j - 1].freq; j--)
{
queueArray[j] = queueArray[j - 1];
}
queueArray[j] = temp;
}
}
void enqueue(node dataItem)
{
if (front == -1)
front = 0;
rear = rear + 1;
numItems++;
queueArray[rear] = dataItem;
if (numItems == queueSize)
this->resize();
sort();
}
node dequeue()
{
node temp;
if (this->isEmpty())
{
cout << "Queue is empty.\n";
return temp;
}
else
{
temp = queueArray[front];
front++;
numItems--;
return temp;
}
}
bool isEmpty() const
{
if (numItems == 0)
return true;
return false;
}
void display()
{
cout << " --------------------- " << endl;
cout << " Character | Freq " << endl;
cout << " --------------------- " << endl;
for (int i = front; i < front + numItems; i++)
{
cout << "| " << queueArray[i].data << " | " << queueArray[i].freq << " | " << endl;
cout << " --------------------- " << endl;
}
}
int numOfItems()
{
return numItems;
}
};