Question

In: Computer Science

Please use C programming to write the code to solve the following problem. Also, please use...

Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially:

void inputStringFromUser(char *prompt, char *s, int arraySize);

void songNameDuplicate(char *songName);

void songNameFound(char *songName);

void songNameNotFound(char *songName);

void songNameDeleted(char *songName);

void artistFound(char *artist);

void artistNotFound(char *artist);

void printMusicLibraryEmpty(void);

void printMusicLibraryTitle(void);

const int MAX_LENGTH = 1024;

You will write a program that maintains information about your personal music library using a Linked List data structure. The program will allow you to add and delete entries from your personal music library, to search your personal music library for songs by song name, and to print out the entire list of your library. This lab will be due in the week of April 1.

Your Personal Music Library. The data in your personal music library will be stored in memory with the use of a linked list, with one list node per song. Each node will contain three strings: a song’s name, its artist, and its genre (the type of music). The linked list must be kept in sorted alphabetical order, by song name, beginning with A through Z (i.e. increasing alphabetical order). No two songs in your personal music library should have the same name. Your program should be “menu” driven, with the user being offered a choice of the following five “commands”:
Command I. Insert a new song into the library. The program should prompt the user for a new song name, its artist’s name, and its genre. This information must be placed in a new node that has been created using the malloc function (to be clear, you must use malloc for this purpose). This node should then be inserted at the appropriate (alphabetical) position in the linked list. Don’t forget that the music library must be stored in increasing order, by song name. If a node with the given song name is already in the music library, an error message should be output, and the new node should not be inserted into the linked list.
Command D. Delete an entry from your the library. The program should prompt the user for the name of the song to be deleted, and then find and delete the node containing that song name from the library. If no node with the given song name is found, an error message should be output. All memory allocated for a deleted entry must be released back to the system using the free function. This includes not only the memory allocated for the node, but also the strings in the node that would have been separately allocated.
Command S. Search for a user supplied song name in the library. The program should print the name, artist, and genre of the song, with each piece of information on a separate line. If no node with the given song name is found, an error message should be output.
Command P. Print your personal music library, in alphabetical order by song name. Print the song name, artist, and genre of each song, each on a separate line. A blank line should be printed between each song.
Command Q. Quit the program. When the program is given the Q command, it should delete all of the nodes in the linked list, including all the strings contained

in each node. Deletion means both removing from the list, but also freeing all dynamically allocated memory using call the free function. It should then print the (what should be an empty) linked list.
To assist you in the production of your program, we have provided you with a file, musiclibrary.c, that contains part of the complete program. This program is provided on the course website along with this lab. This “skeleton” of the lab 9 program includes all of the C statements required to implement the menu driven parts of the program. It also includes a few helpful functions for reading data and printing messages. You should take this file and edit it to become your version of Lab9.c. Note, however, that you may not change any of the code in the existing implementation of the skeleton program, except where indicated in comments. In particular, you must use the inputStringFromUser() function and the prompts provided to obtain inputs from the user, and you must use the given Node structure. In addition we strongly recommend that you do your work for this lab in the following way:

• Read the entire skeleton program carefully. Take note of the provided functions for reading strings, printing the name, artist and genre of a song, and for printing error messages. Using these functions will make it easier for you to satisfy the exercise and marker programs.
• Add the function for inserting a new node(the I command) into the linked list. Your function will need to read the name, artist, and genre of a song. Test your program by trying to insert nodes into the linked list. Try to insert nodes with both new and duplicate song names.
• Add a function for printing the linked list (the P command). Test your program by inserting songs into the linked list and then printing them out. Are the entries in the correct order? • Add a function that searches the linked list for a given song name and then either prints the appropriate song or, if a node is not found, prints an error message. This is the S command.
• Add the statements that need to be executed when the Q command is entered. These statements should delete the linked list by using calls to the free function. To check your work, print the linked list after the elements have been deleted. • Add a function for deleting a song from the personal music library. It will need to search the linked list for a given song name, delete the appropriate node from the linked list, and then use the free function to release the memory used to store the node, as well as all the memory that the node uses for storing strings. If the given song name is not found in the music library, print an error message.
We recommend that you test your program after attempting to complete each step. This way, if your program no longer works, you will know which statements are causing the error. Complete each step before moving on to the next one.

Sample Output From Executing The Program
Here is a sample output from an execution of the program that you are to prepare.

Personal Music Library.
Commands are I (insert), D (delete), S (search by song name), P (print), Q (quit).
Command --> P
The music library is empty.
Command --> I
Song name --> The Shade
Artist --> Metric
Genre --> Rock

