链式栈的基本操作,如进栈,出栈,判栈空等
#include<iostream.h>
#include<malloc.h>
#define NULL 0
typedef struct snode{
int data;
struct snode *next;
}stack;
void push( struct snode *s,int x)
{struct snode *p;
p=( struct snode *)malloc(sizeof(snode));
cin>>x;
p->data=x;
p->next=s;
s=p;
}
void pop( struct snode *s )
{struct snode *p;
int e;
p=(struct snode *)malloc(sizeof(snode));
if(s->next==NULL) cout<<"error";
else
{p=s;
e=p->data;
s=p->next;
}
free(p);
cout<<e;
}
void print()
{struct snode *s;
if(s->next==NULL) cout<<"空栈 ";
else if(s->next!=NULL)
{cout<<s->data;
s=s->next;
}
}
void stackempty()
{struct snode *s;
if(s->next==NULL) cout<<"栈空";
else cout<<"栈非空";
}
void main()
{struct snode *s;
int f,x,e;
cin>>f;
while(1)
{
switch (f)
{case 'a':cin>>x;
push(s,x);
print();
stackempty();
break;
case 'b':pop(s);
print();
stackempty();
break;
}
}
}
有问题,帮我改一下吧.谢谢啦!