我无法尝试实现链接列表而不使用类(我们还没有在我的课程中),并且Google根本没有任何帮助。每个链表的例子都使用了我没有涉及的类。我可以创建一个链接列表,它将一个值添加到链表的开头,但我不知道如何遍历列表并在特定节点之后添加值。任何帮助,将不胜感激。对我来说最难的部分是弄清楚如何遍历列表。链接列表Python 2.7
def addValue(linkedSet, value):
"""
Adds a new element to a set.
Parameters:
the set to be changed (address of the first node)
the new value to add to the set
Return result: pointer to the head of the modified set. This is
usually the same as the linkedSet parameter, unless the value
added is less than any value already in the set.
If the value is already in the set, return the set unchanged.
This is not an error.
"""
newNode={}
newNode['data']=value
node=linkedSet
if linkedSet==None:
newNode['next']=None
return newNode
if member(linkedSet,value)==True:
return linkedSet
elif linkedSet['next']==None:
newNode['next']=None
linkedSet['next']=newNode
elif linkedSet['next']!=None:
return linkedSet
您是否正在实施像列表或链接列表一样行事的链接列表?你的问题似乎表明前者,但'addValue'上的文档字符串似乎表示后者。 – 2012-03-13 17:49:23
你不允许使用课程或只是不熟悉它们? – Tom 2012-03-13 17:49:23
使用'var is None'和'var is not None'而不是'var == None'和'var!= None' – juliomalegria 2012-03-13 17:50:35