0% found this document useful (0 votes)
20 views5 pages

DSA Lab Assignment # 2 (FA23-BSE-126)

The document contains a Java implementation of a Playlist class using a doubly linked list structure to manage songs. It includes methods to add, remove, display songs, and play the next or previous song. The main method demonstrates the functionality of the Playlist class with sample songs and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

DSA Lab Assignment # 2 (FA23-BSE-126)

The document contains a Java implementation of a Playlist class using a doubly linked list structure to manage songs. It includes methods to add, remove, display songs, and play the next or previous song. The main method demonstrates the functionality of the Playlist class with sample songs and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name Munaeem

Reg no FA23-BSE-126

Lab Assignment # 2 ( Data Structures )


Code:
public class Node {
String songTitle;
String songArtist;
Node prev;
Node next;

public Node(String songTitle, String songArtist) {


this.songTitle = songTitle;
this.songArtist = songArtist;
prev = null;
next = null;
}
}
import java.util.Objects;

public class Playlist {


Node head;
Node currPlaying ;

public Playlist() {
this.head = null;
currPlaying = null;
}

// to add a song
public void addSong(String name , String artist){
Node newNode = new Node(name , artist);
if(head == null){
head = newNode;
currPlaying = head;
System.out.println("Song "+name+" added at start successfully.." );
return;
}
Node curr = head;
while(curr.next != null){
curr = curr.next;
}
curr.next = newNode;
newNode.prev = curr;
System.out.println("Song "+name+" added at last successfully.." );
}

// to remove a song by name


public void removeSong(String title){
if(head == null){
System.out.println("List is empty..");
return;
}

if(head.next == null){
if (Objects.equals(head.songTitle, title)) {
System.out.println("The only song in the list "+head.songTitle+" is
removed..");
head = null;
currPlaying = null;
return;
}
System.out.println("not found at first position...");
return;
}

if(Objects.equals(head.songTitle,title)){
System.out.println("The first song "+head.songTitle+" in the list is
removed..");
head = head.next;
head.prev = null;
currPlaying = currPlaying.next;
return;
}

Node curr = head;


while(curr != null){
if(Objects.equals(curr.songTitle,title)){
if(curr.next == null){
curr.prev.next = null;
currPlaying = currPlaying.prev;
System.out.println(curr.songTitle+" is removed..");
return;
}
curr.prev.next = curr.next;
curr.next.prev = curr.prev;
currPlaying = currPlaying.next;
System.out.println(title+" Song is deleted...");
return;
}
curr = curr.next;
}
System.out.println("Not found in List ...");
}

// to display the list


public void display(){
if(head == null){
System.out.println("List is empty..");
return;
}
Node temp = head;
System.out.println("All songs : ");
do{
System.out.println(temp.songTitle+ " by : "+temp.songArtist);
temp=temp.next;
}while(temp != null);
System.out.println();
}

// to play next song


public void playNext(){

if(currPlaying == null){
System.out.println("List is empty");
}else if(currPlaying.next == null){
System.out.println(currPlaying.songTitle+" is Last song..");
return;
}
currPlaying= currPlaying.next;
System.out.println(currPlaying.songTitle + " is playing currently..");
}

// play previous song


public void playPrevious(){
if(currPlaying == null){
System.out.println("List is empty.");
}else if(currPlaying.prev == null){
System.out.println(currPlaying.songTitle+" is first song..");
}else{
currPlaying = currPlaying.prev;
System.out.println(currPlaying.songTitle + " is playing currently..");
}
}
// play display songs playlist
public void displayForwardBackward(){
System.out.println("Currently Playing : "+currPlaying.songTitle);
Node temp = currPlaying;
System.out.println("Forward : ");
while(temp.next != null){
temp = temp.next;
System.out.println("[ "+temp.songTitle+" ]");
}
Node temp1 = currPlaying;
System.out.println("Backward : ");
while(temp1.prev != null){
temp1 = temp1.prev;
System.out.println("[ "+temp1.songTitle+" ]");
}
}
}

public class Main {


public static void main(String[] args) {
Playlist p1 = new Playlist();
p1.addSong("Shape of you" , "Ed sheeran");
p1.addSong("alone" , "alan walker");
p1.addSong("Rockstar" , "Post malone");
p1.addSong("Bad guy" , "Billie Eilish");

System.out.println("Play Next : ");


p1.playNext();
System.out.println("Play Next : ");
p1.playNext();
System.out.println("Play Previous : ");
p1.playPrevious();
p1.displayForwardBackward();
p1.addSong("Senorita" , "Shawn Mendis");
p1.removeSong("alone");
p1.displayForwardBackward();

}
}
Output:

You might also like