`
kmplayer
  • 浏览: 508769 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

给出前序和中序序列,输出后序序列

阅读更多
1,给出一个实现代码:
#include <stdio.h>
#include <string.h>

void outputPro(char* pre, char* ord)
{
    int len = strlen(pre);
    if (len != strlen(ord))
    {
        printf("input error!\n");
        return;
    }
    if (len == 0)
    {
        return;
    }
    if (len == 1)
    {
        printf("%c ", *pre);
        return;
    }
    else
    {
        char* preLeft;
        char* preRight;
        char* ordLeft;
        char* ordRight;
        char* ordTmp = ord;
        char* preTmp = pre;
        char tmp = *pre;
        while (*ord != tmp)
        {
            ord++;
            *pre = *(pre + 1);
            pre++;
        }
        *pre = '\0';
        preLeft = preTmp;
        preRight = pre + 1;
        *ord = '\0';
        ordLeft = ordTmp;
        ordRight = ord + 1;
        outputPro(preLeft, ordLeft);
        outputPro(preRight, ordRight);
        /*
        printf("%s\n", preLeft);
        printf("%s\n", preRight);
        printf("%s\n", ordLeft);
        printf("%s\n", ordRight);
        */
        printf("%c ", tmp);
        return;
    }
}

int main()
{
    char pre[] = "abcdef";
    char ord[] = "abcedf";
    outputPro(pre, ord);
    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics