Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Delete first element containing a specific string from a linked list Download l i s t d e l e t e c o n

Delete first element containing a specific string from a linked list
Download listdeletecontainsstring.c here, or copy itto your CSE account using the following command:
cp-n?importreedA?dp1091publichtml24T2activitieslistdeletecontainsstringlistdeletecontainsstring.c.
Your task isto add code to this function inlistdeletecontainsstring.c:
???
Delete the first node in the list containing the specific string???
The deleted node is freed.???
Ifno node contains the specified string, the list is not changed???
The head of the list is returned.
struct node *deletecontains(char string[MAXSIZE], struct node *head){
return NULL;
}
Note listdeletecontainsstring.c uses the following familiar data type:
struct node {
struct node *next;
char data[MAXSIZE];
};
deletecontains is given two argument, value and head.
data is a string with max size of100 characters
head is the pointer to the first node in a linked list
The function is designed to delete the first node in the linked list that contains a specific string.
Add code todeletecontains so that it deletes the first node in the linked list that whose data field equals a specified string and returns a pointer to the new list.
If data does not occur in the linked list, the list should not be changed.
If data occurs more than once in the linked list, only the first occurrence should be deleted. Note, if the list is now empty deletecontains should return NULL.
deletecontains should call free to free the memory of the node it deletes.
Testing
listdeletecontains.c also contains a main function which allows you to test your deletecontains function.
This main function:
takes in command line arguments
converts the command line inputs to a linked list,
assigns a pointer to the first node in the linked list to head,
reads a sstring from standard input and assigns itto data,
calls deletecontains(data, head) and
prints the result.
Do not change this main function. If you want to change it, you have misread the question.
If you're trying to leak-check your code locally and getting errors - you'll need to free the entire list at the end of your main function! You can run leak-check by adding the --leak-check option to your dcc command i.e.dcc--leak-check deletefirst.c-odeletefirst Check the updated code sample
Your deletecontains function will be called directly in marking. The main function is only to let you test your deletecontains function
dcclistdeletecontainsstring.c-olistdeletecontainsstring
.listdeletecontainsstring cats sleep 16to18 hours per day
Enter a string to delete: cats
[sleep,16,to,18, hours, per, day]
.listdeletecontainsstring the bumblebee bat is the worlds smallest mammal
Enter a string to delete: bumblebee
[the, bat, is, the, worlds, smallest, mammal]
.listdeletecontainsstring
Enter a string to delete: hello
[]
.listdeletecontainsstring there are parts of Africa in all four hemispheres
Enter a string to delete: four
[there, are, parts, of, Africa, in, all, hemispheres]
.listdeletecontainsstring the most money ever paid for a cow inan auction was 1.3 million dollars
Enter a string to delete: dollars
[the, most, money, ever, paid, for, a, cow, in,an, auction, was, 1.3, million]
AssumptionsRestrictionsClarifications
deletecontains should call free to free the memory for the node it deletes
deletecontains should not change the data fields of list nodes.
deletecontains should not use arrays.
deletecontains should not call malloc.

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_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

Evaluate the importance of the employee handbook.

Answered: 1 week ago

Question

Discuss the steps in the progressive discipline approach.

Answered: 1 week ago