#include<stdio.h> #include<stdlib.h> #define MaxSize 10 typedef struct node* SeqStack; typedef char ElementType; struct node { ElementType data[MaxSize]; int top; //標記棧頂數據 }; //初始化 void InitNode(SeqStack *L) { (*L) = (SeqStack)malloc(sizeof(struct node)); (*L)->top = -1; } //進棧 void PushStack(SeqStack L, ElementType x) { if (L->top == MaxSize - 1) { printf("滿了"); } else { L->top++; //入棧所以加1 L->data[L->top] = x; } } //出棧 void PopStack(SeqStack L, ElementType *x) { if (L->top == -1) { printf("空的"); } else { *x = L->data[L->top]; L->top--; } } //遍歷輸出 void PrintNode(SeqStack L) { for (int i = 0; i <= L->top; i++) { printf("%c", L->data[i]); } printf("\n"); } int main() { SeqStack s; ElementType c; ElementType* y; y = &c; //y指向c,為了出棧用 InitNode(&s); printf("輸入入棧數據"); scanf("%c", &c); while (c != '\n') { PushStack(s, c); scanf("%c", &c); } PrintNode(s); PopStack(s, y); printf("出棧元素是%c\n", *y); PrintNode(s); }