A music app allows users to create a playlist.
The playlist should behave like a circular linked list
so that when the last song finishes, it automatically loops back to the first song.
Each song has:
Song ID (integer)
Song Title (string)
Artist (string)
Duration (seconds)
The system should support the following operations:
1. Add a new song to the playlist.
2. Remove a song by its ID.
3. Display the playlist (loop through songs once).
4. Play songs in order (simulating circular playback).
5. Skip forward/backward to next/previous song.
6. Shuffle play (randomly pick songs, but still keep circular structure).
The system must use a circular linked list implementation, not Java’s built-in collections.
Solution
import java.util.Random;
class Song {
int id;
String title;
String artist;
int duration; // in seconds
Song next;
public Song(int id, String title, String artist, int duration) {
this.id = id;
this.title = title;
this.artist = artist;
this.duration = duration;
this.next = null;
}
}
class Playlist {
private Song head = null;
private Song tail = null;
private Song current = null; // currently playing
// Add song at end
public void addSong(int id, String title, String artist, int duration) {
Song newSong = new Song(id, title, artist, duration);
if (head == null) {
head = tail = newSong;
newSong.next = head; // circular link
} else {
tail.next = newSong;
newSong.next = head;
tail = newSong;
}
System.out.println("Song added: " + title);
}
// Remove song by ID
public void removeSong(int id) {
if (head == null) {
System.out.println("Playlist is empty.");
return;
}
Song currentSong = head;
Song prev = tail;
boolean found = false;
do {
if (currentSong.id == id) {
found = true;
if (currentSong == head && currentSong == tail) {
head = tail = null;
} else if (currentSong == head) {
head = head.next;
tail.next = head;
} else if (currentSong == tail) {
tail = prev;
tail.next = head;
} else {
prev.next = currentSong.next;
}
System.out.println("Removed song: " + currentSong.title);
break;
}
prev = currentSong;
currentSong = currentSong.next;
} while (currentSong != head);
if (!found) {
System.out.println("Song not found.");
}
}
// Display playlist
public void displayPlaylist() {
if (head == null) {
System.out.println("Playlist is empty.");
return;
}
System.out.println("\nPlaylist:");
Song currentSong = head;
do {
System.out.println("[ID: " + currentSong.id + ", Title: " + currentSong.title +
", Artist: " + currentSong.artist + ", Duration: " + currentSong.duration + "s]");
currentSong = currentSong.next;
} while (currentSong != head);
}
// Start playing from head
public void play() {
if (head == null) {
System.out.println("Playlist is empty.");
return;
}
current = head;
System.out.println("Now Playing: " + current.title + " by " + current.artist);
}
// Next song
public void nextSong() {
if (current == null) {
System.out.println("No song is playing.");
return;
}
current = current.next;
System.out.println("Now Playing: " + current.title + " by " + current.artist);
}
// Previous song (trick: go around until before current)
public void prevSong() {
if (current == null) {
System.out.println("No song is playing.");
return;
}
Song temp = head;
while (temp.next != current) {
temp = temp.next;
}
current = temp;
System.out.println("Now Playing: " + current.title + " by " + current.artist);
}
// Shuffle play (pick a random song)
public void shufflePlay() {
if (head == null) {
System.out.println("Playlist is empty.");
return;
}
int count = 0;
Song temp = head;
do {
count++;
temp = temp.next;
} while (temp != head);
int randIndex = new Random().nextInt(count);
temp = head;
for (int i = 0; i < randIndex; i++) {
temp = temp.next;
}
current = temp;
System.out.println("Shuffle Playing: " + current.title + " by " + current.artist);
}
}
public class MusicPlaylistCaseStudy {
public static void main(String[] args) {
Playlist playlist = new Playlist();
// Add songs
playlist.addSong(1, "Shape of You", "Ed Sheeran", 240);
playlist.addSong(2, "Blinding Lights", "The Weeknd", 200);
playlist.addSong(3, "Levitating", "Dua Lipa", 220);
// Display playlist
playlist.displayPlaylist();
// Play songs
playlist.play();
playlist.nextSong();
playlist.prevSong();
// Shuffle play
playlist.shufflePlay();
// Remove a song
playlist.removeSong(2);
playlist.displayPlaylist();
}
}