//链式栈的基本操作.... #include<iostream> using namespace std; typedef struct node//构造栈的节点..... { int data; struct node *next; }LinkStack; void Init_LinkStack(LinkStack *st)//初始化链式栈... { st->next=NULL; } void Creat_LinkStack(LinkStack *st,int n)//创建链式栈.... { int i; LinkStack *p; for(i=1;i<=n;i++) { p=new LinkStack; cin>>p->data; p->next=st->next; st->next=p; } } void Destroy_LinkStack(LinkStack *st)//链式栈的摧毁... { while(st) { LinkStack *q; q=st; st=st->next; delete q; } } void LinkStack_Push(LinkStack *st,int x)//入栈操作.... { LinkStack *p; p=new LinkStack; p->data=x; p->next=st->next; st->next=p; } void LinkStack_Pop(LinkStack *st)//出栈操作..... { if(st->next==NULL) { cout<<"栈已经空了"<<endl; } else { LinkStack *p; p=st->next; st->next=p->next; delete p; } } void LinkStack_Out(LinkStack *st)//栈的输出.... { LinkStack *p; p=st->next; while(p) { cout<<p->data<<" "; p=p->next; } } int Get_Length_LinkStack(LinkStack *st)//得到链式栈的长度.... { int k=0; while(st->next) { k++; st=st->next; } return k; } int main() { LinkStack *st; st=new LinkStack; Init_LinkStack(st); Creat_LinkStack(st,5); int count; count=Get_Length_LinkStack(st); cout<<"长度为:"<<endl; cout<<count<<endl; LinkStack_Push(st,10); LinkStack_Pop(st); LinkStack_Out(st); return 0; }