怀疑:史蒂夫峰会的页面/第22章 [英] Doubts:Steve Summit's page/Chapter 22

查看:75
本文介绍了怀疑:史蒂夫峰会的页面/第22章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


请澄清以下代码:

< CODE>

/ *删除节点包含l来自lp * /


结构列表* lp,* prevlp;

for(lp = list; lp!= NULL; lp = lp-> next)

{

if(lp-> item == i)

{

if(lp == list)//混乱!!

list = lp-> next;

else prevlp-> next = lp-> next ;

休息;

}

prevlp = lp;

}

}

< / CODE>


发现在 http://www.eskimo.com/~scs/cclass/int/sx8.html


我对'if''语句中'list''的含义感到困惑。

在我看来这是一个错字......


问候

Suman

Hi all,

Please clarify the following piece of code:
<CODE>
/* delete node containing i from list pointed to by lp */

struct list *lp, *prevlp;
for(lp = list; lp != NULL; lp = lp->next)
{
if(lp->item == i)
{
if(lp == list) //Confusion!!
list = lp->next;
else prevlp->next = lp->next;
break;
}
prevlp = lp;
}
}
</CODE>

found at http://www.eskimo.com/~scs/cclass/int/sx8.html

I have a confusion as to what is meant by `list'' in the `if'' statement.
That appears to me to be a typo...

Regards
Suman

推荐答案

suman kar写道:
suman kar wrote:
大家好,

请澄清以下代码:
< CODE>
/ *从列表中删除包含i的节点lp * /

struct list * lp,* prevlp;
for(lp = list; lp!= NULL; lp = lp-> next)
^^^^^^^^ {
if(lp-> item == i)
{
if(lp ==列表)//混乱!!
不在新闻组中使用行注释! list = lp-> next;
else prevlp-> next = lp-> next;
break;
}
prevlp = lp;
} <在

sx8.htmltarget =_ blank> http://www.eskimo.com/~scs/cclass/int/sx8.html


我对于什么是混淆在'if''语句中用'list''表示。
在我看来这是一个错字...
Hi all,

Please clarify the following piece of code:
<CODE>
/* delete node containing i from list pointed to by lp */

struct list *lp, *prevlp;
for(lp = list; lp != NULL; lp = lp->next) ^^^^^^^^ {
if(lp->item == i)
{
if(lp == list) //Confusion!! do not use line comments in newsgroups! list = lp->next;
else prevlp->next = lp->next;
break;
}
prevlp = lp;
}
}
</CODE>

found at http://www.eskimo.com/~scs/cclass/int/sx8.html

I have a confusion as to what is meant by `list'' in the `if'' statement.
That appears to me to be a typo...




第一:我还没有看了看网址,但似乎没有必要。


不幸的是,你似乎已经剪掉了标识符所在的部分

" list"请注意,可以有一个变量list。因为

标签(结构列表)和变量的名称空间(假设在这里:

struct list * list)不要冲突。

]清单"在if语句中是完全相同的列表如在我标记的

部分(lp = list)。

如果这让您感到困惑,请将其替换为first。


原因:" list"似乎是链表的开始。

如果我们删除了一些列表节点,我们将其前任链接到它的

后继 - 除非它是第一个节点没有前任;

在这种情况下,我们说要删除的节点的后继者是列表中新的第一个节点



干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



First: I have not looked at the URL but that seems not to be
necessary.

Unfortunately, you seem to have snipped the part where the identifier
"list" comes in. Note that there can be a variable "list" because
the name spaces of tags (struct list) and variables (assumed here:
struct list *list) do not clash.
The "list" in the if statement is exactly the same "list" as in the
part marked by me (lp = list).
If this confuses you, replace it by, say, "first".

