为什么子函数执行结束之后,返回主函数时,主函数不继续进行呢

代码如下
我的疑惑以注释的方式写在代码里了


#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <fstream>
#include <malloc.h>
#define max 100 
using namespace std;

struct BiTNode {      //二叉树结点类型
    int data;         //数据
    BiTNode *left;    //左子树指针
    BiTNode *right;   //右子树指针
};

BiTNode *Tree;      //本程序操作的二叉树根指针

void init1(BiTNode *Tree);
void init(BiTNode *Tree);
void inOrder(BiTNode *Tree);
BiTNode *Insert();

int main()
{
    char choice;
    while(1)
    {
        printf("菜单\n");//根据课程作业构建的菜单,这只是第一个功能... 
        printf("1.根据先序遍历字符串构建二叉树\n");//简陋菜单,见谅 
        printf("2. ...\n");
        printf("请输选择:");
        
        choice = getch();
        system("cls");
        
        switch(choice)
        {
            case '1': 
                init1(Tree);
                system("pause");//我感觉就是在这里停住了,执行完子函数后程序就没反应了 
                break; 
            case '0': 
                exit(0);
        }
    }
    return 0;
}

//初始化二叉树 
char str[101];  //存放输入的先序遍历字符串
int i;

void init1(BiTNode *Tree)
{
    printf("\n请输入先序遍历字符串: ");
    while(scanf("%s",str) != EOF)
    {
        i = 0;
        Tree = Insert();
        inOrder(Tree);
    }
 } 
BiTNode *Insert()
 {
     if(str[i] == '#')
     {
         i++;
         return NULL;
     }
    else
    {
        BiTNode *r = (BiTNode *)malloc(sizeof(BiTNode));
        r->data = str[i];
        r->left = NULL;
        r->right = NULL;
        i++;
        r->left = Insert();
        r->right = Insert();
        return r;
    }
 }
void inOrder(BiTNode *Tree)
{
    if(Tree->left != NULL)
        inOrder(Tree->left);
    printf("%c ",Tree->data);
    if(Tree->right != NULL)
        inOrder(Tree->right) ;
}

输入的"#"表示的是空,代表一颗空树

运行起来是这样子的

img

按回车也没反应,程序也不会结束

我想让这个程序执行完子函数后,清空屏幕再输出一遍菜单,让用户再继续选择操作

子函数参考了@薛定谔的猫ovo 的方法,根据先序遍历字符串构建二叉树。

把 void init1(BiTNode *Tree) 函数里,循环输入 while(scanf("%s",str) != EOF) 去掉 ,修改如下,供参考:

void init1(BiTNode* Tree)
{
    printf("\n请输入先序遍历字符串: ");
    scanf("%s", str);  //while (scanf("%s", str) != EOF) 修改
    
    i = 0;
    Tree = Insert();
    inOrder(Tree);
    
}

#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <fstream>
#include <malloc.h>
#define max 100 
using namespace std;

struct BiTNode {      //二叉树结点类型
    int data;         //数据
    BiTNode* left;    //左子树指针
    BiTNode* right;   //右子树指针
};

BiTNode* Tree;      //本程序操作的二叉树根指针

void init1(BiTNode* Tree);
void init(BiTNode* Tree);
void inOrder(BiTNode* Tree);
BiTNode* Insert();

int main()
{
    char choice;
    while (1)
    {
        printf("菜单\n");//根据课程作业构建的菜单,这只是第一个功能... 
        printf("1.根据先序遍历字符串构建二叉树\n");//简陋菜单,见谅 
        printf("2. ...\n");
        printf("请输选择:");

        choice = getch();
        system("cls");

        switch (choice)
        {
        case '1':
            init1(Tree);
            system("pause");//我感觉就是在这里停住了,执行完子函数后程序就没反应了 
            break;
        case '0':
            exit(0);
        }
    }
    return 0;
}

//初始化二叉树 
char str[101];  //存放输入的先序遍历字符串
int i;

