2012-03-13 48 views
1

我无法尝试实现链接列表而不使用类(我们还没有在我的课程中),并且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 
+0

您是否正在实施像列表或链接列表一样行事的链接列表?你的问题似乎表明前者,但'addValue'上的文档字符串似乎表示后者。 – 2012-03-13 17:49:23

+0

你不允许使用课程或只是不熟悉它们? – Tom 2012-03-13 17:49:23

+0

使用'var is None'和'var is not None'而不是'var == None'和'var!= None' – juliomalegria 2012-03-13 17:50:35

回答

2

就像什么,我觉得你的addValue()函数可能看起来像一个大致轮廓......

def addValue(linkedSet, value): 

    newNode={ 
     'data': value, 
     'next': None 
    } 

    # if linkedSet is None, then you can just return this newNode 

    # if linkedSet isnt None... 
     # if linkedSets next is None, then it should just point to this newNode 
     # (append) 

     # otherwise, you should set its current next to the next of this newnode, 
     # and then set its next to this newNode (insert) 

这是一个通用链表。看起来好像你在暗示你的是一个更专业的版本,它维护着一个值类型,并且总是期望被传递给列表的头节点。你的每一个'next'需要不断循环,直到找到一个值大于当前值的值,然后通过围绕下一个元素(可能还有之前的元素)的'next'引用进行插入。

0

如何使用字典作为链表描述符? 喜欢的东西:

linkedSet = {'first':firstNode, 'last':lastNode} 

然后,当你需要斯格特你总是可以从第一个节点, 开始,当你需要添加你有你的终端节点。

+0

如果我想在两个不是第一个或最后一个节点之间添加一个值,该怎么办? – Unknown 2012-03-13 17:55:13

+0

这将需要两个对象来维护“列表”。如果你建议linkedSet是一个节点指针,它也会包含这个值,那么这不是一个双链表? – jdi 2012-03-13 17:55:35

+0

比你不得不遍历你想要插入值的节点之后,将新节点链接到下一个节点并将旧节点链接到新节点。 @jdi:linkedSet将持有对第一个节点的引用,节点将持有一个值并引用下一个节点,依此类推,直到结束节点。没有处理双链表结论... – cbaby 2012-03-13 18:10:24

1

unless the value added is less than any value already in the set听起来像这个列表应该排序。所以你通过清单,找到第一个大于你的价值的物品,并将它拼接在那里。

您通过保持当前节点的横越轨道名单:

def addValue(linkedSet, value): 

    newNode={} 
    newNode['data']=value 
    newNode['next'] = None 

    #traverse list 
    current = linkedSet 
    while True: 
     if current['value'] == value: 
      return linkedSet # it was already in that list 

     if current['value'] > value: 
      # new node is the new head 
      newNode['next'] = linkedSet 
      return newNode # new head 

     if current['next'] is None: 
      # new tail 
      current['next'] = new_node 
      return linkedSet 

     if current['next']['value'] > value: 
      # new node belongs here, splice it in 
      next_node = current['next'] 
      current['next'] = newNode 
      newNode['next'] = next_node 
      return linkedSet 

     # didnt return so far, try the next element: 
     current = current['next'] 
+0

不要忘了,这是一个作业问题:-) – jdi 2012-03-13 18:03:31

+0

这个工作正在进行一些修改,我打算花一段时间看看这个,看看它为什么工作。非常感谢。 – Unknown 2012-03-13 18:04:12

+0

我不明白的是代码的拼接部分如何影响linkedSet。当你返回链接集时,你不是在技术上返回原始列表吗? – Unknown 2012-03-13 18:07:16