Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: zz-x2580
SampleProblem
Task 3: Add a recursive function, printReverseList, that will print the elements of the linked list in the reverse order, without changing thelist.
LinkedList: In this question, you have been provided with an incomplete code for a Dynamic Linked List. Your task is to complete the code by adding some functionalities to the program. Before describing the tasks, read the following initial notes, that show some differences between the linked list program we saw in the course and the one you are going to develop in this question:
Initial Notes:
-
The data of the structure has only one integer. This means every node of the self-referential structure has one integer as data and a pinter to the same structure.
-
You must not only keep a pointer to the beginning of the linked list, startPtr, but also keep a pointer to the last node of the linked list, endPtr. Note that it is not a doubly-linked list. It is a single linked list, with one extra pointer that points to the end of the list.
-
We do not have the two functions, insert and delete, in this program.
-
The linked list that is generated is not ordered. The elements will be appended to the beginning or end of the list depending on the add function used.
-
The functions isEmpty, printList, and menu, have already been developed, so you should not alter them. You just use them inside the other parts that you mustcomplete.
Tasks:
-
Task 1: Complete the function addFirst, that will add a new element to the beginning of the linked list.
-
Task 2: Complete the function addLast, that will add a new element to the end of the linked list.
Note that in both functions above, you must update the starting and ending pointers, when needed.
-
Task 3: Add a recursive function, printReverseList, that will print the elements of the linked list in the reverse order, without changing thelist.
-
Task 4: Add a function, reverseList, that will reverse the list, in-place. This means at the end you have the same elements in the linked list but in the reverse order. Note that “in-palce” means that you should not use any other dynamic data structure, like another linked list, stack or queue. You must reverse the list itself.
-
Task 5: Add a function, emptyList, that will safely empty a list. “Safely” means that you should make sure that you won’t make any memory leakage when you empty a list.
-
Task 6: Add a function, merge, that merges two ordered lists into a new linked list. The new list must have all the elements from the two original lists, ordered. No need to worry about any possible duplicates. This means if there are similar nodes inside the two lists, they will be copied into the new list one after another.
-
Task 7: Inside the main function, based on the comments provided, complete the code by calling the proper functions, along with adding some other required statements.
To fulfill the above tasks, you must complete the C program, LinkedList.c, that is provided for you.
*Note: You should first CAREFULLY READ THE COMMENTSprovided for each part of the program, and then start completing it.
*Note: DO NOT ALTER/DELETE any part of the existing code. ONLY complete the incompleteparts.
Sample execution of the program:
Enter your choice:
1to add a new element to the beginning of the
2to add a new element to the end of the
3to print the list in the reverse
4to reverse the list,in-place.
5to merge two ordered lists into a new ordered
6to end
? 1
Enter an integer number: 14 The list is:
14 –> NULL
Enter your choice:
1to add a new element to the beginning of the
2to add a new element to the end of the
3to print the list in the reverse
4to reverse the list,in-place.
5to merge two ordered lists into a new ordered
6to end
? 1
Enter an integer number: 23 The list is:
23 –> 14 –> NULL
Enter your choice:
1to add a new element to the beginning of the
2to add a new element to the end of the
3to print the list in the reverse
4to reverse the list,in-place.
5to merge two ordered lists into a new ordered
6to end
? 2
Enter an integer number: 41 The list is:
23 –> 14 –> 41 –> NULL
Enter your choice:
1to add a new element to the beginning of the
2to add a new element to the end of the
3to print the list in the reverse
4to reverse the list,in-place.
5to merge two ordered lists into a new ordered
6to end