void init1(BiTNode* Tree)
{
    printf("\n请输入先序遍历字符串: ");
    scanf("%s", str);  //while (scanf("%s", str) != EOF) 修改
    
    i = 0;
    Tree = Insert();
    inOrder(Tree);
    
}
BiTNode* Insert()
{
    if (str[i] == '#')
    {
        i++;
        return NULL;
    }
    else
    {
        BiTNode* r = (BiTNode*)malloc(sizeof(BiTNode));
        r->data = str[i];
        r->left = NULL;
        r->right = NULL;
        i++;
        r->left = Insert();
        r->right = Insert();
        return r;
    }
}
void inOrder(BiTNode* Tree)
{
    if (Tree->left != NULL)
        inOrder(Tree->left);
    printf("%c ", Tree->data);
    if (Tree->right != NULL)
        inOrder(Tree->right);
}

【相关推荐】



  • 这篇博客: 期末复习笔记——图中的 有1说1,这个大一在杭电刘老师的算法班学到的图存储方法让我记忆深刻,不完全是他的这个高逼格的名字,还有他的方便以及便于理解(真的挺好理解的OvO) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

    链式前向星有一个head数组,几个结点就有几个数,然后是edge结构数组(表数组),存储三个数值 to,dis,next 那么head数组的下标就可以理解为from

    刚开始head值都是-1当插入,from:1  to:2   dis:19   这样我们的head数组就能更新为edge数组的下标,edge的next也可以更新为head的值

     再加入,from:1  to:3   dis:23重复上述操作

      

      草稿而已,忽略字谢谢

    struct node
    {
        int to;
        int val;
        int next;
    }E[maxn << 2];
    
    void add(int from, int to, int w)
    {
        cnt++;
        E[cnt].to = to;
        E[cnt].val = w;
        E[cnt].next = head[from];
        head[from] = cnt;
    }
    
     for (int i = 1; i <= n; i++) {
            dis[i] = inf;
            cin >> x >> y;
            Vnode[i].data = { x,y };
            Vnode[i].name = s[i-1];
            head[i] = -1;
            vis[i] = 0;
        }
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            int u, v, w;
            cin >> u >> v >> w;
            add(u+1, v+1, w);
            add(v+1, u+1, w); 
        }

    这边我们利用链式前向星的遍历以及easyx画一张图的可视化:

    void printG(int n) {
        double xp, yp, xt, yt;
        for (int node = 1; node <= n; node++){
            setfillcolor(BLUE);
            settextcolor(WHITE);
            setbkmode(TRANSPARENT);
            fillcircle(Vnode[node].data.x, Vnode[node].data.y, 20);
            outtextxy(Vnode[node].data.x - 5, Vnode[node].data.y - 5, Vnode[node].name);
            double x = Vnode[node].data.x;
            double y = Vnode[node].data.y;
            for (int i = head[node]; i!=-1 ; i = E[i].next) {
                xt = Vnode[E[i].to].data.x, yt = Vnode[E[i].to].data.y;
    
                setfillcolor(BLUE);
                settextcolor(WHITE);
                setbkmode(TRANSPARENT);
                fillcircle(xt, yt, 20);
                outtextxy(xt - 5, yt - 5, Vnode[E[i].to].name);
               /* line(x, y, xt,yt);*/
               /* Sleep(5000);*/
                if (x < xt) {
                    for (xp = x, yp = y; xp <= xt;) {
                        xp += (xt - x) / 100, yp = ((y - yt) / (x - xt)) * (xp - x) + y;
                        line(x, y, xp, yp);
                        Sleep(15);
                    }
                }
                else if (x > xt) {
                    for (xp = x, yp = y; xp >= xt;) {
                        xp += (xt - x) / 100, yp = ((y - yt) / (x - xt)) * (xp - x) + y;
                        line(x, y, xp, yp);
                        Sleep(15);
                    }
                }
                settextcolor(RED);
    
                setbkmode(TRANSPARENT);
                char a[100];
                _stprintf(a, _T("%d"), E[i].val);
                outtextxy((xt+x)/2+10, (yt +y)/2+10, a);
    
            }
        }
    }

    过程很简单,就是画点和连线,和二叉树的动画一致


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^