Question

In: Computer Science

You will be building a linked list. Make sure to keep track of both the head...

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 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: 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. (1 pt) 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. (3 pts) 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 (3 pts) 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. (2 pts) 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.(4 pts) 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 (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: 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. (2 pt) 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). (2 pts) Ex: OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS) Total time: 1461 seconds

Solutions

Expert Solution

PlaylistNode.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

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

// Constructor
void CreatePlaylistNode
(PlaylistNode* thisNode, char* uniqueID, char* songName, char* artistName, int songLength, PlaylistNode* nextLoc);

/* Insert newNode after node.
Before: thisNode -- next
After: thisNode -- newNode -- next
*/
void InsertPlaylistNodeAfter
(PlaylistNode* thisNode, PlaylistNode* newNode);

//Set a new node to come after node see test 7
void SetNextPlaylistNode(PlaylistNode* thisNode, PlaylistNode* newNode);

// Print data
void PrintPlaylistNode(PlaylistNode* thisNode);


// Grab location pointed by nextNodePtr
PlaylistNode* GetNextPlaylistNode(PlaylistNode* thisNode);

PlaylistNode.c

#include "PlaylistNode.h"

char inputUniqueID[50];

char inputSongName[50];

char inputArtistName[50];

int inputSongLength = 0;

bool foundNode = false;

void PrintMenuHeader(char* title)

{ printf("%s PLAYLIST MENU\n", title);

printf("a - Add song\n");

printf("r - Remove song\n");

printf("c - Change position of song\n");

printf("s - Output songs by specific artist\n");

printf("t - Output total time of playlist (in seconds)\n");

printf("o - Output full playlist\n");

printf("q - Quit\n\n");

}

void PrintMenu(char* title)

{

PlaylistNode* headObj = NULL; // Create PlaylistNode objects

PlaylistNode* currObj = NULL;

PlaylistNode* lastObj = NULL;

PlaylistNode* thisIsTheNode = NULL; //pointer to the node being moved

PlaylistNode* wasBeforeTheNode = NULL;

PlaylistNode* wasAfterTheNode = NULL; //pointer to the next position

PlaylistNode* theNewBefore = NULL; //node pointing to the new position

PlaylistNode* theNewAfter = NULL; //what comes after

int songNumber = 0;

int position = 0;

int newPosition = 0;

//int countUpTo = 0;

if (headObj == NULL){

headObj = (PlaylistNode*)malloc(sizeof(PlaylistNode)); // Front of nodes list

CreatePlaylistNode(headObj, "", "", "", 0, NULL) ;

lastObj = headObj;

} //DO NOT STORE REAL DATA HERE!


PrintMenuHeader(title);

char c = '\0';

// char name[100];

while (c != 'q')

{

printf("Choose an option:\n");

scanf(" %c", &c);

switch (c)

{

case 'a' :

printf("ADD SONG\n");

printf("Enter song's unique ID:\n");

scanf(" %100[^\n]", inputUniqueID);

printf("Enter song's name:\n");

scanf(" %100[^\n]", inputSongName);

printf("Enter artist's name:\n");

scanf(" %100[^\n]", inputArtistName);

printf("Enter song's length (in seconds):\n");

scanf("%d", &inputSongLength);

if (headObj == NULL){

headObj = (PlaylistNode*)malloc(sizeof(PlaylistNode)); // Front of nodes list

CreatePlaylistNode(headObj, "", "", "", 0, NULL) ;

lastObj = headObj;

} //DO NOT STORE REAL DATA HERE!

else {

currObj = (PlaylistNode*)malloc(sizeof(PlaylistNode));

CreatePlaylistNode(currObj, inputUniqueID, inputSongName, inputArtistName, inputSongLength , NULL);

InsertPlaylistNodeAfter(lastObj, currObj); // Append curr

lastObj = currObj; // Curr is the new last item

}

PrintMenuHeader(title);

break;

case 'r' :

printf("REMOVE SONG\n");

printf("Enter song's unique ID:\n");

scanf(" %50[^\n]", inputUniqueID);

currObj = headObj;

lastObj = NULL;

do {

if (strcmp(currObj->uniqueID, inputUniqueID) == 0) { //found it

foundNode = true;

lastObj->nextNodePtr = currObj->nextNodePtr;

printf("\"%s\" removed.\n\n", currObj->songName);

free(currObj);

}

else { //go to the next node

lastObj = currObj;

currObj = currObj->nextNodePtr;

}

} while ((foundNode == false) && (currObj != NULL));

foundNode = false;

PrintMenuHeader(title);

break;

case 'c' :

printf("CHANGE POSITION OF SONG\n");

printf("Enter song's current position:\n");

scanf("%d", &position);

printf("Enter new position for song:\n");

scanf("%d", &newPosition);

lastObj = headObj;

currObj = headObj->nextNodePtr;

songNumber = 1;

while (currObj->nextNodePtr != NULL) {

if (songNumber == position){

wasBeforeTheNode = lastObj;

thisIsTheNode = currObj; //pointer to node that is moving

wasAfterTheNode = currObj->nextNodePtr;

}

if ((position > newPosition) && (songNumber == newPosition - 1)) { //the node before its new place

theNewBefore = currObj;

theNewAfter = currObj->nextNodePtr;

}

if ((position < newPosition) && (songNumber == newPosition)) { //where the node will go

theNewBefore = currObj;

if (currObj->nextNodePtr != NULL) {

theNewAfter = currObj->nextNodePtr;

}

}

lastObj = currObj;

currObj = currObj->nextNodePtr;

songNumber++;

}

thisIsTheNode->nextNodePtr = theNewAfter; // moved here 20170729-0653

theNewBefore->nextNodePtr = thisIsTheNode;

// from here 20170729-0653

wasBeforeTheNode->nextNodePtr = wasAfterTheNode;

printf("\"%s\" moved to position %d\n", thisIsTheNode->songName, newPosition);

PrintMenuHeader(title);

break;

case 's' :

printf("OUTPUT SONGS BY SPECIFIC ARTIST\n");

printf("Enter artist's name:\n");

scanf(" %50[^\n]", inputArtistName);

currObj = headObj;

songNumber = 1;

while (currObj != NULL) {

if (strcmp(inputArtistName, currObj->artistName) == 0) {

printf("%d.\n", songNumber);

PrintPlaylistNode(currObj);

}

currObj = GetNextPlaylistNode(currObj);

songNumber++;

}

break;

case 't' :

printf("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)\n");

int sum = 0;

currObj = headObj;

//lastObj = NULL;

while (currObj != NULL) {

sum += currObj->songLength;

currObj = GetNextPlaylistNode(currObj);

}

printf("Total time: %d seconds\n", sum);

break;

case 'o' :

printf("%s - OUTPUT FULL PLAYLIST\n", title);

if(headObj->nextNodePtr == NULL) {

printf("Playlist is empty\n");

}

else{

currObj = headObj->nextNodePtr;

songNumber = 1;

while (currObj != NULL) {

printf("%d.\n", songNumber);

PrintPlaylistNode(currObj);

currObj = GetNextPlaylistNode(currObj);

songNumber++;

}

}

PrintMenuHeader(title);

break;

}

}

}

