+ 1

Linked list (Python)

I am stuck in this code. How to print the input songs to screen in this linked list? class Track: def __init__(self, title, next): self.title = title self.next = next class Player: def __init__(self): self.head = None def add(self, title): if not self.head: self.head = Track(title, None) return curr = self.head while curr.next: curr = curr.next curr.next = Track(title, None) p = Player() while True: x = input() if x == 'end': break p.add(x) #your code goes here

28th Sep 2021, 11:48 AM
Norberto Costa
Norberto Costa - avatar
3 Answers
+ 2
class Track: def __init__(self, title, next): self.title = title self.next = next class Player: def __init__(self): self.head = None def add(self, title): if not self.head: self.head = Track(title, None) return curr = self.head while curr.next: curr = curr.next curr.next = Track(title, None) p = Player() while True: x = input() if x == 'end': break p.add(x) n = p.head while n != None: print(n.title) n = n.next
3rd Dec 2022, 12:04 AM
Armin
+ 1
class Track: def __init__(self, title, next): self.title = title self.next = next class Player: def __init__(self): self.head = None def add(self, title): if not self.head: self.head = Track(title, None) return curr = self.head while curr.next: curr = curr.next curr.next = Track(title, None) p = Player() while True: x = input() if x == 'end': break p.add(x) print(x)
30th May 2022, 2:50 PM
Thanarasan sivanujan
0
Does printing p not return your inputs?
1st Oct 2021, 6:58 AM
Asanda Ndimande
Asanda Ndimande - avatar