Question
Create a C program called dllstructure.c that implements the following: 1. Create a structure called Node having the following data members: a. Int data member
Create a C program called dllstructure.c that implements the following:
1. Create a structure called Node having the following data members:
a. Int data member to hold the data in each element
b. A pointer to Node to hold the pointer to the next element
c. A pointer to Node to hold the pointer to the previous element
2. Write a function that will loop over all the elements in the list and print their values to the screen. The signature of the function will be void print_dll(Node* head);
3. Write a function that will insert a new element in the linked list after the first occurence of the value provided by the second argument and having the value provided in the third argument. The signature of the function will be: void insert_after( Node* head, int valueToInsertAfter, int valueToBeInserted);
if valueToInsertAfter was not found in the list, insert valueToBeInserted at the end of the list. The first argument is a pointer to the linked list. Remember that you have to update the previous and next pointers where appropriate to reflect the new list after inserting the new value.
4. Write a function that will delete the first occurence of the value provided in the second argument. The signature of the function will be: void delete_element(Node* head, int valueToBeDeleted); The first argument is a pointer to the linked list. Remember that you have to update the previous and next pointers where appropriate to reflect the new list after deleting the node. If the value to be deleted was not found, do nothing.
5. Write a function that will sort the elements in ascending order. The signature of the function will be: void sort_dll(Node* head); Use bubble sort algorithm. This sorting algorithm has the reputation of being the least efficient one, however it is the simplest to implement. Let's ignore efficiency for the sake of simplicity. The algorithm is explained in this wikipedia article: https://en.wikipedia.org/wiki/Bubble_sort
6. What other function you should be adding to your code to make sure that we don't have any memory leaks? Please provide a full implementation.
The main() function that will be used to test your functions is provided as follow, please use it as the main function of your code:
int main() {
int array[5] = {11, 2, 7, 22, 4}; Node* head;
/* Question 1 */ head = create_dll_from_array(array, 5); //size of array is 5 /*
Question 2 */ print_dll(head); /*
Question 3 */ // To insert 13 after the first occurrence of 7 insert_after(head, 7, 13); // To insert 29 after the first occurrence of 21 insert_after(head, 21, 29); print_dll(head);
/* Question 4 */ delete_element(head, 22); print_dll(head); delete_element(head, 11); print_dll(head);
/* Question 5 */ sort_dll(head); print_dll(head);
/* Question 6 */ // add the call to your function here return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started