#include <stdio.h>
#define MaxSize 10
typedef struct {
int data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack *s) {
s->top = -1;
}
int Push(SqStack *s, int e) {
if (s->top == MaxSize-1) return -1;
s->data[++s->top] = e;
return 1;
}
int Pop(SqStack *s, int *e) {
if (s->top == -1) return -1;
*e = s->data[s->top--];
return 1;
}
int GetTop(SqStack s, int *e) {
if (s.top == -1) return -1;
*e = s.data[s.top];
return 1;
}
void StackPrintf(SqStack s) {
for (int i = s.top; i > -1; i--) {
printf("%i\n", s.data[i]);
}
}
int main() {
SqStack s;
InitStack(&s);
Push(&s, 23);
Push(&s, 3);
Push(&s, 45);
Push(&s, 78);
StackPrintf(s);
int e = -1;
Pop(&s, &e);
Pop(&s, &e);
GetTop(s, &e);
printf("%i\n", e);
return 0;
}
顺序栈
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- C#数据结构:顺序栈的两栈共享(双端栈) 栈的应用非常广泛,经常会出现在一个程序中需要同时使用多个栈的情况。若使用...
- //顺序栈入栈,取栈顶元素,出栈 #include #include #include #define MAX...
- //顺序栈初始化,判栈空,进栈,读栈顶元素,出栈 include <stdio.h> include <stdli...