Programming Fundamentals
Download list_delete_ordered.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity list_delete_ordered
Your task is to add code to this function in list_delete_ordered.c:
// Remove any nodes in a list that are higher
// than the node directly after them.
// Return the head of the list.
// The returned list must have no disorder in it!
struct node *remove_disorder(struct node *head) {
// WRITE YOUR CODE HERE (you may need to change the line below)
return head;
}
remove_disorder
is written using the following struct that cannot be changed:
struct node { int data; struct node *next; };
The node
struct is a normal linked list node containing an integer.
remove_disorder
should take a pointer to the head of a node
list
and return the head of the node
list after it has removed any disorder
in the list. A list is considered to have disorder if there are any nodes in it
that are higher in value (using the integer data) than the node directly after
them.
remove_disorder
should remove nodes from the list, making sure to reconnect
the list back together if for example a node from the middle of the list is
removed for being disordered.
For example if the list of nodes looks like this:
{1, 3, 2}
remove_disorder
should return the head of the list, with 3 now removed
{1, 2}
However, if the list looks like this:
{2, 4, 5, 1}
remove_disorder
should return the head of the list
{1}
The 5 is definitely removed for being higher than the 1. After that, the 4 is then disordered because it is now next to the 1 and higher than it. Then, the 2 must be removed because it will be next to the 1 and higher than it.
Assumptions/Restrictions/Clarifications
- struct node cannot be edited. It must be used as it is.
- You may not use arrays in this solution. Arrays are not necessary to complete this task.
- You can assume that you'll never receive an empty list of
node
s.