数据结构----栈
栈 先进后出 必须要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("空的\n");
return true;
}else{
printf("不是空的\n");
return false;
}
}
bool isFull(stack *mystack)
{
if(mystack->t==MAXSIZE-1){
printf("满了\n");
return true;
}else{
printf("不是满的\n");
return false;
}
}
void push(stack *mystack,int x){
if(isFull(mystack)){
printf("无法添加\n");
}else{
mystack->t ++;
mystack->stack_list[mystack->t] = x;
printf("添加了%d\n",x);
}
}
int pop(stack *mystack){
int x;
if(isEmpty(mystack)){
printf("无法取出\n");
}else{
x = mystack->stack_list[mystack->t];
mystack->t --;
printf("取出了%d\n",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;
}

浙公网安备 33010602011771号