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

#include <iostream>

using namespace std;

struct node

{

    int item;

    node *next;

    node(int x, node *t)

    {

        item = x;

        next = t;

    }

};

typedef node *link;

void moveMaxToLast(link h)

{

    // first find maximum node

    link max = h;

    // iterate over given list

    while (h->next != nullptr)

    {

        // move to next node

        h = h->next;

        // if current node is greater than max

        if (h->item > max->item)

        {

            // set max to current node

            max = h;

        }

    }

    // now swap value of max with value of h, which points to last node

    int temp = max->item;

    max->item = h->item;

    h->item = temp;

}

.


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 the following change in order...
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