c**********e 发帖数: 2007 | 1 void reversePrint(Node* head) {
if(head=NULL) {
cout << "Empty List. No node to print." << endl;
return;
}
if(head->next=NULL) {
cout << head->data << endl;
return;
} else {
Node* temp=head;
head=temp->next;
reversePrint(head);
cout << temp->data << endl;
return;
}
} |
s*****y 发帖数: 897 | 2 .....
did not look into detail, but the first look find that
head = NULL...
【在 c**********e 的大作中提到】 : void reversePrint(Node* head) { : if(head=NULL) { : cout << "Empty List. No node to print." << endl; : return; : } : if(head->next=NULL) { : cout << head->data << endl; : return; : } else { : Node* temp=head;
|
c**********e 发帖数: 2007 | 3 You are right. Thanks. The old == versus = problem.
【在 s*****y 的大作中提到】 : ..... : did not look into detail, but the first look find that : head = NULL...
|
c**********e 发帖数: 2007 | 4 发现不定义新节点也没问题。
void reversePrint(Node* head) {
if(head==NULL) {
cout << "Empty List. No node to print." << endl;
return;
}
if(head->next==NULL) {
cout << head->data << endl;
return;
} else {
reversePrint(head->next);
cout << head->data << endl;
return;
}
} |
c**y 发帖数: 2282 | 5 你这Q+数字玩的是什么啊
【在 c**********e 的大作中提到】 : 发现不定义新节点也没问题。 : void reversePrint(Node* head) { : if(head==NULL) { : cout << "Empty List. No node to print." << endl; : return; : } : if(head->next==NULL) { : cout << head->data << endl; : return; : } else {
|
c**********e 发帖数: 2007 | 6 开始是 Brainbench 的练习题。后来加的就是我自己想问的题目了,或者是面试题,或
者是我自己遇到的问题。
【在 c**y 的大作中提到】 : 你这Q+数字玩的是什么啊
|
R****i 发帖数: 104 | 7 Only the last one will be printed.
【在 c**********e 的大作中提到】 : void reversePrint(Node* head) { : if(head=NULL) { : cout << "Empty List. No node to print." << endl; : return; : } : if(head->next=NULL) { : cout << head->data << endl; : return; : } else { : Node* temp=head;
|
c**********e 发帖数: 2007 | 8 Did you try it? I tried it. It printed all.
【在 R****i 的大作中提到】 : Only the last one will be printed.
|
t******g 发帖数: 252 | 9 "Empty List. No node to print"
That will always be printed out first. |
t******g 发帖数: 252 | 10 Actually no. This won't happen. But it's better to have this out of the
recursion.
【在 t******g 的大作中提到】 : "Empty List. No node to print" : That will always be printed out first.
|
|
|
d**e 发帖数: 6098 | 11 你发现有什么错?感觉没什么错误.
但如果说以整个code来看,其实后面两个return是多余的,可删去,另外print的message
有bug,head是空就说是empty list,但因为用了递归,head->next为null时,不代表它是
empty list
手痒写了个,但没验证...
void reversePrint(Node * head){
if(!head){
cout << "the end ..." << endl; // print some message here if needed...
return;
}
reversePrint(head->next);
cout << head->data << endl;
}
【在 c**********e 的大作中提到】 : void reversePrint(Node* head) { : if(head=NULL) { : cout << "Empty List. No node to print." << endl; : return; : } : if(head->next=NULL) { : cout << head->data << endl; : return; : } else { : Node* temp=head;
|
c**********e 发帖数: 2007 | 12 use head==NULL, not head=NULL (this is always true).
【在 t******g 的大作中提到】 : "Empty List. No node to print" : That will always be printed out first.
|
c**********e 发帖数: 2007 | 13
你是对的。后面那两个return语句是多余的,因为程序会自然运行。
这个message应该没错。因为只有这个list为空时,这个message才会打印。否则第二个
return处理最终状态。
验证了一下,你是对的。并且你的处理终止状态比我的简单。
...
【在 d**e 的大作中提到】 : 你发现有什么错?感觉没什么错误. : 但如果说以整个code来看,其实后面两个return是多余的,可删去,另外print的message : 有bug,head是空就说是empty list,但因为用了递归,head->next为null时,不代表它是 : empty list : 手痒写了个,但没验证... : void reversePrint(Node * head){ : if(!head){ : cout << "the end ..." << endl; // print some message here if needed... : return; : }
|
c**********e 发帖数: 2007 | 14 第二个return语句保证了message不会被打印,除非原来的list为空。
【在 t******g 的大作中提到】 : Actually no. This won't happen. But it's better to have this out of the : recursion.
|
d**e 发帖数: 6098 | 15 噢....谢谢,那你原来上面的return是有用的,我当时想的时候是把两个return去掉后才
觉得那个message有错.
【在 c**********e 的大作中提到】 : 第二个return语句保证了message不会被打印,除非原来的list为空。
|
c**********e 发帖数: 2007 | 16 完全不用return也是可以的。你的可以改成
void reversePrint(Node * head){
if(!head){
cout << "the end ..." << endl; // print some message here if needed...
} else {
reversePrint(head->next);
cout << head->data << endl;
}
}
我的可以改成
void reversePrint2(Node* head) {
if(head==NULL) {
cout << "Empty List. No node to print." << endl;
} else if(head->next==NULL) {
cout << head->data << " reversePrint2" << endl;
} else {
reversePrint2(head->next);
cout << head->data << " reversePrint2" << endl;
}
}
message
...
【在 d**e 的大作中提到】 : 你发现有什么错?感觉没什么错误. : 但如果说以整个code来看,其实后面两个return是多余的,可删去,另外print的message : 有bug,head是空就说是empty list,但因为用了递归,head->next为null时,不代表它是 : empty list : 手痒写了个,但没验证... : void reversePrint(Node * head){ : if(!head){ : cout << "the end ..." << endl; // print some message here if needed... : return; : }
|
s*****y 发帖数: 897 | 17 Done's code is better.
void reversePrint(Node * head){
if(!head){
cout << "the end ..." << endl; // print some message here if needed..
.
} else {
reversePrint(head->next);
cout << head->data << endl;
}
}
Suppose you write this function and this function is very very long. Also, m
aybe there are a lot of #ifdef, #endif inside this function to support multi
ple platform. People look at this kind of code often get loss.
Then later on, another engineer jump in and modify this function by adding c
ode behind here:
} else {
reversePrint(head->next);
cout << head->data << endl;
}
/*****************/
add code here
/*****************/
}
But he did not pay attention to the code you write previously as the functio
n is so big. Normally, it should just return when the head is NULL. But now
it continue excuting without return.....Bug happens then.
...
【在 c**********e 的大作中提到】 : 完全不用return也是可以的。你的可以改成 : void reversePrint(Node * head){ : if(!head){ : cout << "the end ..." << endl; // print some message here if needed... : } else { : reversePrint(head->next); : cout << head->data << endl; : } : } : 我的可以改成
|
c**********e 发帖数: 2007 | 18 I agree. It is better to use return than to use if else.
..
【在 s*****y 的大作中提到】 : Done's code is better. : void reversePrint(Node * head){ : if(!head){ : cout << "the end ..." << endl; // print some message here if needed.. : . : } else { : reversePrint(head->next); : cout << head->data << endl; : } : }
|
l******u 发帖数: 22 | 19 It should work, but just is not very simple. We may be try following more
simple codes:
void reversePrint(Node *head){
if(head == NULL) return;
reversePrint(head->next);
cout << head->data << "\t";
} |
c**********e 发帖数: 2007 | 20 It is the same as done's.
【在 l******u 的大作中提到】 : It should work, but just is not very simple. We may be try following more : simple codes: : void reversePrint(Node *head){ : if(head == NULL) return; : reversePrint(head->next); : cout << head->data << "\t"; : }
|