-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkqueue.h
67 lines (53 loc) · 1.37 KB
/
linkqueue.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
//
// Created by q on 3/19/21.
//
#ifndef LINKQUEUE_LINKQUEUE_H
#define LINKQUEUE_LINKQUEUE_H
#include "queue.h"
class emptyError{};
template <class elemType>
class linkQueue: public queue<elemType>{
struct node{
elemType data;
node* next;
node() = default;
node(const elemType &e, node* n = nullptr) : data(e), next(n){}
~node() = default;
};
private:
node* front, *rear;
public:
linkQueue(){front = rear = nullptr;}
~linkQueue();
bool empty() const {return front == nullptr;}
elemType get_front() const {if (empty()) throw emptyError(); return front->data; }
elemType get_rear() const {if (empty()) throw emptyError(); return rear->data;}
void push(const elemType &x);
elemType pop();
};
template <class elemType>
linkQueue<elemType>::~linkQueue() {
node* p;
while(front){
p = front->next;
delete front;
front = p;
}
}
template <class elemType>
void linkQueue<elemType>::push(const elemType &x){
if (empty()) front = rear = new node(x);
else
rear = rear->next = new node(x);
}
template <class elemType>
elemType linkQueue<elemType>::pop() {
if(empty()) throw emptyError();
elemType tmp = front->data;
node* n = front;
front = front->next;
if (front == nullptr) rear = front;
delete n;
return tmp;
}
#endif //LINKQUEUE_LINKQUEUE_H