写的链栈得不到结果
源码如下:编译没有错误,但是就是得不到结果!望各位大侠帮帮忙!#include<iostream>
using namespace std;
typedef struct node //节点定义
{
int data;
struct node *next;
}snode;
class stack //栈的类定义
{
private:
snode *top;
int length;
public:
stack create_stack(stack *st);
void push(stack *st,int x);
void pop(stack *st);
void print(stack *st);
};
//几个函数的实现
stack stack::create_stack(stack *st)
{
st->top=NULL;
st->length=0;
return(*st);
}
void stack::push(stack *st,int x)
{
node *s;
s=(snode *)malloc(sizeof(snode));
s->data=x;
s->next=st->top;
st->top=s;
st->length++;
return;
}
void stack::pop(stack *st)
{
node *p,*s;
p=(node *)malloc(sizeof(node));
if(st->top==NULL)
exit(0);
else
{
p=st->top;
st->top=st->top->next;
free(p);
st->length--;
}
return;
}
void stack::print(stack *st)
{
node *s;
s=(node *)malloc(sizeof(node));
while(st->top!=NULL)
{
s=st->top;
cout<<s->data<<endl;
s=s->next;
}
return;
}
void main()
{
stack *st;
st->create_stack(st);
st->push(st,2);
st->push(st,1);
st->print(st);
return;
}