120k views
1 vote
Suppose you are building a program that manages a playlist of songs. Users can add songs to the playlist, and the songs are played in the order they were added. You want to implement a queue data structure to keep track of the songs. Giving the following list songs: [6 marks]

["Song A", "Song B", "Song C"]

Write a python program that do the following:

1. Creates an empty queue.

2. Takes a song object as an argument and adds it to the back of the queue.

3. Show and print without remove the song object at the front of the queue.

4. Print all songs on playlist queue.

5. Remove the song object at the front of the queue.

6. Print the size of queue.

1 Answer

5 votes

Final answer:

Building a program to manage a song playlist using a queue can be achieved with a Python list. You can add songs to the playlist, show the current song, print all songs, remove the front song, and get the size of the playlist queue.

Step-by-step explanation:

Python Program for Song Playlist Queue

To manage a playlist using a queue in Python, you can utilize a list to implement the queue functionality.

Initial Setup

Create an empty queue and define the function to add songs:

playlist_queue = []

def add_song_to_queue(song):
playlist_queue.append(song)

Show Front Song

To show the front song without removing it from the queue:

current_song = playlist_queue[0]
print("Current song:", current_song)

Print All Songs

To print all songs in the playlist queue:

for song in playlist_queue:
print(song)

Remove Front Song

To remove the song at the front of the queue:

removed_song = playlist_queue.pop(0)

Queue Size

Finally, to print the size of the queue:

queue_size = len(playlist_queue)
print("Size of playlist queue:", queue_size)
User Zachary K
by
8.5k points