请帮我看一下这个程序 ,好像是指针问题
//C++ programming language chapter 7 exercise 7#include <iostream>
#include <string>
using namespace std;
typedef struct Tnode {
string word;
int count;
Tnode *left;
Tnode *right;
};
Tnode* new_Tnode(string const &word) {
Tnode *node = new Tnode;
node->word = word;
node->count = 1;
node->left = node->right = 0;
return node;
}
void enter_word(Tnode* root, string const &word) {
//if (root!=0) {
if (root->left==NULL||root->right==NULL) {
// First search for the node in the tree:
Tnode *node = root;
while (1) {
int order = word.compare(node->word);
if (order==0) { // Word is in the tree already
++node->count;
break;
} else {
Tnode *&next = order<0? node->left
: node->right;
if (next==0) { // Word not yet in tree?
next = new_Tnode(word);
break;
} else
node = next;
}
}
} else { // Create the first node at the root
root = new_Tnode(word);
}
}
void write(ostream &output, Tnode *node,
bool indent, int spaces = 2) {
if (node) {
write(output, node->left, indent, spaces+2);
if (indent)
for (int k = 0; k!=spaces; ++k)
cout << ' ';
output << node->word
<< " (" << node->count << ")\n";
write(output, node->right, indent, spaces+2);
}
}
int main() {
cout << "Enter words terminated by \"$done\"\n";
Tnode* tree;
while (1) {
string word;
cin >> word;
if (word=="$done") {
break;
}
enter_word(tree, word);
}
write(cout, tree, true /* do indent */);
return 0;
}
运行时错误,请帮忙解答一下
用代码的格式发布;
自己尝试注释一下吧!
这样的帖子大牛是不会理会滴... 支持楼上。。。。还要花时间去看,去分析,代码又那么长,一般没心情看。
写代码的习惯很重要。 多谢各位提醒 有到论坛发帖求助等回复还不如自己下断点一部一部去找出问题的所在
页:
[1]