栈的链式入栈出栈
yly11 2008-03-13 06:08:30 #include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct sqst
{
int data;
struct sqst *next;
}stack;
void initstack(stack * &ls)
{
ls=NULL;
}
void push(int x,stack * ls)
{
stack * p;
p=(stack *)malloc(sizeof(stack));
p->data=x;
p->next=ls;
ls=p;
}
int pop(stack * ls,int &x)
{
stack * p;
if(ls==NULL)
return 0;
else
{p=ls;
x=p->data;
cout<<x<<" ";
ls=p->next;
free(p);
return 1;}
}
void main()
{ int x;
stack *ls ;
initstack(ls);
cout<<"ru zhan";
int i=1;
while(i)
{ cin>>x;
if(x=0) i=0;
push(x,ls);
}
while(ls!=NULL)
pop(ls,x);
}