鏈式隊列

聲明:圖片及內容基於https://www.bilibili.com/video/av94576761ios

原理分析

數據結構

注意本文只考慮有頭節點類型數據結構

基本數據類型

定義節點ide

template<class T>
struct Node { T data; struct Node<T>* next; };

聲明隊列函數

template<class T>
class LinkQueue { private: Node<T>* front, * rear;  //先後結構體指針 int length; //隊列長度 public: LinkQueue(); ~LinkQueue(); void enQueue(T x); //入隊 bool deQueue(T& item); //出隊 bool getFront(T& item); //得到頭元素 bool isEmpty();  //是否爲空 void clearQueue(); //清空隊列 void displayQueue(); //打印隊列 int queueLength(); //得到隊列長度 };

隊列初始化

LinkQueue<T>::LinkQueue() { front = new Node<T>; front->next = NULL; rear = front; length = 0; //別忘了 }

析構函數

LinkQueue<T>::~LinkQueue() { struct Node<T>* p =NULL; while (front) { p = front->next; delete front; front = p; } }

入隊

template<class T>
void LinkQueue<T>::enQueue(T x) { struct Node<T>* p = new Node<T>; p->data = x; rear->next = p; rear = p; rear->next = NULL;   //vs裏必須讓尾節點next=NULL不然異常
    length++;  //別忘了 }

出隊

template<class T>
bool LinkQueue<T>::deQueue(T &item) { if (isEmpty()) return false; item = front->next->data; struct Node<T>* p = front->next; //front是頭節點沒有數據 front->next = p->next; if (p->next == NULL)  rear = front; //防止只有一個元素時,其被刪除後rear變爲野指針
    delete p; length--;  //別忘了 return true; }

得到頭元素

template<class T>
bool LinkQueue<T>::getFront(T& item) { if(isEmpty()) return false; item = front->next->data; //front是頭節點沒有數據 return true; }

判斷是否爲空

template<class T>
bool LinkQueue<T>::isEmpty() { if (front == rear) return true; return false; }

清空隊列

template<class T>
void LinkQueue<T>::clearQueue() { struct Node<T>* p ; while (front) { p = front->next; delete front; front = p; } front = rear = NULL; //初始化頭尾指針 length = 0;  //別忘了 }

打印隊列

template<class T>
void LinkQueue<T>::displayQueue() { if (front == NULL) {  //隊列爲空return; } struct Node<T>* p = front->next; while (p!=NULL) { cout << p->data << " "; p = p->next; } cout << endl; }

得到隊列長度

template<class T>
int LinkQueue<T>::queueLength() { return length; }

完整代碼

#include<iostream>
using namespace std; template<class T>
struct Node { T data; struct Node<T>* next; }; template<class T>
class LinkQueue { private: Node<T>* front, * rear; int length; public: LinkQueue(); ~LinkQueue(); void enQueue(T x); bool deQueue(T& item); bool getFront(T& item); bool isEmpty(); void clearQueue(); void displayQueue(); int queueLength(); }; template<class T> LinkQueue<T>::LinkQueue() { front = new Node<T>; front->next = NULL; rear = front; length = 0; } template<class T> LinkQueue<T>::~LinkQueue() { struct Node<T>* p =NULL; while (front) { p = front->next; delete front; front = p; } } template<class T>
void LinkQueue<T>::enQueue(T x) { struct Node<T>* p = new Node<T>; p->data = x; rear->next = p; rear = p; rear->next = NULL;   //vs裏必須讓尾節點next=NULL不然異常
    length++; } template<class T>
bool LinkQueue<T>::deQueue(T &item) { if (isEmpty()) return false; item = front->next->data; struct Node<T>* p = front->next; front->next = p->next; if (p->next == NULL)  rear = front; //防止只有一個元素時rear以後變爲野指針
    delete p; length--; return true; } template<class T>
bool LinkQueue<T>::getFront(T& item) { if(isEmpty()) return false; item = front->next->data; return true; } template<class T>
bool LinkQueue<T>::isEmpty() { if (front == rear) return true; return false; } template<class T>
void LinkQueue<T>::clearQueue() { struct Node<T>* p ; while (front) { p = front->next; delete front; front = p; } front = rear = NULL; length = 0; } template<class T>
void LinkQueue<T>::displayQueue() { if (front == NULL) { return; } struct Node<T>* p = front->next; while (p!=NULL) { cout << p->data << " "; p = p->next; } cout << endl; } template<class T>
int LinkQueue<T>::queueLength() { return length; } int main() { LinkQueue<int> Queue; Queue.enQueue(1); Queue.enQueue(2); Queue.enQueue(3); Queue.displayQueue(); int d; Queue.deQueue(d); Queue.displayQueue(); cout << "d=" << d << endl; cout << "length=" << Queue.queueLength()<<endl; int f; Queue.getFront(f); cout << "front=" <<f <<endl; Queue.clearQueue(); if (Queue.isEmpty()) cout << "empty" << endl; cout << "length=" << Queue.queueLength()<<endl; Queue.displayQueue(); return 0; }
相關文章
相關標籤/搜索