From f44c171de7de01521bd0f20c54353ccc362b119d Mon Sep 17 00:00:00 2001 From: ViolentAyang <76544389+ViolentAyang@users.noreply.github.com> Date: Sat, 19 Mar 2022 21:27:51 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E5=87=BA=E6=A0=88=E6=93=8D=E4=BD=9C.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 栈/出栈操作.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 栈/出栈操作.c diff --git a/栈/出栈操作.c b/栈/出栈操作.c new file mode 100644 index 0000000..5b81490 --- /dev/null +++ b/栈/出栈操作.c @@ -0,0 +1,54 @@ +#include +#include +#include +#define MaxSize 10 + +typedef struct{ + int data[MaxSize]; //静态数组存放栈中元素 + int top; //栈顶指针 +}SqStack; + +//初始化栈 +void InitStack(SqStack *S){ + S->top = -1; +} +//新元素进栈 +bool Push(SqStack *S,int x){ + if(S->top==MaxSize-1){ + return false; + } + S->top += 1; + S->data[S->top] = x; + return true; +} +//出栈操作 +int Pop(SqStack *S,int x){ + if(S->top==-1){ + return false; + } + x = S->data[S->top]; + S->top -= 1; + return x; +} +//打印 +void PrintStack(SqStack S){ + if(S.top==-1){ + printf("栈为空!"); + } + for(int i=0;i<=S.top;i++){ + printf("%d\n",S.data[i]); + } +} +int main(){ + SqStack S; + InitStack(&S); + Push(&S,1); + Push(&S,2); + Push(&S,3); + Push(&S,4); + Push(&S,5); + PrintStack(S); + printf("%d\n",Pop(&S,-1)); + PrintStack(S); + return 0; +}