| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖, 1 人收藏
标题:二叉链表存储结构的问题
只看楼主 加入收藏
nanboxian
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-12-28
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:2 
二叉链表存储结构的问题
#include "whead.h"
#include <stdio.h>
#include <malloc.h>
#include <iostream>


#define MAX_TREE_SIZE 100


typedef char TElemType;
//typedef TElemType SqBiTree[MAX_TREE_SIZE];

typedef struct BiTreeNode {
    TElemType data;
    struct BiTreeNode *lchild;
    struct BiTreeNode *rchild;
}BiTreeNode, *BiTree;


BiTree tm;
Status visit(TElemType v) {
    printf("node==", v);
    printf("\n");
    return OK;
}

Status createBiTreePre(BiTree t) {
    char ch;
    printf("Input char :");
    scanf("%c" ,&ch);
    printf("\n");
    if(ch == ' ')
        t = NULL;
    else {
        if(!(t = (BiTreeNode*)malloc(sizeof(TElemType))))
            exit(OVERFLOW);
        t->data = ch;
        createBiTreePre(t->lchild);
        createBiTreePre(t->rchild);
    }
    return OK;
}

Status traversePre(BiTree t, Status (*visit)(TElemType e)) {
    if(t){
        if(t->data)
            if(traversePre(t->lchild, visit))
                if(traversePre(t->rchild,visit))
                    return OK;
        return ERROR;
    }
    else
        return OK;
}



#include "BinaryTree.h"

void main() {   
    createBiTreePre(tm);
    traversePre(tm, visit);
}

为什么第一次createBiTreePre(t->lchild)无法输入,第二次就可以,第三次又不行。。。。。以此类推


搜索更多相关主题的帖子: include return visit 
2010-12-28 14:25
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:20 
    printf("Input char :");
    scanf("%c" ,&ch);
    getchar();
    printf("\n");
2010-12-28 18:29
nanboxian
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-12-28
收藏
得分:0 
回复 楼主 nanboxian
谢谢版主,正解。我还有比较详细的。如果谁有需要就看看

because system-Buffer of stdin

first Time you type such command in DOS, and you press [Enter] key at end of DOS-command

Collapse

1.exe //assume your program called "1.exe"

but problem comes, the Enter-key's value still has been left in SyS-buffer-input.

Collapse

printf("Input char :");
scanf("%c" ,&ch); // first time receive Enter-value from Buffer
printf("\n");

Scanf() will receive the Enter-value from system-Buffer,which is inputed last time. so scanf() already executed before you wanted input.
and created leftchild-Node.

and code continue execute to create the next left-child

The Second time
Collapse

printf("Input char :");
 
//because the last time the enter-value was got by last scanf.
//the system-buffer was cleared. this time scanf() is waiting for you input
 
scanf("%c" ,&ch);
printf("\n");

The solve. you could add a getch() before call scanf, it could receive Enter-key which has been left at last Inputing

or you could call fflush(stdin), it could clear system-buffer of stdin.

just like following code
Collapse

printf("Input char :") ;
getch() ;
scanf("%c",&ch) ;
printf("\n") ;

BY   tanakahitori

2010-12-28 20:52
快速回复:二叉链表存储结构的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035993 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved