📄 5链栈的操作.cpp
字号:
#include <iostream.h>
struct linkstack
{
int data;
linkstack *next;
};
linkstack *Push_LinkStack(linkstack *top,int x)
{
linkstack *s;
s=new linkstack;
s->data=x;
s->next=top;
top=s;
return top;
}
linkstack *Pop_LinkStack(linkstack *top,int *x)
{
linkstack *p;
if(top==NULL)
{
cout<<"栈空!"<<endl;
return NULL;
}
else
{
*x=top->data;
p=top;
top=top->next;
delete p;
return top;
}
}
linkstack *Init_LinkStack()
{
linkstack *top;
int x;
top=NULL;
cout<<"建立链栈,输入数据,以-1结束:"<<endl;
cin>>x;
while(x!=-1)
{
top=Push_LinkStack(top,x);
cin>>x;
}
return top;
}
void Print(linkstack *top)
{
linkstack *s;
s=top;
cout<<"链栈中的数据为(从栈顶到栈底):";
while(s!=NULL)
{
cout<<s->data<<" ";
s=s->next;
}
cout<<endl;
}
void main()
{
linkstack *top;
int x;
top=Init_LinkStack();
cout<<"输入压栈的数据:";
cin>>x;
top=Push_LinkStack(top,x);
Print(top);
top=Pop_LinkStack(top,&x);
cout<<"链栈栈顶元素出栈:"<<x<<endl;
Print(top);
top=Pop_LinkStack(top,&x);
cout<<"链栈栈顶元素再出栈:"<<x<<endl;
Print(top);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -