資料結構筆記#隊列

來源:互聯網
上載者:User

標籤:

寫得太偷懶,太低端。

以後的代碼盡量用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 Node24 class QUEUE {25 public:26     QUEUE();27     ~QUEUE();28 29     void Push(DataType);30     //return 1 == OK31     //return 0 == ERROR32     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 }

 

資料結構筆記#隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.