[英]Confused about the node when making linked lists
I'm trying to make a linked list in python without using classes.我正在尝试在不使用类的情况下在 python 中创建一个链表。 But I'm a little confused.但我有点困惑。
Here's an example of what I'm trying to achieve: head = [None,None] n = integer and the function is getting called from a loop这是我想要实现的示例: head = [None,None] n = integer 并且该函数正在从循环中调用
def append(head, n):
node = head
while node[1] != None: # Find last node
node = node[1]
node[1] = [n,None] # Attach new node
Basically using a node that holds a value and points to next.基本上使用一个保存值并指向下一个的节点。 Now I'm trying to do something similar with my program, the main difference being that the example gets the head which contains [None,None] and in my program I get 10 empty lists instead.现在我正在尝试对我的程序做一些类似的事情,主要区别在于该示例获取包含 [None,None] 的头部,而在我的程序中,我得到 10 个空列表。 This is what I got:这是我得到的:
def add(word_set,word):
node = word_set
node[-1] = [word,None]
while node[1] != None:
node = node[1]
node[1] = [word,None]
getting called by this from main:被这个从主调用:
names = ["Ella", "Owen", "Fred", "Zoe", "Adam", "Ceve", "Adam", "Ceve", "Jonas", "Ola", "Morgan", "Fredrik", "Simon", "Albin", "Måns", "Amer", "David"]
word_set = ws.new_empty_set()
for s in names:
ws.add(word_set,s)
I dont quite know how to replicate the example function with my code.我不太清楚如何用我的代码复制示例函数。 I always get index out of range when I try.当我尝试时,我总是得到索引超出范围。 Also I dont want to make any changes to main.py另外我不想对 main.py 进行任何更改
Some issues:一些问题:
Assuming that new_empty_set
returns an empty list, in add
the assignment node[-1] =
will produce the error that you mention.假设new_empty_set
返回一个空列表, add
赋值node[-1] =
将产生您提到的错误。 You should verify first whether the list is empty and deal with that boundary case separately.您应该首先验证列表是否为空并单独处理该边界情况。
The last line of add
should not be part of the loop. add
的最后一行不应该是循环的一部分。 It should be executed after the loop finishes.它应该在循环结束后执行。
Less important, but using the name "set" is confusing, as set
is a native type that is not a list, but a hash set.不太重要,但使用名称“set”会造成混淆,因为set
是一种本机类型,它不是列表,而是散列集。 So I would use the term "linked_list" instead所以我会使用术语“linked_list”代替
Corrected code:更正的代码:
# assuming this definition, but don't call it *set
# but *linked_list:
def new_empty_linked_list():
return []
def add(word_linked_list, word): # change of name
node = word_linked_list
if not node: # the list is empty
node.extend([word, None])
else:
while node[1] != None:
node = node[1]
node[1] = [word, None] # outside of loop
names = ["Ella", "Owen", "Fred", "Zoe", "Adam", "Ceve", "Adam", "Ceve", "Jonas", "Ola", "Morgan", "Fredrik", "Simon", "Albin", "Måns", "Amer", "David"]
word_linked_list = new_empty_linked_list()
for s in names:
add(word_linked_list, s)
print(word_linked_list)
There are some duplicate names in your input.您的输入中有一些重复的名称。 You haven't mentioned what should happen with those, and your add
function doesn't deal with them differently.您还没有提到这些会发生什么,并且您的add
函数不会以不同的方式处理它们。 But if the list should not store duplicates, then you should add some condition in your loop:但是如果列表不应该存储重复项,那么您应该在循环中添加一些条件:
def add(word_linked_list, word):
node = word_linked_list
if not node:
node.extend([word, None])
else:
# add condition to spot duplicate value
while node[0] != word and node[1] != None:
node = node[1]
if node[0] != word: # only add when unique
node[1] = [word, None]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.