【问题标题】:insert item to the front of a linked list? python将项目插入到链表的前面? Python
【发布时间】:2013-03-11 07:19:35
【问题描述】:

所以我一直在尝试在链表的前面插入一个项目,它有点工作,但不完全。这是我到目前为止所拥有的(LinkedList 类中有更多方法,但我省略了它们,因为它们不是问题):

class _Node():
    def __init__(self, data=None, link=None):
        self.data = data
        self.link = link

class LinkedList():
    def __init__(self):
        self.first = None
        self.size = 0

    def insert(self, ind, item):
        if self.first is None:
            self.first = _Node(item)
        elif ind == 0:                   # this is where the problem is. When I try to 
            temp = self.first            # insert a node to the front, it seems to
            temp.link = self.first.link  # forget about the rest of the nodes.
            self.first = _Node(item)
            self.first.link = temp  
        else:
            count = 0
            while count != ind - 1:
                count += 1
                self.first = self.first.link
            self.first.link = _Node(item, self.first.link)
        self.size += 1

说我在 shell 中有这个:

    >>> L = LinkedList()
    >>> L.insert(0, 5)
    >>> L.insert(1, 10)
    >>> L.insert(0, 20)
    >>> L[0]
    20
    >>> L[1]
    5
    >>> L[2]
    # and here is an error message, it says NoneType object has no attribute 'data'

所以在我上面的代码中,我要做的是创建一个与第一个节点对象相同的临时节点对象,我将该临时节点链接到第一个节点链接,我创建新节点,然后我将该新节点链接到临时节点,但这不起作用。 任何帮助都会很棒,谢谢!

【问题讨论】:

  • 为什么不使用内置数据结构?
  • 什么意思?我是编程新手,我这样做是为了复习。
  • 我的意思是python已经有链表库
  • 我不知道,我只是应该知道如何使用节点实现一个链表进行测试,这只是我坚持的唯一部分。

标签: python insert linked-list nodes temporary


【解决方案1】:

似乎您因为“不是问题”而忽略的那些功能实际上可能是您的问题...

如果您以这种方式请求每个节点中的数据,您的代码就可以工作:

>>> L = LinkedList()
>>> L.insert(0,5)
>>> L.insert(1,10)
>>> L.insert(0,20)
>>> print L.first.data
20
>>> print L.first.link.data
5
>>> print L.first.link.link.data
10

您可能在定义__getitem__ 时遇到问题。此外,您评论的部分可以在一行中重写,这可能更 Pythonic。

temp = self.first
temp.link = self.first.link
self.first = _Node(item)
self.first.link = temp

前两行什么都不做,因为tempself.first,所以你说的只是self.first.link = self.first.link。后面两个可以写成:

self.first = _Node(item, self.first)

【讨论】:

    【解决方案2】:

    首先,请注意您实际上不需要在此处对空列表进行特殊处理:

    def insert(self, ind, item):
        if ind == 0:
            newnode = _Node(item, self.first)
            self.first = newnode
    

    其次,这不是问题所在。这段代码:

        else:
            count = 0
            while count != ind - 1:
                count += 1
                self.first = self.first.link
            self.first.link = _Node(item, self.first.link)
        self.size += 1
    

    改变 self.first,所以它忘记第一个节点之前是什么。解决此问题的最小更改是:

        else:
            count = 0
            insert_point = self.first # use a local variable to walk the list
            while count != ind - 1:
                count += 1
                insert_point = insert_point.link
            insert_point.link = _Node(item, insert_point.link)
        self.size += 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多