队列

链表实现

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
struct Node {
int item;
Node* next;
};

class Queue {
private:
Node* first = NULL;
Node* last = NULL;
public:
bool isEmpty() {
return first == NULL; //注意队列判空条件
}

void enQueue(int x) {
Node* oldlast = last;
last = new Node();
last->item = x;
last->next = NULL; //

if (isEmpty())
first = last;
else
oldlast->next = last;
}

int deQueue() {
int item = first->item;
first = first->next;
if (isEmpty())
last = NULL;
return item;
}
};

数组实现(循环队列)

1