C语言 实现顺序栈

xiaoxiao2025-11-20  16

实现代码

#include<stdio.h> #include<stdlib.h> #define MaxSize 10 typedef int ElemType; typedef struct{ ElemType data[MaxSize]; int top; }SqStack; void InitStack(SqStack *&s){ s = (SqStack *)malloc(sizeof(SqStack)); s->top=-1; } bool Push(SqStack *&s,ElemType e){ if(MaxSize-1==s->top) return false; s->data[++s->top]=e; return true; } bool Pop(SqStack *&s,ElemType &e){ if(s->top==-1) return false; e=s->data[s->top--]; return true; } bool GetTop(SqStack *s , ElemType &e){ if(s->top==-1) return false; e=s->data[s->top]; return true; } int main(){ SqStack *s; InitStack(s); Push(s,1); Push(s,2); Push(s,3); Push(s,34); for(int i=0;i<4;i++){ ElemType e; Pop(s,e); printf("%d\n",e); } }

 

转载请注明原文地址: https://www.6miu.com/read-5039972.html

最新回复(0)