问题遇到的现象和发生背景
初学数据结构,堆栈题目全部TLE
用代码块功能插入代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define ERROR -1
#define OK 1
typedef struct stack{
int arr[MAXSIZE];
int top;
}*Stack;
int Push(int n,struct stack *s);
int Pop(struct stack *s);
int main(void){
Stack s = (Stack)malloc(sizeof(struct stack));
s->top=-1;
int arr[10];
int a,b,input;
int num=0;
scanf("%c",&input);
while(input!='@'){
if(input>='0'&&input<='9'){
num+=num*10+input-'0';
}
if(input=='.'){
Push(num,s);
num=0;
}
if(input=='+'){
a=Pop(s);
b=Pop(s);
Push(a+b,s);
}else if(input=='-'){
a=Pop(s);
b=Pop(s);
Push(b-a,s);
}else if(input=='*'){
a=Pop(s);
b=Pop(s);
Push(a*b,s);
}else if(input=='/'){
a=Pop(s);
b=Pop(s);
Push(b/a,s);
}
scanf("%c",&input);
}
printf("%d",Pop(s));
return 0;
}
int Push(int n,struct stack *s){
if(s->top==MAXSIZE-1){
return ERROR;
}else{
s->arr[++(s->top)]=n;
}
return OK;
}
int Pop(struct stack *s){
if(s->top==-1){
return ERROR;
}else{
return s->arr[(s->top)--];
}
}
运行结果及报错内容
我的解答思路和尝试过的方法
是不是堆栈导致的?