顺序栈
Posted linkmust
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顺序栈相关的知识,希望对你有一定的参考价值。
#include "stdafx.h" #include<iostream> using namespace std; #define MAXSTACK 10 typedef int StackEntry; typedef struct node { StackEntry entry[MAXSTACK]; int top; }SeqStack; typedef enum Status { success, fail, fatal, rangeerror, overflow }Status; SeqStack * init_SeqStack() { SeqStack *s; s = (SeqStack*)malloc(sizeof(SeqStack)); s->top = -1; return s; } int Empty_SeqStack(SeqStack s) { if (s.top == -1)return 1; else return 0; } int Push_SeqStack(SeqStack *s, StackEntry elem) { if (s->top == MAXSTACK - 1) return 0; else { s->top++; s->entry[s->top] = elem; return 1; } } int Pop_SeqStack(SeqStack *s, StackEntry *elem) { if (s->top == -1)return 0; else { *elem = s->entry[s->top]; s->top--; return 1; } } StackEntry Top_SeqStack(SeqStack *s) { if (s->top == -1)return 0; else { return s->entry[s->top]; } } int main() { SeqStack * s = init_SeqStack(); int num; for (int i = 0; i < MAXSTACK - 1; i++) { cin >> num; Push_SeqStack(s, num); } StackEntry elem; Pop_SeqStack(s, &elem); cout << elem << endl; cout << Top_SeqStack(s) << endl; system("pause"); return EXIT_SUCCESS; }
以上是关于顺序栈的主要内容,如果未能解决你的问题,请参考以下文章