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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构-链表栈

發布時間:2024/2/28 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构-链表栈 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈表棧的實現

#include<iostream> using namespace std; struct stacknode{int date;stacknode* next;stacknode(int x=0,stacknode* io=NULL){date=x;next=io;} }; class stack{ private:stacknode* first;int size; public:int pop();void push(int x);int length(){return size+1;//正確記錄時為0~..所以是size+1}stack(int x,stacknode* io=NULL){first=new stacknode(x,io);size=0;//因為已經有了第一個節點}stacknode* getup(){//復雜了,這里直接使用反向鏈(即使用second-》link=first)更好,減少尋找上一節點的時間。int i=0;if(size==0){//當前棧頂為first時return NULL;}stacknode* ioo=first;while(i<size-1){//因為從0開始,最后結束時為i=size;ioo=ioo->next ;i++;}return ioo;}void input(int n){int x;while(n>0){cin>>x;if(x==0){return;}push(x);n--;}}void output(int n){n--;//因為開始length()返回的是個數,是從1開始的cout<<"output操作:" <<endl;while(n>=0){cout<<"pop操作: ";cout<<pop()<<endl;n--;}}~stack(){cout<<"調用析構函數" <<endl;while(0<=size) {stacknode *ioo=getup()->next;//當前棧頂節點delete ioo;size--;}cout<<size<<endl;}stacknode* getTop(){stacknode*ioo=getup()->next;return ioo;}}; //注意凡是涉及指針都要注意到先要有內存分配了之后才能作為左值使用. int stack::pop(){if(size==0){//當節點只有first時也是可以進行刪除的,所以就要做特殊的判斷.//如果大小 小于0了就不需要進入pop函數了直接就在外面判斷掉就行了size--;return first->date;}stacknode* ioo=getup();int x=getup()->next->date;//棧頂數據stacknode* ip=ioo->next;ioo->next=ip->next;delete ip;size--;return x; } void stack::push(int x){stacknode *ioo;if(getup()==NULL){ioo=first;ioo->next=new stacknode(x) ;size++;}else{stacknode *ioo=getup()->next;ioo->next=new stacknode(x) ;size++;} } int main(){stack io(12);//第一個元素為12;int n;cin>>n;io.input(n);cout<<io.length()<<endl;io.output(io.length()); }

總結

以上是生活随笔為你收集整理的数据结构-链表栈的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。