Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms
Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Media Player Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
2
Data Structures Structure: ‘song’
Stores information of song (ID, title, singer, and duration) List: ‘playlist’ It is of type structure ‘song’ Iterator ‘it’ ‘it’ points to a song in the playlist Modified by incrementing/decrementing based on the functions Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
3
Functions Function 1: openPlayer
Initializes the playlist by making the iterator point to the beginning of the list Function 2: addToPlaylist Adds the song to the playlist. Contains title, singer, and duration of the song. Unique ID is generated for each song that is added to the playlist Function 3: removeFromPlaylist Removes the song mentioned from the playlist Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
4
Functions Function 4: goToFirst
Iterator points to the first song in the playlist Function 5: goToLast Iterator points to the last song in the playlist Function 6: goToPrev Iterator points to the previous song in the playlists Function 7: goToNext Iterator points to the next song in the playlist Function 8: goToSong Iterator points to the desired song in the playlist Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
5
Functions These five functions (4 to 8), respectively return
True: if the function was successfully performed False: if the function was not performed. e.g., if iterator ‘it’ already points to first song in the list, then ‘goToFirst’ or ‘goToPrev’ cannot be performed e.g., if iterator ‘it’ already points to the last song in the list, then ‘goToLast’ or ‘goToNext’ cannot be performed Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
6
Functions Function 9: reverse
Reverses the list of songs in the playlist Function 10: sortTitle Sorts the list of songs in the playlist, based on the title (in ascending order) Function 11: sortDuration based on the duration (in ascending order) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
7
Functions Function 12: displayPlaylist
Displays the list of songs in the playlist Function 13: displaySong Displays the song which is currently being pointed at i.e. ‘it’ Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
8
Stucture and class class mediaPlayer { struct song { private: int id;
song s; static int songID; list<song> playlist; list<song>::iterator it; public : //Functions are given on the next slide struct song { int id; string title; string singer; float duration; }; //End of structure Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
9
Functions void openPlayer();
void addToPlaylist(string songName, string singer, float duration, int position); bool goToFirst(); bool goToLast(); bool goToPrev(); bool goToNext(); bool goToSong(string title); void reverse(); void sortTitle(); void sortDuration(); void removeFromPlaylist(string songName); void displayPlaylist(); void displaySong(); }; //End of class int mediaPlayer::songID=0; Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
10
Functions void mediaPlayer::displayPlaylist(){
cout << " \n"; cout << "Song Title \t\t\t Singer \t Duration\n"; list<song> copyPlaylist; list<song>::iterator copy_it; copyPlaylist = playlist; copy_it = copyPlaylist.begin(); for (copy_it=copyPlaylist.begin(); copy_it!=copyPlaylist.end(); copy_it++) cout << copy_it->title << "\t\t" << copy_it->singer << "\t\t" << copy_it->duration << endl; cout << " \n\n"; } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
11
Functions void mediaPlayer::displaySong(){
cout << it->title << "\t\t" << it->singer << "\t\t" << it->duration << endl; } //End of function void mediaPlayer::openPlayer() { it = playlist.begin(); } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
12
Functions void mediaPlayer::addToPlaylist(string songName, string singer, float duration, int position) { s.id = ++songID; s.title = songName; s.singer = singer; s.duration = duration; int i; list<song>::iterator copy_it; copy_it = it; //Insert song at specified position it = playlist.begin(); if (position <= playlist.size()) { for(i=0;i<position;i++) { it++; } playlist.insert(it,s); else //insert at the end as position is greater than the number of songs in the playlist playlist.push_back(s); //Code for retaining the original position is given on the next slide Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
13
Functions //Code to retain the original position
if (playlist.size()==1) { it = playlist.begin(); //It is the only song in the playlist } else { it = copy_it; //Original position } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
14
Functions bool mediaPlayer::goToFirst() {
if (playlist.size()==0 || it==playlist.begin()) return false; else { it = playlist.begin(); return true; } } //End of function bool mediaPlayer::goToPrev() { if (playlist.size()==0 || it==playlist.begin()) return false; else { it--; return true; } } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
15
Functions bool mediaPlayer::goToLast() { if (playlist.size()==0)
return false; if (++it == playlist.end() ) { it--; //Point to the last song } else { it = playlist.end(); return true; } //End of function bool mediaPlayer::goToNext() { if (playlist.size()==0) return false; it++; if (it != playlist.end()) return true; else { //It is end of the playlist it--; //Point to the last song } } //End of function void mediaPlayer::reverse() { playlist.reverse(); } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
16
Functions bool mediaPlayer::goToSong(string title) {
if(playlist.size()==0) //No song in playlist return false; int i; list<song>::iterator copy_it; copy_it = it; it = playlist.begin(); while(it!=playlist.end()) { if (title.compare(it->title)==0) return true; it++; } it = copy_it; //Since song was not found, restore the original position } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
17
Functions struct removeSong { string title;
bool operator() (const song& s) { return (s.title.compare(title)==0); } }; //End of structure void mediaPlayer::removeFromPlaylist(string songTitle) { removeSong rs; rs.title=songTitle; playlist.remove_if(removeSong(rs)); } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
18
Functions bool byTitle(song first, song second) {
return first.title < second.title; } //End of function void mediaPlayer::sortTitle() { playlist.sort(byTitle); } //End of function bool byDuration(song first, song second) { return first.duration < second.duration; } //End of function void mediaPlayer::sortDuration() { playlist.sort(byDuration); } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
19
Functions int main() { mediaPlayer player; player.openPlayer();
player.addToPlaylist("Mera joota hai japani","Mukesh ",4.2,0); player.addToPlaylist("Gore rang pe naa itna","Kishore Kumar",4.3,1); player.addToPlaylist("Kya hua tera vaada","Mohd. Rafi",4.8,0); player.addToPlaylist("Kabhi Khushi Kabhi Gham","Lata Mangeshkar",4.1,53); player.addToPlaylist("Aao huzoor tumko","Asha Bhosle",3.9,2); cout << "Playlist:\n"; player.displayPlaylist(); Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
20
Functions if (player.goToSong("Aao huzoor tumko")) player.displaySong(); else cout<<"Song not found\n"; if (player.goToFirst()) player.displaySong(); else cout << "Cannot navigate to 1st song\n"; if (player.goToLast()) player.displaySong(); else cout << "Cannot navigate to last song\n"; if (player.goToNext()) player.displaySong(); else cout << "Cannot navigate to next song\n"; if (player.goToPrev()) player.displaySong(); else cout << "Cannot navigate to Previous song\n"; if (player.goToNext()) player.displaySong(); else cout << "Cannot navigate to next song\n"; if (player.goToFirst()) player.displaySong(); else cout << "Cannot navigate to 1st song\n"; if (player.goToPrev()) player.displaySong(); else cout << "Cannot navigate to previous song\n"; Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
21
Functions cout << "Playlist reversed\n";
player.reverse(); player.displayPlaylist(); cout << "Playlist Sorted on Title\n"; player.sortTitle(); player.displayPlaylist(); cout << "Playlist Sorted on Duration\n"; player.sortDuration(); player.displayPlaylist(); cout << "Song Gore rang pe naa itna removed from the list\n"; player.removeFromPlaylist("Gore rang pe naa itna"); player.displayPlaylist(); return 0; } //End of main Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
22
Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.