#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;}
新闻热点
疑难解答