SqStack栈的链式存储
#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 200
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int SElemType;
typedef int Status;
typedef struct StackNode{
SElemType data;
struct StackNode *next;
}StackNode,*LinkStackptr;
typedef struct LinkStack{
LinkStackptr top;
int count;
}LinkStack;
Status InitStack(LinkStack *S){
S->top=NULL;
S->count=0;
return OK;
}
Status PushStack(LinkStack *s,SElemType e){
LinkStackptr ss=(LinkStackptr)malloc(sizeof(StackNode));
ss->data=e;
ss->next=s->top;
s->top=ss;
s->count++;
return OK;
}
Status PopStack(LinkStack *s,SElemType *e){
LinkStackptr p;
if (s->top==NULL){
printf("stack is empty\n");
return ERROR;
}
*e=s->top->data;
p=s->top;
s->top=s->top->next;
free(p);
s->count--;
printf("%d\n",(*e));
return OK;
}
int main(){
int k;
LinkStack s;
SElemType e;
InitStack(&s);
while(1){
printf("入栈请输1,出栈请输2,结束请输0\n");
scanf("%d",&k);
if(k==1){
scanf("%d",&e);
PushStack(&s,e);
}
else if(k==2){
PopStack(&s,&e);
}
else if(k==0){
break;
}
}
return 0;
}