日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++之链队列

發(fā)布時(shí)間:2024/9/21 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++之链队列 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、學(xué)習(xí)要點(diǎn):
1.鏈隊(duì)列的實(shí)現(xiàn)不用限制隊(duì)列的長度;當(dāng)刪除元素時(shí),只需要改變頭結(jié)點(diǎn)指向的首元結(jié)點(diǎn)(phead->next=phead->next->next),插入只改變尾節(jié)點(diǎn)的(pnode為新插入結(jié)點(diǎn),pend->next=pnode;pend=pnode,即可)
2.頭結(jié)點(diǎn),首元結(jié)點(diǎn),尾節(jié)點(diǎn)有不明白的可參考我的上一篇博客。
3.總體來說,鏈隊(duì)列的實(shí)現(xiàn)比數(shù)組隊(duì)列的實(shí)現(xiàn)要方便,而且實(shí)用。
4.結(jié)構(gòu)體里面也可以有構(gòu)造器;
5.第一個(gè)元素一定是插在初始化的pend->next;所以在初始化的時(shí)候一定要讓phead=pend,達(dá)到讓phead指向首元元素的目的。
6.c++中取反用!;不是~,切記切記切記;
二、代碼:
demo092103.cpp

#include<iostream> #include<stdlib.h> using namespace template<class T> struct Node{Node(T t):value(0),next(nullptr){};Node();T value;Node<T>* next; } template<class T> class Linkqueque{ public:Linkqueque();~Linkqueque();bool isEmpty();int size();void push(T t);bool pop();T front(); private:Node<T>* phead;Node<T>*pend;int count; }; Linkqueque<T>::Linkqueque(){//此處一定要有phead=pend,因?yàn)椴迦胧遣逶趐end的next里,故第一個(gè)元素一定在pend->next;因?yàn)榈谝粋€(gè)元素一定在初始化的pend后面,所以初始化的時(shí)候一定有phead=pend;讓phead指向第一個(gè)元素,即首元元素Node<T>* phead=new Node<T>();pend=phead;count=0; } template<class T> Linkqueque<T>::~Linkqueque(){while(pend->next!=nullptr){Node<T>* pnode=new Node<T>();pnode=pend->next;phead=pend->next;delete pnode;} } template<class T> void Linkqueque<T>::push<T t>{Node<T>* pnode=new Node<T>(t);pend->next=pnode;pend=pnode;count++; } template<class T> bool Linkqueque<T>::pop(){ if(count==0){return false;}else{Node<T>* pnode;pnode=phead->next;phead->next=phead->next->next;delete pnode;count--;return true;} } template<class T> bool Linkqueque<T>::isEmpty(){ return count==0; } template<class T> int Linkqueque<T>::size(){ return count; } template<class T> T Linkqueque<T>::front(){ return (phead->next->vlaue); }

主函數(shù)代碼
main.cpp

#include<iostream> #include<stdlib.h> #include<string> #include"demo092103.cpp" using namespace std; int main(){Linkqueque<string> lqueque;lqueque.push("one");lqueque.push("two");lqueque.push("three");lqueque.push("four");lqueque.push("five");cout<<"隊(duì)列大小"<<lqueque.size()<<endl;while(!lqueque.isEmpty()){cout<<lqueque.front()<<endl;lqueque.pop();}system("pause");return 0; }

三、運(yùn)行結(jié)果:

四、如有錯(cuò)誤,歡迎指出,一塊交流學(xué)習(xí);

總結(jié)

以上是生活随笔為你收集整理的C++之链队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。