Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

hi, i have a project, where we have to create a linked list of albums and every

ID: 3543960 • Letter: H

Question

hi, i have a project, where we have to create a linked list of albums and every node of albums are a header of another linked list of songs. i writed some code but does not work when it comes to adding. The method should give a warning message when there is no album that entered. And song names must be unique, so it has to give warning message when same named song is added. Here is the some part of code:


struct album {

char albumName[500];

char singerName[500];

int year;

struct album* nextAlbum;

struct song* nextSong;

};

struct song {

char songName[500];

int length;

struct song* next;

};

typedef struct album anode;

typedef struct album* anodePtr;

typedef struct album** anodePtrPtr;


typedef struct song snode;

typedef struct song* snodePtr;

typedef struct song** snodePtrPtr;

// some part of main

int main() {

anodePtr head = NULL;

anodePtr m;

int choice;

char x[30];

char y[30];

int z,l;

char t[30];

char u[30];

int posn = 0, sposn = 0;


do {

printf( "? " );

scanf( "%d", &choice );

switch(choice) {

case 4:

printf( "Enter album name, song name and song length in seconds in order: " );

scanf("%s %s %d", u, t, &l);

sposn = addSong(&head, u, t, l);

if(!sposn)

printf(" ");

break;

}

}while(choice!=9);


}

int addSong(anodePtrPtr header, char albumName[], char songName[], int length) {

anodePtr headAlbum;

snodePtr newSong, tempSong;

int i;


newSong = (snode*)malloc(sizeof(snode));

for(i = 0; i < 20; i++) {

newSong -> songName[i] = songName[i];

}

newSong -> length = length;


headAlbum = *header;

newSong -> next = NULL;


while(headAlbum != NULL && strcmp(headAlbum -> albumName, albumName) == 0) {

if(headAlbum -> nextSong = NULL) {

headAlbum -> nextSong = newSong;

}

else {

tempSong = headAlbum -> nextSong;

if(tempSong -> next != NULL) {

tempSong = tempSong -> next;

}

else {

tempSong -> next = newSong;

}

return 1;

}

printf("No such album found");

return 0;

}

}

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


struct album {

char *albumName;

char *singerName;

int year;

struct album* nextAlbum;

struct song* nextSong;

};


struct song {

char *songName;

int length;

struct song* nextSong;

};


typedef struct album album;

typedef struct album* albumPtr;

typedef struct album** albumPtrPtr;


typedef struct song snode;

typedef struct song* snodePtr;

typedef struct song** snodePtrPtr;


int addAlbum(albumPtrPtr,char*,char*,int);

void main() {

int choice; albumPtr hdr=NULL;

int year; char* x; char* y;

x = malloc(20*sizeof(char));

y = malloc(20*sizeof(char));

printf("Enter choice: ");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter albumname singername and year: ");

scanf("%s %s %d",x,y,&year);

addAlbum(&hdr,x,y,year);

break;

default:break;

}

}




int addAlbum(albumPtrPtr header, char *albumNamee, char *singerNamee, int year) {

albumPtr newAlbum, prevAlbum, tempAlbum;


newAlbum = malloc(sizeof(album));


newAlbum -> albumName = albumNamee;

newAlbum -> singerName = singerNamee;


newAlbum -> year = year;


newAlbum -> nextAlbum = NULL;

if(*header == NULL)

*header = newAlbum;


else {

tempAlbum = *header;

while(tempAlbum != NULL && tempAlbum -> year < newAlbum -> year) {

prevAlbum = tempAlbum;

tempAlbum = tempAlbum -> nextAlbum;

}


if(tempAlbum != NULL && strcmp(tempAlbum -> albumName, newAlbum -> albumName)==0) {

printf("Album already exists. ");

return 0;

}


else if(tempAlbum != NULL)

newAlbum -> nextAlbum = tempAlbum;


else if(tempAlbum == *header)

*header = newAlbum;


else

prevAlbum -> nextAlbum = newAlbum;

}


return 1;

}