#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int top;
}SqStack;
/*初始化栈*/
void InitStack(SqStack* s){
s->top = -1;
}
void DestroyStack(SqStack* s)
{
free(s);
}
bool Push(SqStack* s, int data)
{
if (s->top == MaxSize - 1)
{
printf("栈满!");
return false;
}
s->top++;
s->data[s->top] = data;
return true;
}
bool Pop(SqStack* s)
{
if (s->top == -1)
{
printf("栈为空!");
return false;
}
s->top--;
return true;
}
int GetTop(SqStack* s)
{
if (s->top == -1)
{
printf("栈为空!");
return 0;
}
return s->data[s->top];
}
int main()
{
SqStack stk;
InitStack(&stk);
Push(&stk, 1);
Push(&stk, 9);
for (int i = 0; i <= stk.top; i++)
{
printf("%d", stk.data[i]);
}
Pop(&stk);
Pop(&stk);
return 0;
}
下面是运行结果