8.15 Ch 8 Program: Playlist (C Language please) Could you also show that the cod
ID: 3857923 • Letter: 8
Question
8.15 Ch 8 Program: Playlist (C Language please)
Could you also show that the code compiled and produces the sample output.
I tried this code https://www.chegg.com/homework-help/questions-and-answers/c-code-unix-c-code-please-playlist-c-building-linked-list-make-sure-keep-track-head-tail-n-q23089711 but it would not compile there were a lot of errors.
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:
Explanation / Answer
1. playlist.h
#include<stdio.h>
#include<stdlib.h>
struct PlayListnode{
char uniqueID[50];
char songName[50];
char artistName[50];
int songLength;
struct PlaylistNode *nextNodePtr;
}*head=NULL,*tail=NULL;
void CreatePlayListNode();
void InsertPlayListNodeAfter();
struct PlayListnode *SetNextPlaylistNode();
struct PlayListnode *GetNextPlaylistNode();
void PrintPlaylistNode(struct PlayListnode *ptr);
void PrintMenu(char *title);
void removeSong();
void changePosition();
void output_time();
void output_full_playlist();
2. playlist.c
#include<stdio.h>
#include<stdlib.h>
#include "playlist.h"
void createPlayListNode()
{
}
void PrintPlaylistNode(struct PlayListnode *ptr)
{
printf("UNIQUE ID : %s ",ptr->uniqueID);
printf("Song Name : %s ",ptr->songName);
printf("Artist Name : %s ",ptr->artistName);
printf("Song Length (in seconds) : %d ",ptr->songLength);
printf(" ");
}
//adds the song to the last of the list
void InsertPlayListNodeAfter()
{
char song_id[50], song_name[50], artist_name[50];
int song_length;
struct PlayListnode *tmp;
printf("Enter song's unique ID: ");
gets(song_id);
getchar();
printf("Enter song's name: ");
gets(song_name);
getchar();
printf("Enter artist's name: ");
gets(artist_name);
getchar();
printf("Enter song's length (in seconds): ");
scanf("%d", &song_length);
getchar();
tmp = (struct PlayListnode *)malloc(sizeof(struct PlayListnode));
strcpy(tmp->uniqueID,song_id);
strcpy(tmp->songName,song_name);
strcpy(tmp->artistName,artist_name);
tmp->songLength = song_length;
tmp->nextNodePtr = NULL;
if(head==NULL)
{
head=tmp;
tail=tmp;
return;
}
tail->nextNodePtr=tmp;
tail=tmp;
return;
}
void removeSong()
{
char song_id[50];
struct PlayListnode *p ,*tmp;
if(head==NULL)
{
printf("No songs in the playlist ");
return;
}
printf("Enter song's unique ID ");
gets(song_id);
getchar();
p=head;
//to check for the first node
if(strcmp(head->uniqueID,song_id)==0)
{
tmp=head;
head=head->nextNodePtr;
printf("%s removed ",tmp->songName);
free(tmp);
if(head==NULL||head->nextNodePtr==NULL)
tail=head;
return;
}
//to check for other nodes
p=head;
while(p->nextNodePtr!=NULL)
{
if(strcmp(p->nextNodePtr,song_id)==0)
{
tmp=p->nextNodePtr;
p->nextNodePtr = tmp->nextNodePtr;
printf("%s removed ",tmp->songName);
free(tmp);
if(p->nextNodePtr==NULL)
tail=p;
return;
}
p=p->nextNodePtr;
}
return;
}
void changePosition()
{
int old_pos,new_pos,i;
struct PlayListnode *p, *q, *p_prev, *q_prev, *tmp, *tmp1;
printf("Enter song's current position ");
scanf("%d",&old_pos);
getchar();
printf("Enter new position for song ");
scanf("%d",&new_pos);
getchar();
p_prev=NULL;
p=head;
for(i=1; i<old_pos; i++)
{
p_prev=p;
p=p->nextNodePtr;
if(p==tail)
break;
}
//remove the node p from old position
//
if(p_prev==NULL){
head=p->nextNodePtr;
p->nextNodePtr=NULL;
}
else{
p_prev->nextNodePtr=p->nextNodePtr;
p->nextNodePtr=NULL;
}
q_prev=NULL;
q=head;
for(i=1; i<new_pos; i++)
{
q_prev=q;
q=q->nextNodePtr;
if(q==NULL)
break;
}
//insert at the given position
if(q_prev==NULL){
p->nextNodePtr=head;
head=p;
}
else{
q_prev->nextNodePtr=p;
p->nextNodePtr=q;
}
return;
}
void specific_artist_display()
{
char artist_name[50];
int i=0;
printf("Enter artist's name ");
gets(artist_name);
getchar();
struct PlayListnode *p=head;
while(p!=NULL)
{
if(strcmp(p->artistName,artist_name)==0)
{
printf("%d ",i);
PrintPlaylistNode(p);
}
p=p->nextNodePtr;
i++;
}
return;
}
void output_time()
{
int sum=0;
struct PlayListnode *p = head;
while(p!=NULL)
{
sum+=p->songLength;
p=p->nextNodePtr;
}
printf("Total time: %d ",sum);
}
void output_full_playlist(char *title)
{
printf("%s OUTPUT FULL LIST ",title);
int i=1;
struct PlayListnode *p=head;
while(p!=NULL)
{
printf("%d ",i);
PrintPlaylistNode(p);
p=p->nextNodePtr;
i++;
}
return;
}
char display_menu(char *title)
{
char ch;
printf("%s PLAYLIST MENU ",title);
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 ");
printf("o- Output full playlist ");
printf("Choose an option: ");
scanf("%c",&ch);
getchar();
return ch;
}
int main()
{
char op ,title[50] ;
printf("enter playlist's title ");
gets(title);
getchar();
op = display_menu(title);
while(op!='q')
{
switch(op)
{
case 'a':InsertPlayListNodeAfter();
break;
case 'r':removeSong();
break;
case 'c':changePosition();
break;
case 's':specific_artist_display();
break;
case 't':output_time();
break;
case 'o':output_full_playlist(title);
break;
default:printf("Wrong choice");
}
op = display_menu(title);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.