[問題] 利用linked list作中序轉後序
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
DEV c++
問題:
老師出了一份作業
要使用stack資料結構並以linked list實作
去完成中序轉後序
小弟寫了之後compile沒有問題
但是程式卻跑不出來
希望大大們可以幫我看一下
#include<stdio.h>
#define no_of_operator 6
#define null 0
#define true 1
#define false 0
void create_stack(void);
void free_stack(void);
int empty(void);
void push(char stack_data);
char pop(void);
int prexedence(char op);
char operator[no_of_operator] = {'(','+','-','*','/',};
int priority[no_of_operator] = {0,1,1,2,2};
void infix_to_postfix(char infix[],char postfix[]);
struct node{
char data;
struct node *link;}NODE;
struct node *top;
struct node *past;
void create_stack(void){
top=(struct node*)malloc(sizeof(struct node));
top->link=null;
past->link=null;
}
int empty(void){
if (top->link==null)
return 1;
else
return 0;
}
void push(char stack_data){
struct node *new_node;
new_node=(struct node*)malloc(sizeof(struct node));
past->link=top->link;
new_node->data=stack_data;
new_node->link=top->link;
top->link=new_node;
}
char pop(void){
char stack_data;
struct node *z;
z=top->link;
top->link=past->link;
stack_data=z->data;
free(z);
}
int prexedence(char op){
int i;
for(i=0;i<no_of_operator;i++)
if(operator[i]==op)
return priority[i];
}
void infix_to_postfix(char infix[],char postfix[]){
int i=0,j=-1;
char x,y;
while((x=infix[i++]) != '\0'){
switch(x){
case'+':
case'-':
case'*':
case'/':y=top->data;
while(prexedence(y)>=prexedence(x)){
postfix[++j]= pop();
y=top->data ;
}
push(x);
break;
default:
postfix[++j]=x;
}
}
while(top->link==null)
postfix[++j]=pop();
postfix[++j]='\0';
}
int main(void){
char infix[100], postfix[100];
FILE *fp=fopen("input.txt", "r");
FILE *fp_eq=fopen("equation.txt","w");
while(fscanf(fp,"%s",infix)!=EOF)
{
if(infix[0]=='0')
break;
else{
infix_to_postfix(infix,postfix);
printf("\n中置運算式 : %s ",infix);
printf("\n後置運算式 : %s \n",postfix);
fprintf(fp_eq,"\n後置運算式 : %s \n",postfix);
}
}
system("PAUSE");
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.38.166.87
※ 編輯: callmeshan 來自: 114.38.166.87 (12/01 01:21)
※ 編輯: callmeshan 來自: 114.38.166.87 (12/01 03:05)
→
12/01 03:53, , 1F
12/01 03:53, 1F
→
12/01 04:24, , 2F
12/01 04:24, 2F
推
12/01 05:05, , 3F
12/01 05:05, 3F
→
12/01 05:05, , 4F
12/01 05:05, 4F
→
12/01 09:48, , 5F
12/01 09:48, 5F