Reason: "list" seems to be the start of a linked list.
If we eliminate some list node, we link its predecessor to its
successor -- unless it is the first node which has no predecessor;
in this case, we say the successor of the node to be deleted is the
new first node of the list.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Michael Mair< in ***** @ invalid.invalid>在消息新闻中写道:< 2v ************* @ uni-berlin.de> ...
Michael Mair <in*****@invalid.invalid> wrote in message news:<2v*************@uni-berlin.de>...
suman kar写道:
suman kar wrote:
大家好,

请澄清以下代码:
< CODE>
/ *从lp *指向的列表中删除包含i的节点

struct list * lp,* prevlp;
for(lp = list; lp!= NULL; lp = lp-> next)^^^^^^^^
Hi all,

Please clarify the following piece of code:
<CODE>
/* delete node containing i from list pointed to by lp */

struct list *lp, *prevlp;
for(lp = list; lp != NULL; lp = lp->next) ^^^^^^^^
{
if(lp-> item == i)
{
if(lp == list)>不要在新闻组中使用行注释!
list = lp-> next;
else prevlp-> next = lp-> next;
break;
}
{
if(lp->item == i)
{
if(lp == list) > do not use line comments in newsgroups!
list = lp->next;
else prevlp->next = lp->next;
break;
}


prevlp = lp;


prevlp = lp;

}
}
< / CODE>

http://www.eskimo.com/~scs/cclass/int/sx8.html

我对'if''语句中'list''的含义感到困惑。
在我看来这是一个错字...
}
}
</CODE>

found at http://www.eskimo.com/~scs/cclass/int/sx8.html

I have a confusion as to what is meant by `list'' in the `if'' statement.
That appears to me to be a typo...



首先:我没有看过URL,但似乎没有必要。

不幸的是,你似乎已经剪掉了那里的部分标识符
" list"请注意,可以有一个变量list。因为标签(结构列表)和变量的名称空间(假设在这里:
结构列表*列表)不会发生冲突。
列表在if语句中是完全相同的列表如在我标记的部分(lp = list)。
如果这让您感到困惑,请将其替换为first。



First: I have not looked at the URL but that seems not to be
necessary.

Unfortunately, you seem to have snipped the part where the identifier
"list" comes in. Note that there can be a variable "list" because
the name spaces of tags (struct list) and variables (assumed here:
struct list *list) do not clash.
The "list" in the if statement is exactly the same "list" as in the
part marked by me (lp = list).
If this confuses you, replace it by, say, "first".




无知,纯粹无知我。我真的在想:

的东西:

int int;

哪个当然是不合法的。

原因:列表似乎是一个链表的开始。
如果我们删除了一些列表节点,我们将它的前任链接到它的
后继 - 除非它是没有前任的第一个节点;
in在这种情况下,我们说要删除的节点的后继者是列表中新的第一个节点。

干杯
Michael



Ignorance, sheer ignorance on my part.I was really thinking
of something on the lines of :
int int;
which of course is not legal.

Reason: "list" seems to be the start of a linked list.
If we eliminate some list node, we link its predecessor to its
successor -- unless it is the first node which has no predecessor;
in this case, we say the successor of the node to be deleted is the
new first node of the list.
Cheers
Michael




这部分(你对'list''做的是正确的)是正确的。


还有一件事,可能是OT,

给出单链表中特定节点的地址,是否可以从列表中删除该元素而不遍历

列表?

不完全是家庭作业,但是在面试中被接受了。

面试官

应该说有关于内存复制的内容需要

这样做。

因为我不是那个错误的一方我不能

给你

面试官的解决方案是逐字的.Al所以我很好奇,知道是否有任何这样的事情是可能的。列表没有给出连续的

内存块,所以内存复制不能帮助我们在这里。


您可以选择忽略我的查询,如此含糊地描述,以及

这个事实

我显然没有付出太多努力解决它;-)然后我是一个

乐观主义者...


非常感谢您的快速回复。

问候

Suman。



