写得太偷懒,太低端。
以后的代码尽量用c++,等稍微学习下python那也是极好的。
queue.h
1 #ifndef QUEUE_QUEUE_H
2 #define QUEUE_QUEUE_H
3
4 #include <stddef.h>
5
6 typedef int DataType;
7
8 typedef struct _Node {
9 DataType data;
10 struct _Node *next;
11
12 _Node(DataType d) {
13 data = d;
14 next = NULL;
15 }
16 _Node() {
17 data = 0;
18 next = NULL;
19 }
20 }NODE;
21
22
23 //With Head Node
24 class QUEUE {
25 public:
26 QUEUE();
27 ~QUEUE();
28
29 void Push(DataType);
30 //return 1 == OK
31 //return 0 == ERROR
32 int Pop(DataType*);
33
34 private:
35 NODE *head;
36 NODE *tail;
37 };
38
39
40 #endif //QUEUE_QUEUE_H
queue.cpp
1 #include "queue.h"
2
3 QUEUE::QUEUE() {
4 head = new NODE(0);
5 tail = head;
6 }
7
8 void QUEUE::Push(DataType data) {
9 NODE *tmp = new NODE(data);
10 tail->next = tmp;
11 tail = tmp;
12 tmp = NULL;
13 }
14
15 int QUEUE::Pop(DataType *data) {
16 if (head->next != NULL) {
17 NODE *tmp = head->next;
18 *data = tmp->data;
19 head->next = tmp->next;
20 if(head->next == NULL) {
21 tail = head;
22 }
23 delete tmp;
24 tmp = NULL;
25 return 1;
26 } else {
27 return 0;
28 }
29 }
30
31 QUEUE::~QUEUE() {
32 NODE *tmp = head;
33 while (tmp != NULL) {
34 delete tmp;
35 head = head->next;
36 tmp = head;
37 }
38 tmp = NULL;
39 head = NULL;
40 tail = NULL;
41 }