0

25.2 Practice Name That Tune (Python Data Structures)

Problem: Linked List You are making a Music Player, which allows you to create a playlist of tracks. The given code defines Player and Track classes, where the Player is a linked list, chaining together Track objects. The code takes a number of tracks from user input and adds them to the playlist. You need to iterate over the linked list and output all the tracks in the playlist in the order of playback. Use a while loop to iterate over the 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 I'm not sure what to do here

16th Sep 2021, 7:56 PM
Cory Peitsch
Cory Peitsch - avatar
2 odpowiedzi
+ 1
The logic is you have to iterate all the nodes from head until it becomes none Here i have written a code to get all the titles https://code.sololearn.com/c39qn9nLSIu7/?ref=app
17th Sep 2021, 7:49 AM
Niththish
Niththish - avatar
+ 1
This one worked for me: 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 curr = p.head while curr: print(curr.title) curr = curr.next
19th Jan 2022, 3:37 PM
Martin Valdivia