11.27實驗一
阿新 • • 發佈:2020-11-27
#include <bits/stdc++.h> using namespace std; const int MaxSize = 100000; typedef char ElemType; typedef struct{ ElemType data[MaxSize]; int top; }SqStack; void InitStack(SqStack *& s){ s = (SqStack * )malloc(sizeof(SqStack)); s -> top = -1; } void DestoryStack(SqStack *& s){ free(s); } bool StackEmpty(SqStack * s){ return (s -> top == -1); } bool Push(SqStack *& s, ElemType e){ if(s -> top == MaxSize - 1) return false; s -> top ++; s -> data[s -> top] = e; return true; } bool Pop(SqStack *& s, ElemType &e){ if(s -> top == -1) return false; e = s -> data[s -> top]; 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); for(char i = 'a'; i <= 'e'; ++i) Push(s, i); if(StackEmpty(s)) cout << "空的" << endl; else cout << "不是空的" << endl; puts("輸出棧"); while(!StackEmpty(s)){ char e; Pop(s, e); printf("%c\n", e); } if(StackEmpty(s)) cout << "空的" << endl; else cout << "不是空的" << endl; DestoryStack(s); return 0; }