Question

In: Computer Science

The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1...

The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1 & 2) struct node { int item; node* next; node(int x, node* t) { item = x; next = t; } }; typedef node* link; Write a C++ function void moveMaxToLast(link h) that takes as a parameter a link to the head of a linked list L. After this function performs, L still contains the same item values with the following change in order of values: the function moves the maximum value in the item field to the last in L. You can assume (it is given) that L has at least two nodes, and item values in all nodes are distinct. Hint: This can be done by finding a link to the node whose item field is maximum in L, and finding a link to the last node. Then the item values in both nodes can be swapped (interchanged). Note that this does not require any delete or insert of nodes. void moveMaxToLast(link h) {

Solutions

Expert Solution

Please find all the code explanations within the code itself as comments.

We have added some snippets of our code for clarification of some points. The full code can be found at the end.

We are given the definition of the node structure in the problem statement:

// the definition of the structure as given in the problem statement
struct node { 

        int item; 
        node* next; 

        node(int x, node* t) { 
                item = x; 
                next = t; 
        } 
};

We need to design a function moveMaxToLast(link h) which interchanges the item values of the node with maximum item value and the last node of the linked list.

Therefore, we need to do this (according to the problem statement) :

1. Find the link to the last node of the linked list. (we will create a helper function for this)

2. Find the link to the maximum item value node of the linked list. (we will create a helper function for this)

To get the link of the last node of our linked list, we need to traverse till the end of our linked list, therefore, the running time complexity for this operation will be:

Similarly, to get the link of the maximum item value node, again, we need to traverse till the end of our linked list, therefore, the running time complexity for this operation will be again:

Therefore, the overall running time of our algorithm will be:

(

Please note that to test our code, we have used a very simple main function implementation for clarity and we have not considered this for our main function(although, it's running time complexity is the same).

We have created the linked list by adding new nodes at the front of the list. Also, because in the constructor, we need to provide the next link of the new node we are creating, we can easily add the new nodes at the front of the list by using the next to the new node as the head of the current list. Have a look at the code snippet of the main function.

link head = NULL;

// first node 
head = new node (1, NULL);

// just for testing the code we have written, 
// we are creating a linked list in a random fashion

// we will push the new nodes to the front of the existing list
// we will use the constructor of the linked list for that

// because everytime we are pushing in the front
// the head of the linked list will change 

// second node 
head = new node (5, head);

// third node 
head = new node (10, head);

// forth node 
head = new node (7, head);

// fifth node
head = new node (3, head);

)

Here is the full code:

# include <iostream>
# include <climits>
using namespace std;

// the definition of the structure as given in the problem statement
struct node { 

        int item; 
        node* next; 

        node(int x, node* t) { 

                item = x; 
                next = t; 
        } 
};

// the typedef statement as provided in the problem statement
typedef node* link;

// this is a helper function used to find the link to the node 
// with maximum value in the linked list 
link findMaxValueNode (link h) {

        // we will create a link to traverse the list
        link ptr = h;

        // we will save the link with maximum value 
        // we will initialize it with the head of the list
        link maxValueNode = h;
                
        // we will traverse the list until we hit the NULL
        while (ptr != NULL) {
                
                // check if the list item value is greater than 
                // our maxValueNode item value

                // if yes, then, update the maxValueNode link
                if ((ptr -> item) > (maxValueNode -> item)) {
                        maxValueNode = ptr;     
                }

                // after checking the node, we will update 
                // the ptr link to point to the next node
                ptr = ptr -> next;
        }

        return maxValueNode;
}


// this is a helper function used to find the link to the last node 
// in the linked list 
link findLastNode (link h) {
                
        // we will save the link to the last node in this variable
        link lastNode = h;

        // because, the last node of the linked list will have its next equal to NULL
        // we will traverse until the next link is NULL
        while (lastNode -> next != NULL) {
                // the lastNode link to point to the next node
                lastNode = lastNode -> next;
        }

        return lastNode;
}

// this function will interchange the maximum value node
// and the last node
void moveMaxToLast(link h) {

        // get the link to the last node
        link lastNode = findLastNode (h);

        // get the link to the maximum item value node
        link maxItemValueNode = findMaxValueNode (h);

        // swap the item values present in both the nodes
        swap (lastNode -> item, maxItemValueNode -> item);
}

void print (link head) {
                
        // create a link to traverse the list
        link ptr = head;

        // traverse until NULL is encountered
        while (ptr != NULL) {

                // print the list
                cout << ptr -> item << " ";

                // update the ptr link to point to the next node
                ptr = ptr -> next;
        }
}

int main (void) {
                
        link head = NULL;

        // first node 
        head = new node (1, NULL);

        // just for testing the code we have written, 
        // we are creating a linked list in a random fashion

        // we will push the new nodes to the front of the existing list
        // we will use the constructor of the linked list for that

        // because everytime we are pushing in the front
        // the head of the linked list will change 

        // second node 
        head = new node (5, head);

        // third node 
        head = new node (10, head);

        // forth node 
        head = new node (7, head);

        // fifth node
        head = new node (3, head);

        // printing the linked list before the call to moveMaxToLast() function
        cout << "\n\nLinked list before the moveMaxToLast() call: ";
        print (head);

        // interchange the item values of the node with maximum item value and the last node of the linked list
        moveMaxToLast (head);

        cout << "\nLinked list after the moveMaxToLast() call: ";
        // printing the linked list after the call to moveMaxToLast() function
        print (head);
        cout << "\n\n";


        return 0;
}
                

Please refer to the screenshots of the code for understanding the indentation of the code.

Let's take a look at the input-output:

Note the change in position of the max item value node (10) and the last node (1).


Related Solutions

The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1 & 2)     struct node    { int item; node* next;            node(int x, node* t)          { item = x; next = t; }     };     typedef node* link; Write a C++ function void moveMaxToLast(link h) that takes as a parameter a link to the head of a linked list L. After this function performs, L still contains the same item values with...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Complete the following program:LinkedQueue.zip package jsjf; /** * Represents a node in a linked list. */...
Complete the following program:LinkedQueue.zip package jsjf; /** * Represents a node in a linked list. */ public class LinearNode<T> {    private LinearNode<T> next;    private T element;    /**    * Creates an empty node.    */    public LinearNode()    {        next = null;        element = null;    }    /**    * Creates a node storing the specified element.    * @param elem element to be stored    */    public LinearNode(T elem)...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
Complete the following program: LinkedStack.zip package jsjf; /** * Represents a node in a linked list....
Complete the following program: LinkedStack.zip package jsjf; /** * Represents a node in a linked list. */ public class LinearNode<T> {    private LinearNode<T> next;    private T element;    /**    * Creates an empty node.    */    public LinearNode()    {        next = null;        element = null;    }    /**    * Creates a node storing the specified element.    * @param elem element to be stored    */    public LinearNode(T...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT