You are on page 1of 7

#include <iostream>

#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

// Structure to represent a song


struct Song {
string title;
Song* next;
};

// Function to create a new song


Song* createSong(string title) {
Song* newSong = new Song;
newSong->title = title;
newSong->next = nullptr;
return newSong;
}

// Function to add a song to the playlist


void addSong(Song** playlist, string title) {
Song* newSong = createSong(title);

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;
}

// Function to remove a song from the playlist


void removeSong(Song** playlist, string title) {
if (*playlist == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

Song* currentSong = *playlist;


Song* prevSong = nullptr;

while (currentSong != nullptr && currentSong->title != title) {


prevSong = currentSong;
currentSong = currentSong->next;
}

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;
}

// Function to display the playlist


void displayPlaylist(Song* playlist) {
if (playlist == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

cout << "Playlist:" << endl;


Song* currentSong = playlist;
while (currentSong != nullptr) {
cout << "- " << currentSong->title << endl;
currentSong = currentSong->next;
}
}

// Function to shuffle the playlist


void shufflePlaylist(Song** playlist) {
if (*playlist == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

srand(time(0)); // Seed the random number generator with the current time

int count = 0;
Song* currentSong = *playlist;

// Count the number of songs in the playlist


while (currentSong != nullptr) {
count++;
currentSong = currentSong->next;
}

// Create an array of song titles


string* songTitles = new string[count];
currentSong = *playlist;

// Populate the array with song titles


for (int i = 0; i < count; i++) {
songTitles[i] = currentSong->title;
currentSong = currentSong->next;
}

// Shuffle the array


for (int i = count - 1; i > 0; i--) {
int j = rand() % (i + 1);
swap(songTitles[i], songTitles[j]);
}

// Update the playlist with shuffled songs


currentSong = *playlist;
for (int i = 0; i < count; i++) {
currentSong->title = songTitles[i];
currentSong = currentSong->next;
}

delete[] songTitles;
cout << "Playlist shuffled." << endl;
}

// Function to sort the playlist in ascending order


void sortPlaylist(Song** playlist) {
if (*playlist == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

int count = 0;
Song* currentSong = *playlist;

// Count the number of songs in the playlist


while (currentSong != nullptr) {
count++;
currentSong = currentSong->next;
}

// Create an array of song titles


string* songTitles = new string[count];
currentSong = *playlist;

// Populate the array with song titles


for (int i = 0; i < count; i++) {
songTitles[i] = currentSong->title;
currentSong = currentSong->next;
}

// Sort the array in ascending order

for (int i = 0; i < count - 1; i++) {


for (int j = i + 1; j < count; j++) {
if (songTitles[i] > songTitles[j]) {
swap(songTitles[i], songTitles[j]);
}
}
}

// Update the playlist with sorted songs


currentSong = *playlist;
for (int i = 0; i < count; i++) {
currentSong->title = songTitles[i];
currentSong = currentSong->next;
}

delete[] songTitles;
cout << "Playlist sorted." << endl;
}

// Function to search for a song in the playlist


void searchSong(Song* playlist, string title) {
if (playlist == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

Song* currentSong = playlist;


bool found = false;
int index = 1;

while (currentSong != nullptr) {


if (currentSong->title == title) {
cout << "Song '" << title << "' found at position " << index << " in
the playlist." << endl;
found = true;
}
currentSong = currentSong->next;
index++;
}

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;
}

cout << endl;


} while (choice != 7);

return 0;
}

The above code is an implementation of a playlist manager in C++. It allows users


to perform various operations on a playlist of songs, such as adding and removing
songs, displaying the playlist, shuffling the playlist, sorting the playlist, and
searching for a specific song.

Let's go through the code step by step:

1. The code includes the necessary C++ libraries:

- `iostream`: Provides input/output stream functionalities.


- `string`: Provides string manipulation functionalities.
- `cstdlib`: Provides functions for general-purpose operations, such as memory
allocation and random number generation.
- `ctime`: Provides functions for working with time and date.

2. A structure named `Song` is defined, which represents a song. It has two


members: `title` (string) to store the title of the song and `next` (pointer to
Song) to point to the next song in the playlist.

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.

4. The `addSong` function is defined to add a song to the playlist. It takes a


double pointer to the playlist and the title of the song as parameters. It creates
a new song using the `createSong` function and checks if the playlist is empty. If
the playlist is empty, it sets the playlist pointer to the new song. Otherwise, it
traverses the playlist to find the last song and appends the new song to the end.
5. The `removeSong` function is defined to remove a song from the playlist. It
takes a double pointer to the playlist and the title of the song to be removed as
parameters. It first checks if the playlist is empty. If it is, it displays a
message. Otherwise, it traverses the playlist to find the song to be removed,
updates the pointers to remove the song from the linked list, and frees the memory
occupied by the removed song.

6. The `displayPlaylist` function is defined to display the songs in the playlist.


It takes a pointer to the playlist as a parameter. It checks if the playlist is
empty and displays a message if it is. Otherwise, it traverses the playlist and
prints the titles of all the songs.

7. The `shufflePlaylist` function is defined to shuffle the songs in the playlist.


It takes a double pointer to the playlist as a parameter. It first checks if the
playlist is empty. If it is, it displays a message. Otherwise, it performs the
following steps:
- Seeds the random number generator using the current time.
- Counts the number of songs in the playlist.
- Creates an array of song titles and populates it with the titles of the songs
in the playlist.
- Shuffles the array of song titles using the Fisher-Yates shuffle algorithm.
- Updates the playlist with the shuffled songs.

8. The `sortPlaylist` function is defined to sort the songs in the playlist in


ascending order. It takes a double pointer to the playlist as a parameter. It first
checks if the playlist is empty and displays a message if it is. Otherwise, it
performs the following steps:
- Counts the number of songs in the playlist.
- Creates an array of song titles and populates it with the titles of the songs
in the playlist.
- Sorts the array of song titles using the bubble sort algorithm.
- Updates the playlist with the sorted songs.

9. The `searchSong` function is defined to search for a song in the playlist. It


takes a pointer to the playlist and the title of the song to be searched as
parameters. It checks if the playlist is empty and displays a message if it is.
Otherwise, it traverses the playlist to find the song and displays its position if
found.

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.

- Displaying the playlist (`displayPlaylist`): The time complexity of displaying


the playlist is O(n), where n is the number of songs in the playlist. The function
needs to traverse the entire playlist to print the titles of all the songs.
- Shuffling the playlist (`shufflePlaylist`): The time complexity of shuffling the
playlist is O(n), where n is the number of songs in the playlist. The function
needs to create an array of song titles, shuffle the array, and update the playlist
with the shuffled songs.

- Sorting the playlist (`sortPlaylist`): The time complexity of sorting the


playlist is O(n^2), where n is the number of songs in the playlist. The function
uses the bubble sort algorithm, which has a worst-case time complexity of O(n^2).

- 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.

You might also like