#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 100
typedef int SElemType;
typedef int Status;
#define ERROR 0
#define TRUE 1
#define OK 1
#define FALSE 0
typedef struct StackNode{
SElemType data;
struct StackNode* next;
}StackNode,*LinkStackPtr;
typedef struct LinkStack{
LinkStackPtr top;
int count;
} LinkStack;
Status StackEmpty (LinkStack* S);
Status InitLinkStack(LinkStack* s);
Status Push(LinkStack *S,SElemType e);
Status Pop(LinkStack* S,SElemType* e);
Status printStack(LinkStack* S);
Status ClearStack(LinkStack* S);
Status InitLinkStack(LinkStack* s){
s->top=NULL;
s->count=0;
}
Status Push(LinkStack *S,SElemType e){
LinkStackPtr s=(LinkStackPtr) malloc(sizeof(StackNode));
s->data=e;
s->next=S->top;
S->top=s;
S->count++;
return OK;
}
Status Pop(LinkStack* S,SElemType* e){
LinkStackPtr p;
if(StackEmpty(S)){
return ERROR;
}
*e=S->top->data;
p=S->top;
S->top=S->top->next;
free(p);
S->count--;
return OK;
}
Status printStack(LinkStack* S){
LinkStackPtr p=S->top;
while(p){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
return OK;
}
Status StackEmpty (LinkStack* S){
return S->count;
}
Status ClearStack(LinkStack* S){
LinkStackPtr p=(S)->top;
LinkStackPtr q;
while(p){
q=p;
p=p->next;
free(q);
}
(S)->count=0;
return OK;
}
int main(void){
LinkStack* S=malloc(sizeof(LinkStack));
int data;
if(InitLinkStack(S)){
printf("success init LinkStack\n");
}
for(int i=0;i<10;i++){
Push(S,rand()%100+1);
}
printf("success push data in LinkStack\n");
printStack(S);
Pop(S,&data);
printf("success pop data in LinkStack,Poped data is %d\n",data);
printStack(S);
ClearStack(S);
printf("success clear LinkStack\n");
return 0;
}
大话数据结构-链栈ADT实现
最新推荐文章于 2022-08-02 20:38:26 发布