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...
ONLY looking for part B!! a. Using C++, define a node structure of the linked list...
ONLY looking for part B!! a. Using C++, define a node structure of the linked list (e.g. value is an integer, next is a node type pointer), construct a linked list of 10 nodes and assign random numbers as the nodes’ values. Use loop to track and print from the first node to the last and output all nodes’ values. Finally, free all memories of the linked list. b. Based on 2.a, (1) define a function which takes the header...
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)...
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
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...
can u give me an example of how to removing Double Linked List a node at...
can u give me an example of how to removing Double Linked List a node at a given location in java the method should be like this: public void removeFromLocation(int location) { }                                }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT