#include <stdlib.h> #include <stdio.h> #include <stdbool.h> #define ElementType int #define ERROR -1 typedef int Position; struct SNode{ ElementType *Data; Position Top; int MaxSize; }; typedef struct SNode *Stack; Stack CreateStack(int MaxSize){ Stack s = (Stack)malloc(sizeof(struct SNode)); s->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType)); s->Top = -1; s->MaxSize = MaxSize; return s; } bool IsFull(Stack S){ return (S->Top == S->MaxSize-1); } bool Push(Stack S, ElementType X){ if(IsFull(S)){ printf("Stack is full\n"); return false; }else{ S->Data[++S->Top] = X; return true; } } bool IsEmpty(Stack S){ return (S->Top == -1); } ElementType Pop(Stack S){ if(IsEmpty(S)){ printf("Stack is Empty\n"); return ERROR; }else{ return (S->Data[S->Top--]); } } int main(){ Stack S = CreateStack(10); if(IsEmpty(S)){ printf("S is Empty\n"); } Push(S, 1); Push(S, 2); ElementType a = Pop(S); printf("pop is %d\n",a); a = Pop(S); printf("pop is %d\n",a); Pop(S); return 0; }
标签:return,实现,Top,Stack,int,MaxSize,堆栈,ElementType,顺序存储 From: https://www.cnblogs.com/xinrenbool/p/16879201.html