Professional Documents
Culture Documents
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
if (*playlist == nullptr) {
*playlist = newSong;
} else {
Song* currentSong = *playlist;
while (currentSong->next != nullptr) {
currentSong = currentSong->next;
}
currentSong->next = newSong;
}
cout << "Song '" << title << "' added to the playlist." << endl;
}
if (currentSong == nullptr) {
cout << "Song '" << title << "' not found in the playlist." << endl;
return;
}
if (prevSong == nullptr) {
*playlist = currentSong->next;
} else {
prevSong->next = currentSong->next;
}
delete currentSong;
cout << "Song '" << title << "' removed from the playlist." << endl;
}
srand(time(0)); // Seed the random number generator with the current time
int count = 0;
Song* currentSong = *playlist;
delete[] songTitles;
cout << "Playlist shuffled." << endl;
}
int count = 0;
Song* currentSong = *playlist;
delete[] songTitles;
cout << "Playlist sorted." << endl;
}
if (!found) {
cout << "Song '" << title << "' not found in the playlist." << endl;
}
}
int main() {
Song* playlist = nullptr;
int choice;
string title;
do {
cout << "Menu:" << endl;
cout << "1. Add a song" << endl;
cout << "2. Remove a song" << endl;
cout << "3. Display playlist" << endl;
cout << "4. Shuffle playlist" << endl;
cout << "5. Sort playlist" << endl;
cout << "6. Search for a song" << endl;
cout << "7. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the title of the song to add: ";
cin >> title;
addSong(&playlist, title);
break;
case 2:
cout << "Enter the title of the song to remove: ";
cin >> title;
removeSong(&playlist, title);
break;
case 3:
displayPlaylist(playlist);
break;
case 4:
shufflePlaylist(&playlist);
break;
case 5:
sortPlaylist(&playlist);
break;
case 6:
cout << "Enter the title of the song to search: ";
cin >> title;
searchSong(playlist, title);
break;
case 7:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
return 0;
}
3. The `createSong` function is defined to create a new song. It takes the title of
the song as a parameter, allocates memory for a new `Song` object, sets its title,
and initializes the `next` pointer to `nullptr`. It returns the pointer to the
newly created song.
10. The `main` function is the entry point of the program. It initializes the
`playlist` pointer to `nullptr` and uses a do-while loop to display a menu of
options to the user and perform the corresponding operations based on the user's
choice. The loop continues until the user chooses to exit (option 7).
Now let's discuss the time complexity of the program's main operations:
- Adding a song (`addSong`): The time complexity of adding a song to the playlist
is O(n), where n is the number of songs in the playlist. In the worst case, the
function needs to traverse the entire playlist to find the last song and append the
new song to the end.
- Removing a song (`removeSong`): The time complexity of removing a song from the
playlist is also O(n), where n is the number of songs in the playlist. In the worst
case, the function needs to traverse the playlist to find the song to be removed.
- Searching for a song (`searchSong`): The time complexity of searching for a song
in the playlist is O(n), where n is the number of songs in the playlist. The
function needs to traverse the playlist to find the song.
It's worth noting that the above time complexities assume that the playlist is
implemented as a linked list. If the playlist was implemented using a different
data structure, such as an array or a binary search tree, the time complexities of
the operations might be different.
Overall, the total time complexity of the program can be considered as O(n^2),
where n is the number of songs in the playlist. This is because the most time-
consuming operations, such as sorting the playlist, have a time complexity of
O(n^2). However, for smaller playlists, the actual time complexity may be lower in
practice.