0% found this document useful (0 votes)
21 views

Case Study1

Uploaded by

Sonia Devi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Case Study1

Uploaded by

Sonia Devi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Data Structures and Applications (CS204) Case Study 1

A tree is a finite set of


one or more nodes such
that: (i) there is a
specially designated
node called the root;
(ii) the remaining nodes
are partitioned into n 0
disjoint sets T1, ...,Tn
where each of these
sets is a
tree. T1, ...,Tn are called
the subtrees of the root.
Case Studies:
Design a music playlist system: The functionality of a playlist needs to be implemented,
i.e., adding a song to the playlist, playing the next song, playing the previous song,
switching to a song, display of songs based on its genre type etc.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 1


Data Structures and Applications (CS204) Case Study 1

Explanation

Key Components

1. Structures:
o struct song: Represents a song with the following attributes:
 title: The title of the song.
 artist: The artist of the song.
 genre: The genre of the song.
 duration: The duration of the song in seconds.
o struct playlist: Represents a playlist, containing:
 An array of Song structures (songs).
 count: The number of songs currently in the playlist.
 current_index: The index of the currently playing song.
2. Typedefs:
o typedef struct song Song; and typedef struct playlist Playlist;
provide shorthand references to these structures.

Functions

1. add_song: Adds a new song to the playlist.


o It checks if the playlist is full (up to MAX_SONGS).
o If there's space, it copies the song details into the next available slot in the
songs array and increments the count.
2. play_next: Moves to the next song in the playlist.
o If the current index is less than the total count of songs, it increments the
current_index and returns a pointer to the next song.
3. play_previous: Moves to the previous song in the playlist.
o If the current index is greater than 0, it decrements the current_index and
returns a pointer to the previous song.
4. switch_to_song: Switches to a specific song by its index.
o It checks if the provided index is valid and sets the current_index to that
value, returning a pointer to the selected song.
5. display_songs_by_genre: Displays all songs of a specified genre.
o It iterates through the playlist and prints details of songs that match the
specified genre.
6. display_current_song: Displays information about the currently playing song.
o If a song is currently playing (i.e., the current_index is valid), it prints the
song's title and artist. If no song is playing, it indicates so.

Main Function

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 2


Data Structures and Applications (CS204) Case Study 1

 Initialization: Creates a Playlist instance (my_playlist) with zero songs and no


current song.
 Adding Songs: It adds three songs to the playlist.
 Playing Songs: It attempts to play the next song and display the current song after
each action.
 Switching Songs: It switches back to the first song and displays it.
 Displaying Songs by Genre: It calls the function to display all "Rock" and "Pop"
songs.

Program

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_SONGS 100

#define TITLE_LENGTH 100

#define ARTIST_LENGTH 100

#define GENRE_LENGTH 50

struct song{

char title[TITLE_LENGTH];

char artist[ARTIST_LENGTH];

char genre[GENRE_LENGTH];

int duration;

};

typedef struct song Song;

struct playlist{

Song songs[MAX_SONGS];

int count;

int current_index;

};

typedef struct playlist Playlist;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 3


Data Structures and Applications (CS204) Case Study 1

void add_song(Playlist* playlist, char* title, char* artist, char* genre, int duration);

Song* play_next(Playlist* playlist);

Song* play_previous(Playlist* playlist);

Song* switch_to_song(Playlist* playlist, int index);

void display_songs_by_genre(Playlist* playlist,char* genre);

void display_current_song(Playlist* playlist);

int main() {

Playlist my_playlist = { .count = 0, .current_index = -1 };

add_song(&my_playlist, "Song 1", "Artist A", "Rock", 210);

add_song(&my_playlist, "Song 2", "Artist B", "Pop", 180);

add_song(&my_playlist, "Song 3", "Artist A", "Rock", 240);

display_current_song(&my_playlist); // No song playing

Song* current = play_next(&my_playlist);

display_current_song(&my_playlist); // Playing Song 1

current = play_next(&my_playlist);

display_current_song(&my_playlist); // Playing Song 2

current = switch_to_song(&my_playlist, 0);

display_current_song(&my_playlist); // Switched to Song 1

// Display songs by genre

printf("\nRock Songs:\n");

display_songs_by_genre(&my_playlist, "Rock");

printf("\nPop Songs:\n");

display_songs_by_genre(&my_playlist, "Pop");

return 0;

void add_song(Playlist* playlist, char* title, char* artist, char* genre, int duration) {

if (playlist->count < MAX_SONGS) {

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 4


Data Structures and Applications (CS204) Case Study 1

Song* new_song = &playlist->songs[playlist->count];

strncpy(new_song->title, title, TITLE_LENGTH);

strncpy(new_song->artist, artist, ARTIST_LENGTH);

strncpy(new_song->genre, genre, GENRE_LENGTH);

new_song->duration = duration;

playlist->count++;

} else {

printf("Playlist is full! Cannot add more songs.\n");

Song* play_next(Playlist* playlist) {

if (playlist->current_index < playlist->count - 1) {

playlist->current_index++;

return &playlist->songs[playlist->current_index];

return NULL;

Song* play_previous(Playlist* playlist) {

if (playlist->current_index > 0) {

playlist->current_index--;

return &playlist->songs[playlist->current_index];

return NULL;

Song* switch_to_song(Playlist* playlist, int index) {

if (index >= 0 && index < playlist->count) {

playlist->current_index = index;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 5


Data Structures and Applications (CS204) Case Study 1

return &playlist->songs[playlist->current_index];

return NULL;

void display_songs_by_genre(Playlist* playlist, char* genre) {

int i;

for (i = 0; i < playlist->count; i++) {

if (strcmp(playlist->songs[i].genre, genre) == 0) {

printf("Title: %s, Artist: %s, Duration: %d seconds\n",

playlist->songs[i].title, playlist->songs[i].artist,

playlist->songs[i].duration);

void display_current_song(Playlist* playlist) {

if (playlist->current_index >= 0) {

Song* song = &playlist->songs[playlist->current_index];

printf("Currently Playing: %s by %s\n", song->title, song->artist);

} else {

printf("No song is currently playing.\n");

Output

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 6


Data Structures and Applications (CS204) Case Study 1

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 7

You might also like