Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE WRITE IN C, NOT C++ Problem Description You will be building a linked list. Make sure to keep track of both the head and

PLEASE WRITE IN C, NOT C++

Problem Description

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.

  • PlaylistNode.h - Struct definition and related function declarations
  • PlaylistNode.c - Related function definitions
  • main.c - main() function

The PlaylistNode struct as well as function declarations for the related functions described below have been provided in PlaylistNode.h. Please do not modify the provided PlaylistNode.h file. Details regarding each function as well as expected return values are included in the comments associated with each function declaration in PlaylistNode.h. The following is a summary of this content:

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()
      • Call malloc to allocate space for a PlaylistNode and return a pointer to the initialized object
    • InsertPlaylistNodeAfter()
      • Insert a new node after node
    • SetNextPlaylistNode()
      • Set a new node to come after node
    • GetNextPlaylistNode()
      • Return location pointed by nextNodePtr
    • PrintPlaylistNode()
      • Display the contents of this PlaylistNode on stdout using printf
    • DestroyPlaylistNode()
      • Call free to release all memory dynamically allocated by malloc for the specified node

Ex. of PrintPlaylistNode output:

Unique ID: S123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237 

2. In main(), prompt the user for the title of the playlist. Ex:

Enter playlist's title: JAMZ 

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. Ex:

JAMZ PLAYLIST MENU a - Add song r - Remove song c - Change position of song s - Output songs by specific artist t - Output total time of playlist (in seconds) o - Output full playlist q - Quit Choose an option: 

4. Implement "Output full playlist" menu option. If the list is empty, output: `Playlist is empty` Ex:

JAMZ - OUTPUT FULL PLAYLIST 1. Unique ID: SD123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237 2. Unique ID: JJ234 Song Name: All For You Artist Name: Janet Jackson Song Length (in seconds): 391 3. Unique ID: J345 Song Name: Canned Heat Artist Name: Jamiroquai Song Length (in seconds): 330 4. Unique ID: JJ456 Song Name: Black Eagle Artist Name: Janet Jackson Song Length (in seconds): 197 5. Unique ID: SD567 Song Name: I Got The News Artist Name: Steely Dan Song Length (in seconds): 306 

5. Implement the "Add song" menu item. New additions are added to the end of the list. Ex:

ADD SONG Enter song's unique ID: SD123 Enter song's name: Peg Enter artist's name: Steely Dan Enter song's length (in seconds): 237 

6. Implement the "Remove song" function. Prompt the user for the unique ID of the song to be removed. Ex:

REMOVE SONG Enter song's unique ID: JJ234 "All For You" removed 

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:

CHANGE POSITION OF SONG Enter song's current position: 3 Enter new position for song: 2 "Canned Heat" moved to position 2 

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.

Ex:

OUTPUT SONGS BY SPECIFIC ARTIST Enter artist's name: Janet Jackson 2. Unique ID: JJ234 Song Name: All For You Artist Name: Janet Jackson Song Length (in seconds): 391 4. Unique ID: JJ456 Song Name: Black Eagle Artist Name: Janet Jackson Song Length (in seconds): 197 

9. Implement the "Output total time of playlist" menu option. Output the sum of the time of the playlist's songs (in seconds).

Ex:

OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS) Total time: 1461 seconds

PlaylistNode.h class

-------------------------------------------------

#ifndef __PLAYLIST_NODE_H #define __PLAYLIST_NODE_H

struct PlaylistNode_struct { char uniqueID[50]; char songName[50]; char artistName[50]; int songLength; struct PlaylistNode_struct* nextNodePtr; }; typedef struct PlaylistNode_struct PlaylistNode;

/* CreatePlaylistNode: Allocate a PlaylistNode in the heap using malloc * the object will be initialized with the specified unique id, song name, * artist name and song length. The nextNodePtr field will be set to NULL; * id - NULL terminated string containing unique id * songName - NULL terminated string containing the song name * artistName - NULL terminated string containing the name of the artist * songLength - int value with duration of song in seconds * * returns - Pointer to PlaylistNode allocated on the heap */ PlaylistNode* CreatePlaylistNode(char id[], char songName[], char artistName[], int songLength);

/* InsertPlaylistNodeAfter: Insert a new PlaylistNode into the linked list * immediately after the specified node. * nodeInList - Pointer to the PlaylistNode that the new PlaylistNode * should be inserted after in the list * newNode - Pointer to the new PlaylistNode to be added * returns - 0 on success, -1 on error */ int InsertPlaylistNodeAfter(PlaylistNode* nodeInList, PlaylistNode* newNode);

/* SetNextPlaylistNode: Update the nextNodePtr field of the specified nodeInList * to point to newNode. It is valid for newNode to be NULL, but not nodeInList. * nodeInList - Pointer to PlaylistNode whose nextNodePtr field will be updated * newNode - Pointer PlaylistNode to be set in the nextNodePtr * returns - 0 on success, -1 on error */ int SetNextPlaylistNode(PlaylistNode* nodeInList, PlaylistNode* newNode);

/* GetNextPlaylistNode: Return a pointer to the PlaylistNode that immediately * follows the specified node in the list * nodeInList - Pointer to PlaylistNode that we want to get the next node of * returns - Pointer to next PlaylistNode, NULL on error or end of list */ PlaylistNode* GetNextPlaylistNode(PlaylistNode* nodeInList);

/* PrintPlaylistNode: Write the fields of the PlaylistNode, nicely formatted, * to stdout (console) using printf. * thisNode - Pointer to PlaylistNode object to be displayed */ void PrintPlaylistNode(PlaylistNode* thisNode);

/* DestroyPlaylistNode: Release memory allocated by malloc in the * CreatePlaylistNode function. Does nothing if thisNode is NULL * thisNode - Pointer to PlaylistNode object to be freed. */ void DestroyPlaylistNode(PlaylistNode* thisNode);

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions