数据结构c语言实现链栈
2019-01-27 本文已影响0人
甜柚小仙女
#include<stdio.h>
#include<stdlib.h>
#define ElemType int
typedef struct StackNode{
struct StackNode *next;
ElemType data;
}Node;
typedef struct Stack{
Node *top;
int count;
}LinkStack;
LinkStack *createStack(){
LinkStack *s ;
s = (LinkStack*)malloc(sizeof(LinkStack));
s->top = NULL;
s->count = 0;
return s;
}
int isNull(LinkStack *s){
if(s->top==NULL){
return 1;
}
return 0;
}
void pushLinkStack(LinkStack *s,ElemType ele){
Node *p;
p=(Node*)malloc(sizeof(Node));
p->data = ele;
p->next = s->top;
s->top = p;
s->count++;
}
ElemType popLinkStack(LinkStack *s){
if(!isNull(s)){
ElemType res = s->top->data;
s->top = s->top->next;
s->count--;
return res;
}
return -1;
}
void getCount(LinkStack *s){
printf("%d\n",s->count);
}
void getTop(LinkStack *s){
printf("%d\n",s->top->data);
}
int main(){
LinkStack *s;
s=createStack();
int i;
for(i=1;i<100;i++){
pushLinkStack(s,i);
}
getTop(s);
getCount(s);
for(i=1;i<100;i++){
printf("%d ",popLinkStack(s));
}
}