#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
typedef struct LinkNode{
int data;
struct LinkNode *next;
}LinkNode,*LiStack;
void InitStack(LiStack &S){
S=NULL;
}
bool Push(LiStack &S,int e){
LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode));
s->data = e;
if(S == NULL){
S = s;
s->next = NULL;
}else{
s->next = S;
S = s;
}
return true;
}
bool Pop(LiStack &S,int &e){
if(S == NULL){
return false;
}
LinkNode *p = S;
e = p->data;
S = p->next;
free(p);
return true;
}
bool GetTop(LiStack S,int &e){
if(S == NULL){
return false;
}
e = S->data;
return true;
}
void PrintStack(LiStack S){
LinkNode *s=S;
while(s!=NULL){
printf("\t%d",s->data);
s=s->next;
}
printf("\n");
}
int main(){
LiStack S;
InitStack(S);
for(int i = 0; i < 5; i++){
Push(S,i);
}
PrintStack(S);
for(int i = 0; i < 5; i++){
int e;
bool flag = Pop(S,e);
if(flag){
printf("\t%d",e);
}
}
}
王道数据结构:栈的链式存储实现 不带头结点(个人)
于 2023-04-30 15:49:45 首次发布