#include<stdio.h>
#include<stdlib.h>
typedef struct LinkNode{
int data; //数据域
struct LinkNode *next; //指针域
}LNode,*LiStack; //栈类型定义
int main(){
LiStack InitLinkStack();
bool InsertStack(LiStack &S,int e);
LNode * SeclectStack(LiStack S,int e);
int deleteStack(LiStack &S,int &e);
bool empty(LiStack S);
LiStack S;
S=InitLinkStack();
int i;
for(i=0;i<10;i++){
InsertStack(S,i);
}
LNode *q;
q= SeclectStack(S,1);
printf("栈顶元素为:%d\n",q->data);
int w=0;
for(i=0;i<10;i++){
w=deleteStack(S,w);
printf("出栈元素为:%d\n",w);
}
if(empty(S)){
printf("这是一个空栈");
}else{
printf("这不是一个空栈");
}
return 0;
}
//带头结点
//创(初始化)
LiStack InitLinkStack(){
LiStack S;
S=(LNode *)malloc(sizeof(LNode));
S->next=NULL;
return S;
}
//增(进栈)
bool InsertStack(LiStack &S,int e){
LNode *x;
x=(LNode *)malloc(sizeof(LNode));
if(x==NULL){
return false; //内存空间不足
}
x->data =e;
x->next = S->next;
S->next =x;
return true;
}
//查(获取栈顶元素)
LNode * SeclectStack(LiStack S,int e){
return S->next;
}
//删(出栈)
int deleteStack(LiStack &S,int &e){
e = S->next->data;
S->next =S->next->next;
return e;
}
//判断为空栈
bool empty(LiStack S){
if(S->next == NULL){
return true;
}else{
return false;
}
}