写得太偷懒,太低端。
以后的代码尽量用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 delete tmp; 21 tmp = NULL; 22 return 1; 23 } else { 24 return 0; 25 } 26 } 27 28 QUEUE::~QUEUE() { 29 NODE *tmp = head; 30 while (tmp != NULL) { 31 delete tmp; 32 head = head->next; 33 tmp = head; 34 } 35 tmp = NULL; 36 head = NULL; 37 tail = NULL; 38 }
时间: 2024-10-11 04:58:58