forked from CodersForLife/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.h
More file actions
79 lines (67 loc) · 1.36 KB
/
Copy pathQueue.h
File metadata and controls
79 lines (67 loc) · 1.36 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
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;
template <class T>
class Queue {
private:
T *data;
int first, last;
int length;
public:
Queue() {
data = new T[100];
first = last = 0;
length = 100;
}
Queue(int _length): length(_length) {
data = new T[_length];
first = last = 0;
}
~Queue() {
if (data) {
delete[] data;
}
}
void push(T _data) {
if (last < length) {
if (first != last) {
data[last] = _data;
} else {
data[first] = _data;
}
last++;
} else {
cout<<"Queue is full"<<endl;
}
}
T pop() {
if (first != last) {
T ret = data[first];
for (int i = first; i < last; i++) {
data[i] = data[i+1];
}
last--;
return ret;
} else {
return T();
}
}
T peek() {
return data[first];
}
int occupiedSize() {
return last;
}
void print() {
if (first != last) {
for (int i = first; i < last; i++) {
cout<<data[i]<<" ";
}
cout<<endl;
} else {
cout<<"Queue is empty"<<endl;
}
}
};
#endif // QUEUE_H