Site icon DataFlair

DSA Python Project – Music Playlist

Program 1

# Music Play list based on Doubly Linked List

class Song:             # Node   of doubly linked list                                   
    def __init__(self, title):
        self.prev = None
        self.title = title
        self.next = None


class MusicPlaylist:
    def __init__(self):
        self.head = None
        self.tail = None
        self.current = None

    def add_song(self, title):
        new_song = Song(title)      # Create Node 
        if  self.head==None:
            self.head = self.tail = self.current = new_song
        else:
            self.tail.next = new_song
            new_song.prev = self.tail
            self.tail = new_song
        print(f"Added: {title}")

    def delete_current_song(self):
        if not self.current:
            print("No song to delete.")
            return

        print(f"Deleting: {self.current.title}")

        if self.current.prev!=None:
            self.current.prev.next = self.current.next
        else:
            self.head = self.current.next

        if self.current.next!=None:
            self.current.next.prev = self.current.prev
        else:
            self.tail = self.current.prev

        temp = self.current
        self.current = self.current.next if self.current.next else self.current.prev
        del temp

        if not self.current:
            print("Playlist is now empty.")
        else:
            print(f"Now playing: {self.current.title}")

    def next_song(self):
     if self.head==None:
            print("\nPlaylist is empty...")
     else:        
         if self.current and self.current.next:
             self.current = self.current.next
             print(f"Now playing: {self.current.title}")
         else:
            print("You're at the end of the playlist.")

    def prev_song(self):
     if self.head==None:
            print("\nPlaylist is empty...")
     else:     
        if self.current and self.current.prev:
            self.current = self.current.prev
            print(f"Now playing: {self.current.title}")
        else:
            print("You're at the start of the playlist.")

    # Show Play List Function
    def display_playlist(self):
        if  self.head==None:
            print("\nPlaylist is empty...")
            return
        print("\nPlaylist:")
        temp = self.head
        while temp:
            if temp == self.current:
                print(f"--> {temp.title} [CURRENT SONG]")
            else:
                print(f"    {temp.title}")
            temp = temp.next
        print()

    def clear_playlist(self):
        while self.head:
            temp = self.head
            self.head = self.head.next
            del temp
        self.tail = self.current = None


def main():
    playlist = MusicPlaylist()

    while True:
        print("\n---------------------Music Playlist--------------------")
        print("1. Add Song")
        print("2. Delete Current Song")
        print("3. Next Song")
        print("4. Previous Song")
        print("5. Show Playlist")
        print("6. Exit")
        print("-----------------------------------------------------------")
        choice = input("Enter your choice: ")

        if choice == '1':
            title = input("Enter song title: ")
            playlist.add_song(title)
        elif choice == '2':
            playlist.delete_current_song()
        elif choice == '3':
            playlist.next_song()
        elif choice == '4':
            playlist.prev_song()
        elif choice == '5':
            playlist.display_playlist()
        elif choice == '6':
            playlist.clear_playlist()
            print("Exiting playlist.")
            break
        else:
            print("Invalid choice.")


if __name__ == "__main__":
    main()

 

Exit mobile version