自己写的两个C语言程序,功能如题,如果有什么问题,请大家留言!!!
开发平台:linux;vim;gcc
已知先序,中序,求后序:
#include<stdio.h>
#include<string.h>
#define MAX 20
char str[MAX]={'/0'};
int main()
{
void postorder(char *str1,char *str2);
char str1[MAX]={'/0'};
char str2[MAX]={'/0'};
int i;
printf("please input the preorder:/n");
scanf("%s%*c",str1);
printf("please input the inorder:/n");
scanf("%s%*c",str2);
postorder(str1,str2);
printf("the postorder is:/n");
for(i=0;str[i]=='/0';i++);
puts(str+i);
}
void postorder(char *str1,char *str2)
{
static int num= MAX-1;
int i,j;
char str3[MAX]={'/0'};
char str4[MAX]={'/0'};
char str5[MAX]={'/0'};
char str6[MAX]={'/0'};
if(str1[0]=='/0'||str2[0]=='/0')
return;
str[num--]=str1[0];
i=0;
j=0;
while(str2[i]!=str1[0])
str3[j++]=str2[i++];
i++;
j=0;
while(str2[i]!='/0')
str4[j++]=str2[i++];
for(i=0;i<strlen(str3);i++)
str5[i]=str1[i+1];
for(j=0;j<strlen(str4);j++)
str6[j]=str1[++i];
postorder(str6,str4);
postorder(str5,str3);
}
已知后序,中序,求先序:
#include<stdio.h>
#include<string.h>
#define MAX 20
char str[MAX]={'/0'};
int main()
{
void postorder(char *str1,char *str2);
char str1[MAX]={'/0'};
char str2[MAX]={'/0'};
int i;
printf("please input the inorder:/n");
scanf("%s%*c",str1);
printf("please input the postorder:/n");
scanf("%s%*c",str2);
postorder(str1,str2);
printf("the preorder is:/n");
puts(str);
}
void postorder(char *str1,char *str2)
{
static num=0;
int i,j,position;
char str3[MAX]={'/0'};
char str4[MAX]={'/0'};
char str5[MAX]={'/0'};
char str6[MAX]={'/0'};
if(str1[0]=='/0'||str2[0]=='/0')
return;
position=strlen(str2)-1;
str[num++]=str2[position];
i=0;
j=0;
while(str1[i]!=str2[position])
str3[j++]=str1[i++];
i++;
j=0;
while(str1[i]!='/0')
str4[j++]=str1[i++];
for(i=0;i<strlen(str3);i++)
str5[i]=str2[i];
for(j=0;j<strlen(str4);j++)
str6[j]=str2[i++];
postorder(str3,str5);
postorder(str4,str6);
}