由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - [合集] C++ Q76: singly linked list -- 这个逆序打印有什么错?
相关主题
C++ Q76: singly linked list -- 这个逆序打印有什么错?a singly-linked list question
一道Linked List题发个面试coding题,攒人品
how to judge a linked list is palindrome?问问careerup书上的一道题:
remove a node (and its memory) from a doubly linked listHow can one determine whether a singly linked list has a cycle?
求推荐学习recursive 算法的资料[合集] H1B被裁的概率
问道面试题判断一个linked list是不是palindrome
实现next_permutation大家来看看这个CC150的题
问下嵌入式/DSP软件开发面试也需要刷题么?Amazon电面分享
相关话题的讨论汇总
话题: c++话题: q76话题: 逆序
进入JobHunting版参与讨论
1 (共1页)
S**I
发帖数: 15689
1
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 4 20:25:58 2011, 美东) 提到:
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;
}
}
☆─────────────────────────────────────☆
speeddy (Wade) 于 (Sat Jun 4 20:30:33 2011, 美东) 提到:
.....
did not look into detail, but the first look find that
head = NULL...

☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 4 20:49:01 2011, 美东) 提到:
You are right. Thanks. The old == versus = problem.
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 4 20:54:13 2011, 美东) 提到:
发现不定义新节点也没问题。
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;
}
}
☆─────────────────────────────────────☆
city (city) 于 (Sat Jun 4 20:54:44 2011, 美东) 提到:
你这Q+数字玩的是什么啊
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 4 21:51:43 2011, 美东) 提到:
开始是 Brainbench 的练习题。后来加的就是我自己想问的题目了,或者是面试题,或
者是我自己遇到的问题。
☆─────────────────────────────────────☆
RayCai (菜菜) 于 (Mon Jun 6 16:14:26 2011, 美东) 提到:
Only the last one will be printed.
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 11 11:59:06 2011, 美东) 提到:
Did you try it? I tried it. It printed all.
☆─────────────────────────────────────☆
timzheng (zmit) 于 (Sat Jun 11 15:06:08 2011, 美东) 提到:
"Empty List. No node to print"
That will always be printed out first.
☆─────────────────────────────────────☆
timzheng (zmit) 于 (Sat Jun 11 15:08:51 2011, 美东) 提到:
Actually no. This won't happen. But it's better to have this out of the
recursion.
☆─────────────────────────────────────☆
done (done) 于 (Sat Jun 11 15:22:00 2011, 美东) 提到:
你发现有什么错?感觉没什么错误.
但如果说以整个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;
}
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 11 17:18:21 2011, 美东) 提到:
use head==NULL, not head=NULL (this is always true).
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 11 20:15:06 2011, 美东) 提到:
你是对的。后面那两个return语句是多余的,因为程序会自然运行。
这个message应该没错。因为只有这个list为空时,这个message才会打印。否则第二个
return处理最终状态。
验证了一下,你是对的。并且你的处理终止状态比我的简单。
...
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 11 20:17:44 2011, 美东) 提到:
第二个return语句保证了message不会被打印,除非原来的list为空。
☆─────────────────────────────────────☆
done (done) 于 (Sat Jun 11 20:27:43 2011, 美东) 提到:
噢....谢谢,那你原来上面的return是有用的,我当时想的时候是把两个return去掉后才
觉得那个message有错.
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 11 20:47:22 2011, 美东) 提到:
完全不用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
...
☆─────────────────────────────────────☆
speeddy (Wade) 于 (Sat Jun 11 21:14:04 2011, 美东) 提到:
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.
...
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 11 21:26:46 2011, 美东) 提到:
I agree. It is better to use return than to use if else.
..
☆─────────────────────────────────────☆
lipingwu (ping) 于 (Sun Jun 12 14:19:09 2011, 美东) 提到:
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";
}
☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sun Jun 12 15:18:45 2011, 美东) 提到:
It is the same as done's.
1 (共1页)
进入JobHunting版参与讨论
相关主题
Amazon电面分享求推荐学习recursive 算法的资料
leetcode 一道简单题的疑问问道面试题
问一题实现next_permutation
one linked list question问下嵌入式/DSP软件开发面试也需要刷题么?
C++ Q76: singly linked list -- 这个逆序打印有什么错?a singly-linked list question
一道Linked List题发个面试coding题,攒人品
how to judge a linked list is palindrome?问问careerup书上的一道题:
remove a node (and its memory) from a doubly linked listHow can one determine whether a singly linked list has a cycle?
相关话题的讨论汇总
话题: c++话题: q76话题: 逆序