C++資料結構之鏈式佇列模版實現
鏈佇列的儲存結構
將對頭指標front指向鏈佇列的頭結點(頭節點為空,不存資料),隊尾指標rear指向終端結點。元素從隊尾進入,隊首出列。
元素為空時,隊尾指標指向隊頭指標。
鏈式佇列模版實現:
功能:
1 建立
2 遍歷
4 入隊,出隊
5 獲取隊尾隊首隊中某位置值
6 修改隊尾隊首隊中某位置值
7 刪除隊尾隊首隊中某位置值
8 清空複製等
節點定義
template <typename T> struct Link { T data; struct Link *next; 建構函式 Link(){this->next = nullptr;} Link(const T &data){this->data = data; this->next = nullptr;} Link(const T &data, struct Link *next){this->data = data; this->next = next;} Link(const struct Link &node){this->data = node.data; this->next = node.next;} };
別名定義
template <typename T> using LinkNode = struct Link<T>;
佇列定義
template <typename T> class LinkQueue { private: LinkNode <T> *front; LinkNode<T> *rear; public:
建構函式 LinkQueue() {this->front = this->rear = new LinkNode<T>; this->rear->next = nullptr;} LinkQueue(const T &data){this->front = new LinkNode<T>; this->rear = new LinkNode<T>(data); this->front->next = this->rear;} LinkQueue(const LinkNode<T> &node){this->front = new LinkNode<T>; this->rear = new LinkNode<T>(node); this->front->next = this->rear;} LinkQueue(const LinkQueue<T> &queue); ~LinkQueue(); void clear(); bool isEmpty(); int length(); void show(); bool GetFrontNode(T &data); bool GetRearNode(T &data); bool GetPosNode(T &data, int pos); bool SetFrontNode(const T &data); bool SetRearNode(const T &data); bool SetPosNode(const T &data, int pos); bool PushRearNode(const T &data);
bool PopFrontNode(T &data); bool DelFrontNode(); bool DelRearNode(); bool DelPosNode(int pos); /* function:find the position of node which equal to data in the list data:the data which compared return value:the position which equal to data; this function need edit accoding the T */ int GetFirstNodePos(T &data); /* function:find the position of node which equal to data in the list data:the data which compared pos:the array which storage the position return value:the num which equal to data; this function need edit accoding the T */ int GetNodePos(T &data,int *pos);
};
成員函式實現:
複製建構函式:
template <typename T> LinkQueue<T>::LinkQueue(const LinkQueue<T> &queue) { ULOGE("LINKQUEUE destruct"); this->front = this->rear = new LinkNode<T>; this->rear->next = nullptr; if(queue.rear == queue.front){ ULOGW("empty queue"); this->front->next = nullptr; return; } LinkNode<T> *p1 = queue.front->next, *p2 = this->front, *p3; //this->front->next = p2; //p2 = p2->next; while(p1->next != nullptr){ p3 = new LinkNode<T>(*p1); p2->next = p3; p2 = p2->next; p1 = p1->next; } p3 = new LinkNode<T>(*p1); p2->next = p3; this->rear = p3; this->rear->next = nullptr; }
解構函式
template <typename T> LinkQueue<T>::~LinkQueue() { LinkNode<T> *p = this->front; while(p != nullptr){ p = p->next; delete this->front; this->front = p; } }
清除函式
template <typename T> void LinkQueue<T>::clear(){ LinkNode<T> *p1 = this->front->next; LinkNode<T> *p2 = p1; while(p1 != nullptr){ p1 = p1->next; delete p2; p2 = p1; } this->rear = this->front; this->front->next = nullptr; }
判斷是否為空
template <typename T> bool LinkQueue<T>::isEmpty() { if(this->front == this->rear) return true; return false; }
獲取長度
template <typename T> int LinkQueue<T>::length() { int len = 0; LinkNode<T> *p = this->front->next; while(p != nullptr){ p = p->next; ++len; } return len; }
遍歷
template <typename T> void LinkQueue<T>::show() { LinkNode<T> *p = this->front->next; std::cout<<"The sum of data in the queue is: "<<length()<<std::endl; std::cout<<"detail:"; while(p != nullptr){ std::cout<<p->data<<" "; p = p->next; } std::cout<<std::endl; }
獲取隊頭隊尾及pos處值
template <typename T> bool LinkQueue<T>::GetFrontNode(T &data) { return GetPosNode(data, 0); }
template <typename T> bool LinkQueue<T>::GetRearNode(T &data) { return GetPosNode(data, length()-1); }
template <typename T> bool LinkQueue<T>::GetPosNode(T &data, int pos) { if(pos < 0){ ULOGW("invalue pos"); return false; } int pos1 = pos; LinkNode<T> *p = this->front->next; while((p != nullptr)&& pos){ --pos; p = p->next; } if(p == nullptr){ ULOGW("no ", pos1, "th node"); return false; } data = p->data; return true; }
修改隊頭隊尾及pos位置值
template <typename T> bool LinkQueue<T>::SetFrontNode(const T &data) { return SetPosNode(data, 0); }
template <typename T> bool LinkQueue<T>::SetRearNode(const T &data) { return SetPosNode(data, length()-1); } template <typename T> bool LinkQueue<T>::SetPosNode(const T &data, int pos) { if(pos < 0){ ULOGW("invalue pos"); return false; } int pos1 = pos; LinkNode<T> *p = this->front->next; while((p != nullptr)&& pos){ --pos; p = p->next; } if(p == nullptr){ ULOGW("no ", pos1, "th node"); return false; } p->data = data; return true; }
入隊
template <typename T> bool LinkQueue<T>::PushRearNode(const T &data) { LinkNode<T> *p = new LinkNode<T>(data); this->rear->next = p; this->rear = p; }
出隊
template <typename T> bool LinkQueue<T>::PopFrontNode(T &data) { if(this->rear == this->front){ ULOGW("empty queue"); return false; } LinkNode<T> *p = this->front->next; data = p->data; if(p != this->rear){ this->front->next = p->next; }else{ this->rear = this->front; this->front->next = nullptr; } delete p; return true; } 刪除隊頭隊尾或pos處的值 template <typename T> bool LinkQueue<T>::DelFrontNode() { return DelPosNode(0); }
template <typename T> bool LinkQueue<T>::DelRearNode(){ return DelPosNode(length()-1); } template <typename T> bool LinkQueue<T>::DelPosNode(int pos) { if(pos < 0){ ULOGW("invalue pos"); return false; } int pos1 = pos; LinkNode<T> *p1 = this->front; LinkNode<T> *p2 = p1; p1 = p1->next; while((p1 != nullptr)&& pos){ --pos; p2 = p1; p1 = p1->next; } if(p1 == nullptr){ ULOGW("no ", pos1, "th node"); return false; } if(p1 != this->rear){ p2->next = p1->next; }else{ this->rear = p2; this->rear->next = nullptr; } delete p1; return true; }
如有問題歡迎大家指正。