 Welcome to this session in the course on data structures and algorithms. We will recall that last time we discussed implementation of a media player and demonstrated various functions that user would like to perform using a media player. In this session, we are going to discuss a C++ program which implements the entire functionality of a media player. First we discussed the data structures that we shall use for implementing media player functionality. There is a structure for song which stores information about the song such as its ID, title, singer, duration, etcetera. The playlist itself is a list naturally and this will be of the structure song. So, there will be multiple songs in our list. There is an iterator which we termed as IT. It points to a song in the playlist and this value of IT can be modified by increment or decrementing based on the functions that are used. What are the functions that we will use? First and foremost, open player which initializes the playlist by making the iterator point to the beginning of the list. Next is adding to a playlist so we can add a song to the playlist which will contain title, singer, duration, etcetera and a unique ID which we will generate for each song which is added to the playlist. There will be a function to remove from the playlist in which case a particular song will be removed. These are all various functions which will permit us to navigate through the playlist. So, go to first, go to last, go to previous, go to next or go to a particular song. We have already seen this functionality. We will be writing individual functions to implement these. Please note that these five functions respectively written either true or false. It returns true if the function is successfully performed and if it is not performed properly, it returns false. Let us very quickly look at some sample conditions under which the function may return false. For example, if the iterator IT already points to the first song in the list and we say go to first or go to previous, it cannot be performed because already at the first there is nothing previous. Similarly, if iterator IT points to the last song in the list then go to the last is meaningless. Similarly, go to the next is meaningless because there is no next song. So, when we implement these functions, the appropriate precautions have to be taken such that the execution of the function whether it is successfully done or not successfully done is reported back. We have additional functions such as reverse to reverse the list of song sort on title which will sort the list on the titles of the playlist. As I mentioned, it will sort on ascending order, but it is a toggle list. So, if we press it again, it will sort in the other direction. Sort duration is another sorting which is sorting the which will sort the playlist using the duration of this. Display playlist and display song are two functions which are added merely to demonstrate the output of the program. These are not central to the functionality of the playlist, but this is for illustration purpose. We start looking at the details of the program. We begin by defining the structure that we require for these songs and the class for media player. The structure is very simple. It has an ID, title, singer and duration. The class media player has several private members. First of all song itself that is S. It will have a static integer value song ID which we will use to increment subsequent songs which are added. Please note that the list that we are declaring which is the playlist uses song as the structure as a part of that list. The list consists of such structure. Similarly, for the list we also define an iterator ID. The functions which are public are given in the next slide. We already discussed all of these functions. So, you have an open player function. Add to the playlist, go to first, go to preve, go to song, reverse, sort on title, sort on duration, remove from playlist, display playlist and display song. Remember the last two functions are merely for the purpose of illustration. Please note carefully the parameters that will be passed to some of these functions such as add to playlist will obviously have a song name, singer, duration, position, etcetera. The media player song ID is 0. Please remember we actually used a static integer and this is set to 0 to begin with. Initially there are no songs in the playlist. Next, we display the playlist. This is very simple. We just output this. There is a list of the type song. The copy playlist will permit us to copy the entire playlist. The iterator copy IT is associated with that list. Now, I iterate for copy IT from copy playlist dot begin till it ends. One by one I will go through that list and I will print the desired information associated with that song. So, this is very simple. Display song just displays one song at a time. The open player is the initialization function which merely sets the iterator at the beginning position of the playlist. Next comes the addition to the playlist. So, here I will have the song name, singer, duration, position as parameters all of these are assigned to various components of s, s dot id. I increment the song id which is the static integer that we have been carrying on. Then I insert title, singer, duration, etcetera. Now, I have to insert song at a specified position. So, I will say I will set iterator to playlist dot begin. If the position given is less than or equal to playlist dot size, then I will traverse for i equal to 0 to that position and I will insert that. If that is not so, then I will insert at the end. We have already seen this functionality earlier. To retain the original position, if playlist dot size is 1, that means the IT is set at the beginning of the playlist because that is the only song in the playlist. If that is not so, then we use the copy IT and restore it to the original position. Go to first and go to previous are two functions implemented here. Very simple. I just check the size of the playlist and I will check where the iterator is. If this is 0 and it is at the beginning, go to first will return false, otherwise it will set to the beginning. Exactly opposite will happen when I say go to previous and if there is nothing to go to previous, it will return false, otherwise it will decrement iterator and return true. Go to last and go to next again are similar functions. Please note the care that we are taking. If the playlist size is 0, then it will obviously return false in both cases because there is nothing to go to. If otherwise, I will increment that IT and check whether playlist dot end as read. If so, I will set IT minus minus to 0.2 last song and it will return false. It is only in this case that I will set IT to playlist dot end and it will point to the last song by reducing IT by 1. Exactly the same thing is done when I say go to next. All similar checks are made and if IT is not equal to playlist dot end, then it will return true that means I have gone to the next, otherwise it will return false. Reverse is a very simple playlist dot reverse is a single call which will reverse the entire playlist that I have. Next, let us look at the implementation of go to song. I will of course check the size of the playlist. If it is 0, there is no song, I will return false. Otherwise, I have to go through that list to find out what is my desired song. So, playlist dot begin is the point that I set IT to and while the playlist has not ended, I will iterate around. I will use the comparison operation using title and if the comparison title dot compare is 0, I will return true set IT to plus plus. At the end of this, I will say copy IT since song was not found, I will restore it to the original position and return false. We will now see how a particular song can be removed from the playlist. The main function here is they remove from playlist which will take a string which is the song title. Now, what we are doing is given the song title, we will assign it to r s dot title and then we will use a predicate which is associated with the particular list. So, playlist dot remove underscore if is a predicate. It takes a parameter which is a removed song. What is removed song? Removed song itself is a parameter r s. Notice the removed song here. So, it is actually an object function. This is a stuck inside which there is a Boolean operator to which we are passing this r s. Please note what this simple statement does s dot title dot compare title. It will compare the given title with all titles in the list and if anyone matches that means it is 0, it will return that. If nothing matches, it will return false. Now, if this returns to that means there is a particular object that will be removed from the list. So, this simple implementation shows you the power of STL that once you give a parameter it will be compared with all titles of the playlist. How so ever big that playlist is? The remaining functions are very straight forward. So, if I have let us say by title I want to sort something. So, here I have playlist dot sort by title and you will notice that by title is here. So, it will return first dot title less than second dot title. So, this is the kind of comparison that is being made for sorting exactly the same sorting is implemented here when I want to sort by duration. Here I have by duration instead of by title which will implement exactly the same logic for duration. The main program itself is extremely straight forward. I will execute open player which will initialize the player. Then various songs are added by add to playlist 1, 2, 3, 4, 5 like that. I will output that playlist it will using the display function which I had written. Now these are various operations that are being performed. You remember we said these are the different thing that we will do. So, look at what all we are doing go to song. Now if we are able to go to that song the display function will display that. Similarly, go to first go to last go to next go to previous go to previous again go to next go to first go to previous all of these are executed depending upon whether that particular action is possible or not you will get an appropriate message either the song will be displayed or an error message will be displayed. Here is some additional function that is implemented reverse. So, player dot reverse sort on title sort on duration specific song to be removed from the list. So, player dot remove from playlist this particular string that we give. You will thus see that the standard template library permits variety of operations on the list all of which have been illustrated in this simple example of the playlist functional implementation. You can actually use a similar mechanism to implement any other list with various operations which are required to be performed on the list. Thank you.