链栈的建立、插入、弹出、判空。
#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxSize 100
typedef struct LNode
{
int data;
struct LNode *next;
} LNode;
void initStack(LNode *&lst)
{
lst=(LNode*)malloc(sizeof(LNode));
lst->next=NULL;
}
int isEmpty(LNode *lst)
{
if(lst->next==NULL)
cout<<"It is empty."<<endl;
else
cout<<"It is not empty."<<endl;
}
void push(LNode *lst, int x)
{
LNode *p;
p=(LNode*)malloc(sizeof(LNode));
p->next=NULL;
p->data=x;
p->next=lst->next;
lst->next=p;
}
int pop(LNode *lst,int &x)
{
LNode *p;
if(lst->next==NULL)
return 0;
p=lst->next;
x=p->data;
lst->next=p->next;
free(p);
return 1;
}
void print(LNode *lst)
{
LNode *p;
p=lst->next;
while(p->next!=NULL)
{
cout<<p->data;
p=p->next;
}
if(p->next==NULL)
cout<<p->data;
}
int main()
{
LNode *lst;
int x;
initStack(lst);
isEmpty(lst);
push(lst,1);
push(lst,2);
push(lst,3);
push(lst,4);
push(lst,5);
print(lst);
cout<<endl;
pop(lst,x);
print(lst);
cout<<endl;
pop(lst,x);
print(lst);
cout<<endl;
pop(lst,x);
print(lst);
cout<<endl;
pop(lst,x);
print(lst);
cout<<endl;
isEmpty(lst);
}