This Project will help you to understand the basic operations on linked list such as adding, removing and iterating a linked list forward and backward by using a real-world problem i.e PlayList
Hello everyone!!
Today we will learn how to make a simple songs playlist using linked list in java.
The main motive of this project is to clear the operations of linked list like adding elements,deleting elements,iterating a linked list,etc
Now let's go throught all the operations one by one.
1) Create a Class Song which whose objects will be the nodes of our linked list.
-The first thing we have to do to use a linked list is to import it,you can import it as
import java.util.LinkedList;
-Now To use a Linked list we first have to create one,Let's see how->
LinkedList songs=new LinkedList();
Here Song is the name of my class,here you can give any class name either pre defined classes like Integer,Double,etc or you can make your own class as I did,See the Song.java file it includes a class made by me which has three fields Song name,Artist and duration of the songs
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package playlist; /** * * @author ABHI */ public class Song { private String SongName; private String Artist; private String Duration; public Song(String SongName, String Artist, String Duration) { this.SongName = SongName; this.Artist = Artist; this.Duration = Duration; } public void showSong(){ System.out.println(this.getSongName()+"----->"+this.getArtist()+"----->"+this.getDuration()); } public String getSongName() { return SongName; } public void setSongName(String SongName) { this.SongName = SongName; } public String getArtist() { return Artist; } public void setArtist(String Artist) { this.Artist = Artist; } public String getDuration() { return Duration; } public void setDuration(String Duration) { this.Duration = Duration; } }
2) To Add a node to the Linked List
First make an object of your class and then we will use .add() function to add this as a node to our linked list.
LinkedList songs=new LinkedList(); Song s=new Song("Perfect","Ed sheeran",5 min); songs.add(s);
This will add this object as a node at the [0] index of the linked list.
In our case we have used Scanner class to get inputs from the user and add this object as a node to our linked list.
3) To Remove a node from a Linked List
here we are using .remove(int index) function to remove a node from a linked list.
We have to give the index as a parameter to remove a node from the linked list.
for ex->
songs.remove(0)
//this will remove the node(song in our case) from the 1st postion or 0th index.
3)To Show/display All the nodes of the linked list
For this we will be using enhanced for loop to fo through each node and display one by one
for(Song i:songs){ //enhanced for loop.
i.showSong(); //showSong() is a function in the Song class that will display the current node contents.
}
Alternatively we can also use Iterator class in order to display the contents of the linked list.
for ex->
ListIterator i=l.listIterator(); //To use listIterator we have to import this from (import java.util.ListIterator;)
while(i.hasNext())
{
i.next().showSong();
}
4) To Iterator A linked List
To iterate linked list forward or backward we will make use of iterator class only.
for moving forward we will first check that our LinkedList have the next node or node by using hasNext() function which will return true if there
is a next node and false otherwise ,so if this is true we will go to the next node by using next() function.
for moving backwards we will first check that our LinkedList have the previous node or not by hasPrevious() function which will return true if there
is a previous node and false otherwise,so if this is true we will go to the previous node using previous() function.
we have also used boolean variable like forward and finished to keep track wheather we are iterating forward or backwards and did the linked list has
iterated to the final node or not.
Now here is the complete code you can go through it and see how it works.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package playlist; import java.util.LinkedList; import java.util.ListIterator; import java.util.Scanner; /** * * @author ABHI */ public class Playlist { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("---------------------------Welcome All-------------------\n------------------- Create Your Own Playlist---------------"); System.out.println("Please Enter your Playlist Name"); String playlistname=input.nextLine(); LinkedList songs=new LinkedList();//Song is a class which has three fields song name,artist name and duration of the song boolean first=true; while(first){ System.out.println("\nEnter 1) To Add Songs To Playlist: "+playlistname+"\n"+ "Enter 2)To remove Songs From Playlist: "+playlistname+"\n"+ "Enter 3) To show the songs in Playlist: "+playlistname+"\n"+ "Enter 4) To Play The Playlist: "+playlistname+"\n"+ "Enter 5) To Exit"); int choice1=input.nextInt(); switch(choice1){ case 1: System.out.println("Enter The name of the Song"); input.nextLine();//Just to eliminate the enter otherwise it will be stored as a input String songname=input.nextLine(); System.out.println("Enter The Name of the artist"); String artist=input.nextLine(); System.out.println("Enter The Duration of the song"); String duration=input.nextLine(); Song s=new Song(songname,artist,duration); songs.add(s); System.out.println(songname+" song is added to playlist "+playlistname+"!!!\n"); break; case 2: if(songs.size()==0){ System.out.println("No songs present in the Playlist: "+playlistname); break; } else{ System.out.println("Here is the List of songs"); for(Song i:songs){ //enhanced for loop. i.showSong(); } System.out.println("Enter The index of the song to remove,first song is at index 1"); int index=input.nextInt(); if(index<=0|| index>songs.size()) { System.out.println("No Song in that index"); } else{ songs.remove(index-1);//As we don't want to confuse the user by the fact that actual index starts from zero. System.out.println("Song Removed!!"); } break; } case 3: if(songs.size()==0){ System.out.println("No songs in the playlist"); break; } else{ System.out.println("Here is the List of songs"); for(Song i:songs){//enhanced for loop i.showSong(); } break; } case 4: if(songs.size()==0){ System.out.println("No songs in the playlist"); break; } else{ System.out.println("Here is the List of songs"); for(Song i:songs){ //enhanced for loop i.showSong(); } System.out.println("Current Song-----"); songs.getFirst().showSong(); ListIterator j=songs.listIterator();//This is iterator and helps us to iterate to a linked list. j.next();//as our first song is currently playing. boolean forward=true;//helps to keep a track as user is going forward the linked list or backwards boolean finished=false; //To check wheather the user reached end of the linked list or not. boolean second=true; while(second){ System.out.println("\n Enter 1)To play the next song\n" +"Enter 2)To play the previous song\n" +"Enter 3)To play the same song again\n" +"Enter 4)To stop the playlist"); int choice2=input.nextInt(); switch(choice2){ case 1: if(!forward) { if(j.hasNext()) { j.next(); } forward=true; } if(finished==true){ j.previous(); finished=false; } if(j.hasNext()) { System.out.println("Song Playing----->"); j.next().showSong(); } else{ System.out.println("Playlist finished....."); finished=true; } break; case 2: if(forward) { if(j.hasPrevious()) { j.previous(); } forward=false; } if(finished==true){ j.next(); finished=false; } if(j.hasPrevious()) { System.out.println("Song Playing------ "); j.previous().showSong(); } else{ System.out.println("Playlist finished....."); finished=true; } break; case 3: if(forward) {System.out.println("Current Song again\n\n"); j.previous().showSong(); forward=false; } if(!forward) { System.out.println("Current Song again\n\n"); j.next().showSong(); forward=true; } break; case 4:second=false; break; default:second=false; break; } } } break; case 5:first=false; break; default:first=false; break; } } } }
I hope you all got a good idea about how operations on linked list can be performed easily to build real world problems.
Submitted by Abhijeet Verma (abhijeetvermayash)
Download packets of source code on Coders Packet
Comments