#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct StNode{
ElemType data;
struct StNode *next;
}StNode,*LinkSt;
//初始化栈
void InitStack(LinkSt &S)
{
S = NULL;
}
//判断栈空
bool StackEmpty(LinkSt S)
{
if(!S)
return true;
else
return false;
}
//入栈
bool Push(LinkSt &S,ElemType x)
{
StNode *t = (StNode*)malloc(sizeof(StNode));
t->data = x;
t->next = S;
S = t;
return true;
}
//出栈
bool Pop(LinkSt &S,ElemType &x)
{
if(!S)
return false;
StNode *t = (StNode*)malloc(sizeof(StNode));
t = S;
x = t->data;
S = S->next;
return true;
}
//读取栈顶元素
bool GetTop(LinkSt S,ElemType &x)
{
if(!S)
return false;
x = S->data;
return true;
}
void main()
{
ElemType topVal;
LinkSt S;
InitStack(S);
Push(S,1);
Push(S,2);
Push(S,3);
Pop(S,topVal);
Pop(S,topVal);
GetTop(S,topVal);
printf("%d\n",topVal);
}
链栈的基本操作
最新推荐文章于 2024-06-12 09:31:30 发布