Command --> I
Song name --> Heads Will Roll
Artist --> Yeah Yeah Yeahs
Genre --> Punk

Command --> I
Song name --> Bad Boys Need Love Too
Artist --> Bahamas (Afie Jurvanen)
Genre --> Folk

Command --> P

My Personal Music Library:

Bad Boys Need Love Too
Bahamas (Afie Jurvanen)
Folk

Heads Will Roll
Yeah Yeah Yeahs
Punk

The Shade
Metric
Rock

Command --> I
Song name --> Heads Will Roll
Artist --> Yeah Yeah Yeahs
Genre --> Punk

A song with the name 'Heads Will Roll' is already in the music library.
No new song entered.

Command --> I
Song name --> Adult Diversion
Artist --> Alvvays
Genre --> Pop

Command --> P

My Personal Music Library:

Adult Diversion
Alvvays
Pop

Bad Boys Need Love Too
Bahamas (Afie Jurvanen)
Folk

Heads Will Roll
Yeah Yeah Yeahs
Punk

The Shade
Metric
Rock

Command --> S

Enter the name of the song to search for --> Bad Boys Need Love Too

The song name 'Bad Boys Need Love Too' was found in the music library.

Bad Boys Need Love Too
Bahamas (Afie Jurvanen)
Folk

Command --> S

Enter the name of the song to search for --> Young Blood

The song name 'Young Blood' was not found in the music library.

Command --> D

Enter the name of the song to be deleted --> The Shade

Deleting a song with name 'The Shade' from the music library.

Command --> P

My Personal Music Library:

Adult Diversion
Alvvays
Pop

Bad Boys Need Love Too
Bahamas (Afie Jurvanen)
Folk

Heads Will Roll
Yeah Yeah Yeahs
Punk

Command --> Q

Deleting a song with name 'Adult Diversion' from the music library.

Deleting a song with name 'Bad Boys Need Love Too' from the music library.

Deleting a song with name 'Heads Will Roll' from the music library.

The music library is empty.

Solutions

Expert Solution

Program:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node   
{
char name[40],artist[40],genre[40];
struct node *next;
};
struct node *head=NULL;   
struct node *getnode(char *n,char *a,char *g)
{
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node));
strcpy(ptr->name,n);
strcpy(ptr->artist,a);
strcpy(ptr->genre,g);
ptr->next=NULL;
return ptr;
}
struct node* search(char *n)   
{
struct node *s;
int flag=0;
if(head==NULL)
return NULL;
s=head;
while(s!=NULL)
{
if(strcmp(s->name,n)==0)
return s;
s=s->next;
}
return NULL;
}
void insert()   
{
char n[40],a[40],g[40];
struct node *ptr,*s,*s1;
int flag=0;
fflush(stdin);
printf("\nEnter the song name:");
gets(n);
printf("\nEnter the artist name: ");
gets(a);
printf("\nEnter the genre: ");
gets(g);
s=search(n);
if(s!=NULL)
{
printf("\nSong already present in list. Insertion not possible.\n");
return;
}
ptr=getnode(n,a,g);
if(head==NULL)
head=ptr;
else if(strcmp(ptr->name,head->name)<0)
{
ptr->next=head;
head=ptr;
}
else
{
s=head;
s1=head->next;
while(s1!=NULL)
{
if((strcmp(ptr->name,s->name)>=0)&&((strcmp(ptr->name,s1->name)<0)))
{
s->next=ptr;
ptr->next=s1;
flag=1;
break;
}
else
{
s=s->next;
s1=s1->next;
}
}
if(flag==0)
s->next=ptr;
}
}
void deletesong(char *s)   
{
struct node *a1,*a2,*a3;
int flag=0;
printf("\nDeleting song named %s from music library\n",s);
a1=head;
a2=head->next;
if(a1->next==NULL)
{
head=NULL;
free(a1);
}
else if(a2->next==NULL)
{
if(strcmp(s,a1->name)==0)
{
head=a2;
free(a1);
}
else
{
head=a1;
head->next=NULL;
free(a2);
}
}
else
{
if(strcmp(s,a1->name)==0)
{
head=a2;
free(a1);
}
else
{
while((a2->next)!=NULL)
{
if(strcmp(s,a2->name)==0)
{
a3=a2->next;
a1->next=a3;
free(a2);
flag=1;
break;
}
else
{
a1=a1->next;
a2=a2->next;
}
}
if(flag==0)
{
a1->next=NULL;
free(a2);
}
}
}
}
void display()
{
struct node *s;
if(head==NULL)
printf("\nMusic Library is empty\n");
else
{
s=head;
printf("\nMusic library contents: ");
while(s!=NULL)
{
printf("\nSong: %s\nArtist: %s\nGenre: %s\n\n",s->name,s->artist,s->genre);
s=s->next;
}
}
}
void emptylibrary()   
{
struct node *s,*p;
p=head;
while(p!=NULL)
{
s=p;
p=p->next;
deletesong(s->name);
}
display();
}
main()
{
char ch,s[40];
struct node *p;
while(1)   
{
printf("\nPersonal music library.");
printf("\nI.Insert a new song");
printf("\nD.Delete a song existing in library.");
printf("\nS.Search a song in library.");
printf("\nP.Print entire music library");
printf("\nQ.Empty Music Library");
printf("\nE.Exit");
printf("\nEnter your choice: ");
fflush(stdin);
scanf("%c",&ch);
switch(ch)
{
case 'I':
insert();
break;
  
case 'D':
fflush(stdin);
printf("\nEnter song name to delete: ");
gets(s);
p=search(s);
if(p==NULL)
printf("\nSorry,song not found in library");
else
deletesong(s);
break;
  
case 'S':
fflush(stdin);
printf("\nEnter song name to search: ");
gets(s);
p=search(s);
if(p==NULL)
printf("\nSorry,song not found in library");
else
{
printf("\nSong %s found in library.\n",s);
printf("\nSong name: %s\nArtist name: %s\nGenre: %s\n",p->name,p->artist,p->genre);
}
break;
  
case 'P':
display();
break;
  
case 'Q':
emptylibrary();
break;
  
case 'E':
exit(0);
  
default:
printf("\nInvalid case.\n");
}
}
}


