-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.hpp
73 lines (54 loc) · 1018 Bytes
/
queue.hpp
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
/*
Subject: node.hpp
Created Date: 2019-08-15
Author: Dodo([email protected])
Description:
*/
#pragma once
#include <iostream>
using namespace std;
template <typename T>
Queue<T>::Queue() {
front = NULL;
rear = NULL;
}
template <typename T>
Queue<T>::~Queue() {
Node<T>* tmpNode = NULL;
while (front != NULL) {
tmpNode = front;
front = front->getNext();
delete[] tmpNode;
}
}
template <typename T>
void Queue<T>::enqueue(T data) {
Node<T>* createNode = NULL;
Node<T>* curNode = NULL;
createNode = new Node<T>();
createNode->setData(data);
createNode->setNext(NULL);
if (front == NULL) {
front = createNode;
rear = front;
}
else {
rear->setNext(createNode);
rear = rear->getNext();
}
}
template <typename T>
T Queue<T>::dequeue() {
Node<T>* tmpNode = front;
T data = front->getData();
front = front->getNext();
delete[] tmpNode;
return data;
}
template <typename T>
int Queue<T>::isEmpty() const {
if (this->first != NULL)
return -1;
else
return 0;
}