【问题标题】:python linkedlist append tuplespython链表追加元组
【发布时间】:2020-06-14 11:50:56
【问题描述】:

我需要打印添加的项目。

 class LinkedList:
    def __init__(self, data= [(None,None)], number = None, letter = None, tail = None):
        self.letter = data[0][0]
        self.number = data[0][1]
        self.tail = None if (len(data) == 1) else LinkedList(data[1:])

    def insert(self, val):
        new = LinkedList(val)
        new.tail = self.data
        self.data = new

    def printer(self,curr):
        while curr:
            print(curr.letter, curr.number)
            curr = curr.tail

new = LinkedList()
new.insert([("A", 1)])
new.insert([("B", 2)])
new.insert([("C", 3)])
new.printer(new)  

【问题讨论】:

    标签: python tuples singly-linked-list


    【解决方案1】:

    你犯了一个很小的错误,把你的插入函数改成

    def insert(self, val):
            new = LinkedList(val)
            new.tail = self.tail
            self.tail = new
    

    您的LinkedList 没有data 属性,它使用tail 作为“数据”属性

    【讨论】:

      猜你喜欢
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 2016-07-24
      • 2020-11-25
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多