You will be building a linked list. Make sure to keep track of both the head and
ID: 3721107 • Letter: Y
Question
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
Using
Playlist.h - Struct definition and related function declarations
Playlist.c - Related function definitions
main.c - main() function
Private data members
char uniqueID[50]
char songName[50]
char artistName[50]
int songLength
PlaylistNode* nextNodePtr
Related functions
CreatePlaylistNode()
InsertPlaylistNodeAfter()
Insert a new node after node
SetNextPlaylistNode()
Set a new node to come after node
GetNextPlaylistNode()
Return location pointed by nextNodePtr
PrintPlaylistNode()
(5) Implement the "Add song" menu item. New additions are added to the end of the list.
Ex:
(6) Implement the "Remove song" function. Prompt the user for the unique ID of the song to be removed.
Ex:
(7) Implement the "Change position of song" menu option. Prompt the user for the current position of the song and the desired new position. Valid new positions are 1 - n (the number of nodes). If the user enters a new position that is less than 1, move the node to the position 1 (the head). If the user enters a new position greater than n, move the node to position n (the tail). 6 cases will be tested:
Moving the head node
Moving the tail node
Moving a node to the head
Moving a node to the tail
Moving a node up the list
Moving a node down the list
Ex:
(8) Implement the "Output songs by specific artist" menu option. Prompt the user for the artist's name, and output the node's information, starting with the node's current position. (2 pt)
Ex:
(9) Implement the "Output total time of playlist" menu option. Output the sum of the time of the playlist's songs (in seconds). (2 pts)
Ex:
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct PlaylistNode{
char *uniqueID;
char *songName;
char *artistName;
int songLength;
struct PlaylistNode* nextNodePtr;
};
struct PlaylistNode* head = NULL;
struct PlaylistNode * tail = NULL;
struct PlaylistNode * temp = NULL;
struct PlaylistNode * prev = NULL;
int choice;
struct PlaylistNode * CreatePlaylistNode(char uniqueID[50], char songName[50],char artistName[50],int songLength){
struct PlaylistNode * node=(struct PlaylistNode *)malloc(sizeof(struct PlaylistNode));
node->uniqueID = uniqueID;
node->songName = songName;
node->artistName = artistName;
node->songLength = songLength;
node->nextNodePtr=NULL;
return node;
}
InsertPlaylistNodeAfter(){
}
SetNextPlaylistNode(){
}
struct PlaylistNode * GetNextPlaylistNode(struct PlaylistNode * node){
return node->nextNodePtr;
}
PrintPlaylistNode(){
}
void doOperations(){
char *uniqueID1;
char *songName1;
char *artistName1;
int songlength1;
int currentposition;
int newpostion;
switch (choice){
case 1:
printf("Enter song's unique ID: ");
scanf("%s",&uniqueID1);
printf("Enter song's name: ");
scanf("%s",&songName1);
printf("Enter artist's name: ");
scanf("%s",&artistName1);
printf("Enter song's length(in seconds): ");
scanf("%d",&songlength1);
temp = head;
if(temp==NULL){
head = CreatePlaylistNode(uniqueID1,songName1,artistName1,songlength1);
return ;}
while(temp->nextNodePtr!=NULL){
temp=temp->nextNodePtr;
}
temp->nextNodePtr = CreatePlaylistNode(uniqueID1,songName1,artistName1,songlength1);
break;
case 2:
printf("Enter song's unique ID:");
scanf("%s",&uniqueID1);
temp = head;
if(head!=NULL && head->uniqueID==uniqueID1){
printf("%s removed. ",head->songName);
head = head->nextNodePtr;
return;
}
while(temp!=NULL){
printf("before second if ");
if (temp->uniqueID==uniqueID1){
prev->nextNodePtr = temp->nextNodePtr;
temp = prev;
printf("%s removed. ",temp->songName);
printf("second if ");
return ;
}
prev = temp;
temp = temp->nextNodePtr;
}
if (temp==NULL){
printf("No song found with this ID ");
}
break;
case 3:;
int currentposition;
int newposition;
printf("Enter song's current position: ");
scanf("%d",¤tposition);
printf("Enter song's new position: ");
scanf("%d",&newposition);
struct PlaylistNode* prev1,prev2;
int count=0;
temp = head;
break;
case 4:
printf("Enter artist's name:");
scanf("%s",&artistName1);
temp = head;
int position =1;
while(temp!=NULL){
if(temp->artistName == artistName1){
printf("%d ",position);
printf("Unique ID:%s ",temp->uniqueID);
printf("Song Name:%s ",temp->songName);
printf("Artist Name:%s ",temp->artistName);
printf("Song Length (in seconds):%d ",temp->songLength);
}
temp = temp->nextNodePtr;
}
break;
case 5:;
int totaltime=0;
while(temp!=NULL){
totaltime = totaltime+temp->songLength;
temp = temp->nextNodePtr;
}
printf("Total time :%d ",totaltime);
break;
default:
printf("You have chosen the wrong option. Try Once Again. ");
return ;
}
}
int main(){
do{
printf("See the Menu and Choose the menu item: Press ");
printf("1: To Add Song ");
printf("2: To Remove Song ");
printf("3: To change position of the song ");
printf("4: To output songs by specific artist ");
printf("5: To output total time of playlist ");
printf("0: To exit ");
scanf("%d",&choice);
if(choice!=0){
doOperations();
}
}while(choice);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.