在python链表中添加不同的信息并打印

时间:2018-12-27 16:12:24

标签: python linked-list

我正在尝试在python链表中添加不同的信息并进行打印,但是似乎出现了错误。我想打印ID,音乐和歌手姓名。这是我的错误。 Traceback (most recent call last): File "d:\DSAG\DSAG coding\tempCodeRunnerFile.py", line 101, in <module> for i in range (0,LinkedList.len_link()): TypeError: len_link() missing 1 required positional argument: 'list'

这是我的代码:

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

  def getData(self):
    return self.data

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

  def getNext(self):
    return self.next

  def setNext(self,newNode):
    self.next = newNode

class music :
  def __init__(self, ID, musicname, artistname):
    self.ID = ID
    self.musicname = musicname
    self.artistname = artistname

  def printlist(self):
    print("ID : " + format(self.ID))
    print("Music : " + format(self.musicname))
    print("Artist : " + format(self.artistname))

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

  def next(self,newNode):
    self.head = newNode

  def len_link(list):
    temp=list.head
    count=0
    while(temp):
        count+=1
        temp=temp.next
    return count

  def printAll(self):

    node = None

    if self.head is not None:
        node = self.head
        print(node.getData())
        while(node.getNext() is not None):
            node = node.getNext()
            print(node.getData())

  def AddMusicToTheFront(self,data):
    if(self.head==None):
        newnode=Node(data)
        self.head=newnode
    else:
        current=self.head
        while(current.next!=None):
            current=current.next
        current.next=Node(data)
    self.size=self.size+1

  def AddMusicAtPosition (self, data, position):
    if(position==0):
        newnode=Node(data)
        newnode.next=self.head
        self.head=newnode

    elif(position>self.size):
        print("\nOut of Range\n")

    elif(position==self.size):
        self.AddMusicToTheFront(data)

    else:
        current=self.head
        count=0
        while(current!=None):
            if(count==position-2):
                break
            else:
                count+=1
                current=current.next
        newnode=Node(data)
        newnode.next=current.next
        current.next=newnode




list = LinkedList()
list.AddMusicToTheFront(music(1, "Lauv", "Chasing Fire"))
list.AddMusicToTheFront(music(2, "Panic! At The Disco", "High Hopes"))
list.AddMusicToTheFront(music(3, "Bishop Briggs", "River"))
list.AddMusicAtPosition(music(4,"Why Don't We", "Hooked"),2)

for i in range (0,LinkedList.len_link()):
    music.printlist()

这是我想要的结果:

ID:1
Artist : Lauv
Music : Chasing Fire

^对于我稍后将包含的其余信息,此代码将是相同的

2 个答案:

答案 0 :(得分:1)

您可能需要像这样定义X

len_link

然后,您可以实例化LinkedList对象( def len_link(self): temp=self.head count=0 while(temp): count+=1 temp=temp.next return count ),然后像ll=LinkedList()这样调用len_link()

因此您的代码应如下所示:

ll.len_link()

答案 1 :(得分:0)

for i in range (0,LinkedList.len_link(list)): music.printlist()    #pass list in the len_link finction

未设置音乐。可能会因为音乐从未启动且没有属性而出错,因此尝试打印时会出错。

您需要做的是从列表中提取一个音乐对象,然后在该对象上调用printlist()