// Constructor yes

void CreatePlaylistNode (PlaylistNode* thisNode, char* uniqueID, char* songName, char* artistName, int songLength, PlaylistNode* nextLoc) {

strcpy(thisNode->uniqueID, uniqueID);

strcpy(thisNode->songName, songName);

strcpy(thisNode->artistName, artistName);

thisNode->songLength = songLength;

thisNode->nextNodePtr = nextLoc;

return;

}

//yes

void InsertPlaylistNodeAfter

(PlaylistNode* thisNode, PlaylistNode* newNode) {

PlaylistNode* tmpNext = NULL;

tmpNext = thisNode->nextNodePtr;

// Remember next

thisNode->nextNodePtr = newNode; // this -- new -- ?

newNode->nextNodePtr = tmpNext; // this -- new -- next

return;

}

//no

void SetNextPlaylistNode(PlaylistNode* thisNode, PlaylistNode* newNode) { //Set a new node to come after node (1 pt)

thisNode->nextNodePtr = newNode;

return;

}

// Print data yes

void PrintPlaylistNode(PlaylistNode* thisNode) {

printf("Unique ID: %s\n",thisNode->uniqueID);

printf("Song Name: %s\n",thisNode->songName);

printf("Artist Name: %s\n",thisNode->artistName);

printf("Song Length (in seconds): %d\n", thisNode->songLength);

printf("\n");

return;

}

// Grab location pointed by nextNodePtr

PlaylistNode* GetNextPlaylistNode(PlaylistNode* thisNode) {

return thisNode->nextNodePtr;

}

main.c:-

#include "PlaylistNode.c"


char playlistTitle[50] = "";

void PrintMenu(char* title);

int main(void) {

printf("Enter playlist's title:\n\n");

scanf(" %100[^\n]", playlistTitle);


PrintMenu(playlistTitle);


return 0;
}

========================================================================

Note: Could you please consider my effort on this work and give me a UPVOTE. Thank you :)


Related Solutions

You will be building a linked list. Make sure to keep track of both the head...
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 - Class declaration PlaylistNode.cpp - Class definition main.cpp - 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. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt) SetNext() - Mutator...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please 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 - Class declaration PlaylistNode.cpp - Class definition main.cpp - 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. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
8.13 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep...
8.13 LAB: Warm up: Contacts 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. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
C++ Only Please 10.15 LAB: Warm up: Contacts You will be building a linked list. Make...
C++ Only Please 10.15 LAB: Warm up: Contacts 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. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext()...
Assume that you have a linked list of records. Assume that you have a head, a...
Assume that you have a linked list of records. Assume that you have a head, a current, and a tail pointer. Write an algorithm that DELETES the node BEFORE the current node. You can use pseudo-code, English or drawing to describe your solution.( this was, and remains to be, a popular technical interview question)
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT