/*
创建一个栈 判断栈满栈空 出入栈 查看栈顶元素
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct Stack
{
int *arr;//数组
int len;//栈的容量
int top;//栈顶指针
}Stack,*Sqstack;
//创建栈
Sqstack InitStack(Sqstack &S,int size)
{
S = (Sqstack)malloc(sizeof(Stack));
S->arr = (int *)malloc(sizeof(size));
S->len = size;
S->top = -1;
S->arr[0] = NULL;
return S;
}
bool StackEmpty(Sqstack &S)
{
if(S->top == -1)
{
return true;
}
else
{
return false;
}
}
bool Stackfull(Sqstack &S)
{
if(S->top+1 >= S->len )
{
return true;
}
else
{
return false;
}
}
bool push(Sqstack &S,int value)
{
if(Stackfull(S))
{
return false;
}
S->top+1;
S->arr[S->top] = value;
return true;
}
bool pop(Sqstack &S,int &value)
{
if(StackEmpty(S))
{
return false;
}
value = S->arr[S->top];
S->top--;
return true;
}
bool Getstack(Sqstack &S,int &value)
{
if(StackEmpty(S))
{
return false;
}
value = S->arr[S->top];
return true;
}
void Display(Sqstack &S)
{
for(S->top;S->top > -1;S->top--)
{
printf("%d ",S->arr[S->top]);
}
}
int main()
{
Sqstack S1;
InitStack(S1,5);
push(S1,5);
push(S1,5);
Display(S1);
int x;
Getstack(S1,x);
printf("%d ",x);
}