#include<stdio.h>
#define MaxSize 10
typedef struct{
int data[MaxSize];
int top;
}SqStack;
//初始化一个栈
void InitStack(SqStack *S)
{
S->top = -1;
}
//判断栈是否为空
bool StackEmpty(SqStack *S)
{
if (S->top = -1)
return true;
else
return false;
}
//进栈
bool Push(SqStack *S , int x)
{
if (S->top = MaxSize-1)
return false; //栈满
S->top = S->top + 1;
S->data[S->top] = x;
return true;
}
//出栈
bool Pop(SqStack *S , int *x)
{
if (S->top = -1)
return false;
*x = S->data[S->top];
S->top = S->top -1;
return true;
}
//读取栈顶元素
bool GetTop (SqStack *S , int *x)
{
if (S->top = -1)
return false;
*x = S->data[S->top];
return true;
}
int main()
{
int x,s;
SqStack *S;
InitStack(S);
StackEmpty(S);
Push(S,x);
Pop(S,&s);
GetTop(S,&s);
return 0;
}
栈的线性存储
最新推荐文章于 2024-07-22 20:42:27 发布