You will be building a linked list. Make sure to keep track of both the head and
ID: 3720866 • Letter: Y
Question
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
(1) Create three files to submit.
Playlist.h - Struct definition and related function declarations
Playlist.c - Related function definitions
main.c - main() function
Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps.
Private data members
char uniqueID[50]
char songName[50]
char artistName[50]
int songLength
PlaylistNode* nextNodePtr
Related functions
CreatePlaylistNode() (1 pt)
InsertPlaylistNodeAfter() (1 pt)
Insert a new node after node
SetNextPlaylistNode() (1 pt)
Set a new node to come after node
GetNextPlaylistNode()
Return location pointed by nextNodePtr
PrintPlaylistNode()
Ex. of PrintPlaylistNode output:
(2) In main(), prompt the user for the title of the playlist. (1 pt)
Ex:
(3) Implement the PrintMenu() function. PrintMenu() takes the playlist title as a parameter and outputs a menu of options to manipulate the playlist. Each option is represented by a single character. Build and output the menu within the function.
If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to execute the menu until the user enters q to Quit. (3 pts)
Ex:
(4) Implement "Output full playlist" menu option. If the list is empty, output: Playlist is empty (3 pts)
Ex:
(5) Implement the "Add song" menu item. New additions are added to the end of the list. (2 pts)
Ex:
(6) Implement the "Remove song" function. Prompt the user for the unique ID of the song to be removed.(4 pts)
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 (1 pt)
Moving the tail node (1 pt)
Moving a node to the head (1 pt)
Moving a node to the tail (1 pt)
Moving a node up the list (1 pt)
Moving a node down the list (1 pt)
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:
Using
Compile command
gcc Playlist.c main.c -Wall -o a.out -lm
Explanation / Answer
Playlist.h
#include <math.h>
#include <stdio.h>
typedef struct PlaylistNode
{
char uniqueID[50];
char songName[50];
char artistName[50];
int songLength;
PlaylistNode* next;
}PlaylistNode;
void CreatePlaylistNode(PlaylistNode);
void InsertPlaylistNodeAfter(PlaylistNode);
void SetNextPlaylistNode(PlaylistNode);
void GetNextPlaylistNode(PlaylistNode);
void PrintPlaylistNode(PlaylistNode);
void PrintMenu(char input[]);
void RemovePlayListNode(PlayListNode,char input[]);
Playlist.c:
#include <Playlist.h>
#include <stdio.h>
#include <stdlib.h>
void PrintMenu(char input[])
{
printf("a - Add song ");
printf("r - Remove song ");
printf("c - Change position of song ");
printf("s - Output songs by specific artist ");
printf("t - Output total time of playlist (in seconds) ");
printf("o - Output full playlist ");
printf("q - Quit ");
printf("Choose a Menu ");
}
void PrintPlaylistNode(PlaylistNode *head)
{
PlaylistNode *temp=malloc(sizeof(PlaylistNode));
int number=1;
temp=head;
if(temp==NULL)
{
printf("Playlist is empty");
}
else
{
while(temp!=NULL)
{
printf("%d ",number);
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);
number=number+1;
}
}
}
void CreatePlaylistNode(PlaylistNode *head)
{
char uID[50];
char sName[50];
char aName[50];
int sLength;
printf("ADD SONG ");
printf("Enter song's unique ID: ");
scanf("%s",uID);
printf("Enter song's name: ");
scanf("%s",sName);
printf("Enter artist's name: ");
scanf("%s",aName);
printf("Enter song's length (in seconds): ");
scanf("%d",sLength);
PlaylistNode *temp=malloc(sizeof(PlaylistNode));
PlaylistNode *temp1=malloc(sizeof(PlaylistNode));
temp1=head;
temp->next=NULL;
temp->songLength=sLength;
strcpy(temp->artistName,aName);
strcpy(temp->songName,sName);
strcpy(temp->uniqueID,uID);
if(head==NULL)
{
head=temp;
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
}
}
void PrintbyArtist(PlaylistNode *head)
{
char input[256];
memset(input,'\0',256);
printf("OUTPUT SONGS BY SPECIFIC ARTIST ");
printf("Enter artist's name: ");
scanf("%s",input);
PlaylistNode *temp=malloc(sizeof(PlaylistNode));
temp=head;
if(temp==NULL)
{
printf("Playlist is empty");
}
else
{
int presence=1;
while(temp!=NULL && presence )
{
if(strcmp(temp->artistName,input)==0)
{
printf("%d ",number);
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);
number=number+1;
presence=0;
}
}
}
}
int main()
{
char playlist[256];
char menu=' ';
PlaylistNode *head=malloc(sizeof(head));
head=NULL;
memset(playlist,'\0',256);
printf("Enter Playlist ");
scanf("%s",playlist);
printf("%s PLAYLIST MENU ");
while(menu!='q')
{
PrintMenu(playlist);
scanf("%c",&menu);
if(menu=='a' || menu=='r' || menu=='c' || menu=='s' || menu=='t'|| menu=='o' || menu=='q')
{
switch(menu)
{
case 'a': CreatePlaylistNode(head);
break;
case 'r': RemovePlayListNode(head);
break;
case 'c': ChangePosition();
break;
case 's': PrintbyArtist();
break;
case 't': PrintTotalTime();
break;
case 'o': PrintPlaylistNode(head);
break;
case 'q': exit();
break;
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.