C語言順序棧實現
阿新 • • 發佈:2020-12-21
#include<stdio.h> #define MAXN 20//初始順序棧的空間為20 typedef int SElemType; typedef int Status; typedef struct SqStack{ char stack[MAXN];//假設棧的資料結構為char int top = -1;//初始化棧頂指標,以0為結束符 }SqStack; SqStack a; Status push(char temp) { if (a.top == MAXN) { printf("棧滿了別來了\n");return 0; } else { a.top++; a.stack[a.top] = temp; return 1; } } char pop() { if (a.top == -1) { return 0; } else { char temp = a.stack[a.top];//獲取當前頭指向的位置 a.top--;//頭下移 return temp;//返回 } } void test() { chara[5]; //入棧 for (int i = 0; i < 5; i++) { a[i] = i + 80; push(a[i]); printf("%c", a[i]); } printf("\n"); //出棧 for (int i = 0; i < 5; i++) { a[i] = pop(); printf("%c", a[i]); } } int main() { test(); return 0; }