生活随笔
收集整理的這篇文章主要介紹了
数据结构-链表栈
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈表棧的實現
#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;}stack(int x
,stacknode
* io
=NULL){first
=new stacknode(x
,io
);size
=0;}stacknode
* getup(){int i
=0;if(size
==0){return NULL;}stacknode
* ioo
=first
;while(i
<size
-1){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
--;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){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);int n
;cin
>>n
;io
.input(n
);cout
<<io
.length()<<endl
;io
.output(io
.length());
}
總結
以上是生活随笔為你收集整理的数据结构-链表栈的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。