#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int Position;
typedef int ElementType;
typedef struct SNode* PtrToSNode;
struct SNode {
ElementType* Data;
Position Top;
int MaxSize;
};
typedef PtrToSNode Stack;
int IsFull(Stack a);
Stack CreateSNode(int Size);
bool Push(Stack a, ElementType n);
ElementType Pop(Stack a);
int main()
{
Stack a = CreateSNode(5);
Push(a, 3);
Push(a, 4);
printf("%d", Pop(a));
printf("%d", Pop(a));
}
Stack CreateSNode(int Size)
{
Stack a = (Stack)malloc(sizeof(struct SNode));
a->Data = (ElementType*)malloc(sizeof(ElementType) * Size);
a->Top = -1;//栈为空
a->MaxSize = Size;
return a;
}
int IsFull(Stack a)
{
return(a->Top == (a->MaxSize-1));
}
bool Push(Stack a,ElementType n)
{
if (!IsFull(a))
{
a->Data[++(a->Top)] = n;
return true;
}
else
{
printf("Stack is full\n");
return false;
}
}
bool IsEmpty(Stack a)
{
return(a->Top == -1);
}
ElementType Pop(Stack a)
{
if (!IsEmpty(a))
{
return(a->Data[(a->Top)--]);
}
else
{
printf("Stack is empty!\n");
return -1;//-1不放进去
}
}