栈 先进后出 必须要2个方法 分别是pop 和push 方法
计算器 可以使用栈来实现
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXSIZE 5 struct stack{ int t; int stack_list[MAXSIZE]; }; typedef struct stack stack; stack *create_stack(){ stack *mystack = NULL; mystack = (stack *)malloc(sizeof(stack)); mystack->t = -1; return mystack; } bool isEmpty(stack *mystack) { if(mystack->t==-1){ printf("空的 "); return true; }else{ printf("不是空的 "); return false; } } bool isFull(stack *mystack) { if(mystack->t==MAXSIZE-1){ printf("满了 "); return true; }else{ printf("不是满的 "); return false; } } void push(stack *mystack,int x){ if(isFull(mystack)){ printf("无法添加 "); }else{ mystack->t ++; mystack->stack_list[mystack->t] = x; printf("添加了%d ",x); } } int pop(stack *mystack){ int x; if(isEmpty(mystack)){ printf("无法取出 "); }else{ x = mystack->stack_list[mystack->t]; mystack->t --; printf("取出了%d ",x); return x; } } int top(stack *mystack){ return mystack->stack_list[mystack->t]; } int main(){ stack *mystack = create_stack(); int a,b; isEmpty(mystack); isFull(mystack); push(mystack,1); push(mystack,2); pop(mystack); return 0; }