Related Solutions

PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
Use the method of this section to solve the linear programming problem. Minimize   C = 2x...
Use the method of this section to solve the linear programming problem. Minimize   C = 2x − 3y + 6z subject to   −x + 2y − z ≤ 9 x − 2y + 2z ≤ 10 2x + 4y − 3z ≤ 12 x ≥ 0, y ≥ 0, z ≥ 0   The minimum is C =   at (x, y, z) =    .
Please write this code in C++, also if you could please bold the names of the...
Please write this code in C++, also if you could please bold the names of the input files and provide comments for each choice. For this part, the program gives the user 4 choices for encrypting (or decrypting) the first character of a file. Non-lowercase characters are simply echoed. The encryption is only performed on lowercase characters. If c is char variable, then islower(c) will return true if c contains an lowercase character, false otherwise To read a single character...
PLEASE USE R PROGRAMMING TO SOLVE THIS AND PASTE A COPYABLE CODE Photoresist is a light-sensitive...
PLEASE USE R PROGRAMMING TO SOLVE THIS AND PASTE A COPYABLE CODE Photoresist is a light-sensitive material applied to semiconductor wafers so that the circuit pattern can be imaged on to the wafer. After application, the coated wafers are baked to remove the solvent in the photoresist mixture and to harden the resist. Here are measurements of photoresist thickness (in kA) for eight wafers baked at two different temperatures. We want to see whether different temperatures make difference. The data...
please write the code in C not c++, and not to use Atoi or parseint to...
please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you. #include <stdio.h> #include <stdbool.h> /* * The isinteger() function examines the string given as its first * argument, and returns true if and only if the string represents a * well-formed integer. A well-formed integer consists only of an * optional leading - followed by one or more decimal digits. * Returns true if the given string represents an...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. Your solution must include a function called isUnique() that returns 1 (true) if the number input is...
Write a case you can use PCR to solve the problem and also write a case...
Write a case you can use PCR to solve the problem and also write a case you cannot use PCR to solve to problem
3. Solve the following linear programming problem. You must use the dual. First write down the...
3. Solve the following linear programming problem. You must use the dual. First write down the dual maximization LP problem, solve that, then state the solution to the original minimization problem. (a) Minimize w = 4y1 + 5y2 + 7y3 Subject to: y1 + y2 + y3 ≥ 18 2y1 + y2 + 2y3 ≥ 20 y1 + 2y2 + 3y3 ≥ 25 y1, y2, y3 ≥ 0 (b) Making use of shadow costs, if the 2nd original constraint changed...
Solve the following linear programming problem. You must use the dual. First write down the dual...
Solve the following linear programming problem. You must use the dual. First write down the dual maximization LP problem, solve that, then state the solution to the original minimization problem. (a) Minimize w = 4y1 + 5y2 + 7y3 Subject to: y1 + y2 + y3 ≥ 18 2y1 + y2 + 2y3 ≥ 20 y1 + 2y2 + 3y3 ≥ 25 y1, y2, y3 ≥ 0 (b) Making use of shadow costs, if the 2nd original constraint changed to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT