Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
Advertisements

Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Containers Overview and Class Vector
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Prime numbers Jordi Cortadella Department of Computer Science.
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Exercises on Polymorphism and Operator Overloading TCP1201: 8.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Data Structures and Algorithms
Lecture 36 OOP The STL Standard Template Library
Data Structures and Algorithms
Data Structures and Algorithms
Chapter 1.2 Introduction to C++ Programming
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Chapter 1.2 Introduction to C++ Programming
Data Structures and Algorithms
Announcements HW6 due this Wednesday
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Chapter 4 Linked Lists
Data Structures and Algorithms
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
10.6 Shell Sort: A Better Insertion
Recursion.
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Introduction to Programming
Chapter 9 One-Dimensional Arrays
Unit 3 - The while Loop - Extending the Vic class - Examples
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Chapter 11 Generic Collections
Announcements HW6 due this Wednesday
Recursive Linked List Operations
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Understanding Conditions
Linked Lists.
Class StudentList class StudentList { private: int numStudents;
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Chapter 3 Lists, Stacks, and Queues
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Abstract Data Types Stacks CSCI 240
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay