C++ class实现链队列(完整代码)
生活随笔
收集整理的這篇文章主要介紹了
C++ class实现链队列(完整代码)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
代碼如下:
#include <iostream> using namespace std; typedef int ElemType;class QueueNode {friend class LinkQueue;public:QueueNode(): next(NULL) {};private:ElemType data;QueueNode *next; };class LinkQueue {public:LinkQueue(): front(NULL), rear(NULL) {};~LinkQueue() {QueueNode *p, *q;p = front;while (p) {q = p;p = p->next;delete q;}front = NULL;rear = NULL;};int Empty_Queue();int En_Queue(ElemType e);int De_Queue(ElemType &e);int Front_Queue(ElemType &e);private:QueueNode *front;QueueNode *rear; };int LinkQueue::Empty_Queue() {return (front == NULL && rear == NULL); }int LinkQueue::En_Queue(ElemType e) {QueueNode *p;p = new QueueNode();if (p) {p->data = e;if (rear) {rear->next = p;rear = p;} elsefront = rear = p;return 1;} elsereturn 0; }int LinkQueue::De_Queue(ElemType &e) {QueueNode *p;if (!Empty_Queue()) {p = front;e = p->data;front = front->next;if (!front)rear = NULL;delete p;return 1;} elsereturn 0; }int LinkQueue::Front_Queue(ElemType &e) {if (!Empty_Queue()) {e = front->data;return 1;} elsereturn 0; }int main() {LinkQueue l;l.En_Queue(23);l.En_Queue(45);l.En_Queue(452);l.En_Queue(12);int x;l.De_Queue(x);cout << x << endl;l.Front_Queue(x);cout << x << endl;cout << l.Empty_Queue() << endl;l.De_Queue(x);cout << x << endl;l.De_Queue(x);cout << x << endl;l.Front_Queue(x);cout << x << endl;l.De_Queue(x);cout << l.Empty_Queue() << endl;return 0; }測試結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的C++ class实现链队列(完整代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QQ拼音大五码(BIG5)如何使用
- 下一篇: C++ class实现二叉树(完整代码,