#include <stdio.h>#include <malloc.h>#define ERROR false#define OK true#define MAXSIZE 5typedef int sElemType;typedef struct { sElemType data[MAXSIZE]; int top;} sqStack;//壓棧bool Push(sqStack* stack, sElemType e) { if (stack->top >= MAXSIZE - 1) { PRintf("棧以滿/n"); return ERROR; } stack->top++; stack->data[stack->top] = e; return OK;}//出棧bool Pop(sqStack* stack, sElemType* e) { if (stack->top == -1) { printf("空棧/n"); return ERROR; } *e = stack->data[stack->top]; stack->top--; return OK;}int main() { sqStack* stack = (sqStack*) malloc(sizeof(sqStack)); stack->top = -1; //初始化棧頂指針 for (int i = 0; i < 5; i++) { Push(stack, i); } sElemType e = 0; for (int i = 0; i < 5; i++) { if (Pop(stack, &e)) printf("%d/n", e); } return 0;}
新聞熱點
疑難解答