This question has been asked twice before both answers are incomplete. Create a
ID: 3537907 • Letter: T
Question
This question has been asked twice before both answers are incomplete.
Create a linked list structure Music that contains the data fields Name, Artist, Number_of_Songs, and a pointer to the list. Create the structure with 3 members and fill in data for each member. Create a function that will display() all the data for each member and call it from the main program.
Previous anwers get an error from the "curr->next = head;" line says that you can't convert an item to a list and vice versa.
Also the previous answers only had two data fields vice the three the question specifies.
Explanation / Answer
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct Music
{
char name[40];
char artist[40];
int number;
struct Music *next;
};
typedef struct Music music;
void insert(music **head,char *name,char *artist,int number)
{
music *temp;
temp=(music *)malloc(sizeof(struct Music));
if(!temp)
{
printf("no space ");
exit(1);
}
strcpy(temp->name,name);
strcpy(temp->artist,artist);
temp->number=number;
temp->next=*head;
*head=temp;
}
void display(music *head)
{
music *temp;
if(!head)
{
printf("no information ");
return;
}
temp=head;
while(temp)
{
printf("name:%-20s artist:%-20s number of songs:%-3d ",temp->name,temp->artist,temp->number);
temp=temp->next;
}
}
int main()
{
music *head=0;
insert(&head,"thriller","michael jackson",9);
insert(&head,"born sinner","J.Cole",10);
insert(&head,"Yeezus","Kanye West",7);
display(head);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.