Many of us have large digital music collections that are not always very well or
ID: 640172 • Letter: M
Question
Many of us have large digital music collections that are not always very well organized. It would be nice to have a program that would arrange our music collection based attributes such as artist, album title, song title, genre, song length, number times played, and rating. For this assignment you will write a digital music manager (DMM).
Your DMM program must have a text-based interface which allows the user to select from a menu of options including: load, store, display, insert, delete, edit, sort, rate, and exit. The
Explanation / Answer
Program code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define strlength 25;
struct node {
struct node *prev;
char artist[strlength];
char albumtitle[strlength];
char songtitle[strlength];
char genre[strlength];
int songlen;
int num_ts_played;
int rating;
struct node *next;
} *h, *temp, *temp1, *temp2, *temp4, *temp3;
void insert();
void delete();
void sort();
void edit();
void store(int);
void load();
void display();
void rate();
int count = 0;
int count1=0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
printf(" 1 - load");
printf(" 2 - store");
printf(" 3 - display");
printf(" 4 - insert");
printf(" 5 - delete");
printf(" 6 - sort");
printf(" 7 - edit");
printf(" 8 - rate");
printf(" 9 - exit");
while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
load();
break;
case 2:
store();
break;
case 3:
display();
break;
case 4:
insert();
sort();
break;
case 5:
delete();
break;
case 6:
sort();
break;
case 7:
edit();
break;
case 8:
rate();
break;
case 9:
exit(0);
break;
default:
printf(" Wrong choice menu");
}
}
}
/* TO create an empty node */
void create()
{
int data;
temp = (struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("Artist Name: ");
scanf("%s", temp->artist);
printf("Album title: ");
scanf("%s", temp->albumtitle);
printf("Song title: ");
scanf("%s", temp->songtitle);
printf("Generic name: ");
scanf("%s", temp->genre);
printf("Length of the song: ");
scanf("%d", temp->songlen);
printf("Number of times played: ");
scanf("%d", temp->num_ts_played);
printf("Rating: ");
scanf("%d", temp->rating);
count++;
}
/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To delete an element */
void delete()
{
int i = 1;
char song[strlength];
printf(" Enter title of the song : ");
scanf("%s", song);
temp2 = h;
if (strcmp(temp2->songtitle, song) == 0)
{
printf(" Error : Position out of range to delete");
return;
}
if (h == NULL)
{
printf(" Error : Empty list no elements to delete");
return;
}
else
{
while (temp2->songtitle, song)
== 0)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */
if (i == 1)
h = temp2->next;
printf(" Node deleted");
free(temp2);
}
count--;
}
/* Traverse from beginning */
void display() {
temp2 = h;
if (temp2 == NULL)
{
printf("List empty to display ");
return;
}
printf(" Linked list elements from begining : ");
while (temp2->next != NULL)
{
printf(" %s ", temp2->artist);
printf(" %s ", temp2->albumtitle);
printf(" %s ", temp2->songtitle);
printf(" %s ", temp2->genre);
printf(" %d ", temp2->songlen);
printf(" %d ", temp2->num_ts_played);
printf(" %d ", temp2->rating);
temp2 = temp2->next;
}
printf(" %s ", temp2->songtitle);
}
/* To search for an element in the list */
void search() {
int count = 0;
char artist[strlength];
temp2 = h;
if (temp2 == NULL)
{
printf(" Error : List empty to search for data");
return;
}
printf(" Enter value to search : ");
scanf("%s", artist);
while (temp2 != NULL)
{
if (strcmp(temp2->artist, artist) == 0)
{
printf(" Data found in %d position", count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf(" Error : %d not found in list", data);
}
/* To update a node value in the list */
void edit()
{
char album[strlength];
char album1[strlength];
printf(" Enter album title to be updated : ");
scanf("%d", &album);
printf(" Enter new data : ");
scanf("%d", &album1);
temp2 = h;
if (temp2 == NULL)
{
printf(" Error : List empty no node to update");
return;
}
while (temp2 != NULL)
{
if (strcmp(temp2->albumtitle, album) == 0)
{
temp2->albumtitle = album;
display();
return;
}
else
temp2 = temp2->next;
}
printf(" Error : %d not found in list to update", data);
}
/* To sort the linked list */
void sort()
{
int i, j, x;
temp2 = h;
temp4 = h;
temp3 = h;
if (temp2 == NULL)
{
printf(" List empty to sort");
return;
}
for (temp2 = h; temp2 != NULL; temp2 = temp2->next)
{
for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
{
if (strcmp(temp2->genre, temp4->genre) > 0)
{
temp3 = temp2;
temp2 = temp4;
temp4 = temp3;
}
}
}
display();
}
void load()
{
FILE *fp;
char ch;
fp = fopen("test.txt", "r");
while ((ch = getc(file)) != EOF)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
fscanf("%s", temp->artist);
fscanf("%s", temp->albumtitle);
fscanf("%s", temp->songtitle);
fscanf("%s", temp->genre);
fscanf("%d", temp->songlen);
fscanf("%d", temp->num_ts_played);
fscanf("%d", temp->rating);
temp->next = h;
h->prev = temp;
h = temp;
count1++;
}
fclose(fp);
}
void store()
{
FILE *fp;
temp = (struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
char ch;
fp = fopen("test.txt", "w");
if (fp)
{
for (i = 0; i <= count1; ++i)
{
fprintf("%s ", temp->artist);
fprintf("%s ", temp->albumtitle);
fprintf("%s ", temp->songtitle);
fprintf("%s ", temp->genre);
fprintf("%d ", temp->songlen);
fprintf("%d ", temp->num_ts_played);
fprintf("%d ", temp->rating);
fprintf(" ");
}
}
fclose(fp);
}
void rate()
{
temp2 = h;
char song[strlength];
int r;
printf("rate the songs: ");
printf("enter the song to rate: ");
scanf("%s", &song);
if (temp2 == NULL)
{
printf("List empty to display ");
return;
}
while (temp2->next != NULL)
{
if(strcmp(temp2->songtitle,song)==0)
{
printf("enter the rate(1-5): ");
scanf("%d", r);
temp2->rating=r;
}
else
{
temp2=temp2->next;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.