在Python中实现链接列表

时间:2014-01-15 11:31:52

标签: python data-structures

我不明白为什么下面的代码不起作用。当我将东西附加到我的LinkedList时,它仍然为isEmpty()返回true,而printList()不会打印任何内容。很显然,我搞砸了参考文献,但我无法弄明白我的生活在哪里搞砸了。如果您还可以指出我的设计是否有任何问题,那就太棒了。

class Node(object):

    def __init__(self, data, next=None):

        self.data = data
        self.next = next

    def __str__(self):
        return str(self.data)



class LinkedList(object):



    def __init__(self):

        self.head = None
        self.size = 0


    def append(self, *data):

            n = self.head
            #new_node = Node(data)

            if self.head is None:

                n = self.head

                for elem in data:
                    n = Node(elem)
                    n = n.next
                    self.size += 1

            else:

                n = self.head
                while n.next != None:
                    n = n.next

                for elem in data:
                    n.next = Node(elem)
                    n = n.next
                    self.size += 1




    def isEmpty(self):
        return self.head is None

    def printList(self):

        n = self.head

        while n is not None:
            print str(n)

3 个答案:

答案 0 :(得分:4)

简化您的代码,一次只添加一个元素。

class Node(object):

    def __init__(self, data, next=None):

        self.data = data
        self.next = next

    def __str__(self):
        return str(self.data)



class LinkedList(object):



    def __init__(self):

        self.head = None
        self.size = 0


    def append(self, data):
            if not self.head:
                n = Node(data)
                self.head = n
                return
            else:
                n = self.head

                while n.next != None:
                    n = n.next

                new_node = Node(data)
                n.next = new_node;
                return


    def isEmpty(self):
        return not self.head

    def printList(self):
        n = self.head

        while n:
            print str(n)
            n = n.next

ll = LinkedList()
elems = [1, 2, 3, 54, 6]
for elem in elems:
    ll.append(elem)

ll.printList()

输出:

>>> 
1
2
3
54
6

答案 1 :(得分:-1)

class Node:
    def __init__(self):
        self.data = None
        self.nextNode = None

    def set_and_return_Next(self):
        self.nextNode = Node()
        return self.nextNode

    def getNext(self):
        return self.nextNode

    def getData(self):
        return self.data

    def setData(self, d):
        self.data = d

class LinkedList:
    def buildList(self, array):
        self.head = Node()
        self.head.setData(array[0])
        self.temp = self.head
        for i in array[1:]:
            self.temp = self.temp.set_and_return_Next()
            self.temp.setData(i)
            self.tail = self.temp
        return self.head
    def printList(self):
        tempNode = self.head
        while(tempNode!=self.tail):
            print(tempNode.getData())
            tempNode = tempNode.getNext()
        print(self.tail.getData())
myArray = [3, 5, 4, 6, 2, 6, 7, 8, 9, 10, 21]

myList = LinkedList()
myList.buildList(myArray)
myList.printList()

答案 2 :(得分:-1)

class LinkedList(object):
   def __init__(self,data,none):
      self.data=data
      self.none=none

  def insert(self):