#include<iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node * push(node *top,int val)
{
node *p = new node();
if(p!=NULL)
{
p->val=val;
p->next=top;
top=p;
}
return top;
}
node *pop(node* top, int *val)
{
if(top==NULL)
{
return NULL;
}
node *p = top;
*val = top->val;
top = top->next;
delete p;
p=NULL;
return top;
}
void print(node *top)
{
node *p = top;
if(p==NULL)
{
return;
}
while(p!=NULL)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
int main()
{
int n,m,val;
node *top=NULL;
cin>>n;
while(n--)
{
cin>>m;
top=push(top,m);
}
print(top);
top=pop(top,&val);
cout<<val<<endl;
print(top);
return 0;
}
链栈的基本操作
最新推荐文章于 2024-01-23 21:08:19 发布