gpt4 book ai didi

python - 如何跟踪链表以确定结果列表?

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:46 25 4
gpt4 key购买 nike

我对如何准确地跟踪链表以找出输出是什么感到困惑。

例如,如果链接列表如下:

头部 --> 17 --> 42 --> 25 --> 32 --> 6 --> 无

这是随之而来的代码:

NewNode = {} 
newNode[‘data’] = Head[‘data’]
newNode[‘next’] = None
ptr = Head
count = 0
while count < 3 and ptr != None:
ptr = ptr[‘next’]
count = count + 1
if ptr != None:
newNode[‘next’] = ptr[‘next’]
ptr[‘next’] = newNod

ptr = Head 
while ptr[‘data’] != 25:
ptr[‘data’] = 0
ptr = ptr[‘next’]

我对如何显示结果列表感到困惑。我知道 ['data'] 是值,['next'] 是后面值的占位符,等等。但是如何确定结果列表?

如果是双向链接,您的方法会如何改变?

此外,可选的奖励问题只是为了我自己的常识:为什么要使用链表?目前它们对我来说似乎毫无意义,但也许我只是错过了一些东西。

最佳答案

I'm confused as to how one would go about showing the resulting list

像这样:

node = Head
while node:
print (node['data'])
node = node['next']

关于python - 如何跟踪链表以确定结果列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55834735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com