This part(you were correct as to what `list'' is doing) is ok.

One more thing, possibly OT,
given the address of a particular node in a singly linked list, is it
possible to delete that element from the list without traversing the
list?
Not exactly homework but was put across in an interview.The
interviewer is
supposed to have said something regarding memory copying required to
do this.
Since I was not the one on the wrong side of the table I am unable to
give you
the interviewer''s solution verbatim.Also I am curious as to know if
any such thing is possible.Lists are not given contiguous blocks of
memory so memory copying can''t help us out here.

You can choose to ignore my query so vaguely described and also the
fact that
I apparently did not put much effort to solve it ;-)But then I am an
optimist...

Thanks a lot for the express response.
Regards
Suman.




suman kar写道:

suman kar wrote:
这部分(你对'list''做的是正确的)是正确的。

还有一件事,可能是OT,
给出了单个链接中特定节点的地址列表,是否可以从列表中删除该元素而不遍历
列表?
不完全是作业但是在面试中被放过。
面试官是应该对记忆诽谤说些什么g需要这样做。
因为我不是那个错误的一方我不能给你
面试官的解决方案。也是我很好奇,知道是否有任何这样的事情是可能的。列表没有连续的内存块,所以内存复制不能帮助我们在这里。

你可以选择忽略我的查询,如此含糊地描述,以及
我显然没有付出太多努力来解决它的事实;-)然后我是一个
乐观主义者......
This part(you were correct as to what `list'' is doing) is ok.

One more thing, possibly OT,
given the address of a particular node in a singly linked list, is it
possible to delete that element from the list without traversing the
list?
Not exactly homework but was put across in an interview.The
interviewer is
supposed to have said something regarding memory copying required to
do this.
Since I was not the one on the wrong side of the table I am unable to
give you
the interviewer''s solution verbatim.Also I am curious as to know if
any such thing is possible.Lists are not given contiguous blocks of
memory so memory copying can''t help us out here.

You can choose to ignore my query so vaguely described and also the
fact that
I apparently did not put much effort to solve it ;-)But then I am an
optimist...




有可能。让我们首先考虑

数组上下文中的所有内容:


| a [0] | a [1] | ...... | a [i] | a [i + 1] | ...... | a [size-1] |


如果我们想摆脱a [i]我们可以memmove()(size-i-1)* sizeof a [0]

字节从& a [i + 1]开始到& a [i]。

当然,我们也可以手工完成:

for(k = i; k< size; k ++)

a [k] = a [k + 1];


两者都给我们

| a [0] | a [1] | ...... | a [i-1] | " A [1 + 1] QUOT; | ...... | "一个[大小-1]" |

其中a [i + 1]意思是以前是[i + 1]。


然后,你当然可以对清单做同样的事情:


next = lp-> next;

while(next!= NULL){

memcpy(lp,lp-> next,sizeof * lp);

lp-> next = next;

}


(未经测试)

尽管如此,这不是很有意义,因为通常一个节点的大小太大而无法提高效率。

干杯

Michael

-

电子邮件:我的是gmx dot de地址。



It is possible. Let us first consider the whole thing in the
array context:

| a[0] | a[1] | ... | a[i] | a[i+1] | ... | a[size-1] |

If we want to get rid of a[i] we can memmove() (size-i-1)*sizeof a[0]
bytes starting at &a[i+1] to &a[i].
Of course, we can also do it by hand:
for (k=i;k<size;k++)
a[k] = a[k+1];

Both give us
| a[0] | a[1] | ... | a[i-1] | "a[i+1]" | ... | "a[size-1]" |
where "a[i+1]" means "formerly a[i+1]".

Then, you can of course do the same with the list:

next = lp->next;
while (next!=NULL) {
memcpy(lp,lp->next, sizeof *lp);
lp->next = next;
}

(Untested)
Nonetheless, this makes not much sense as usually the size of a
node is too large for this to be efficient.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.


这篇关于怀疑:史蒂夫峰会的页面/第22章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