Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 19, Problem 4AW

Explanation of Solution

Method definition for “removeMin()”:

The method definition for “removeMin()” is given below:

/* Method definition for "removeMin()" */

String removeMin()

{

    /* If the list head is null, then */

    if (first == null)

        //Returns null

        return null;

    /* Set minimum string to "first" node */

    Node minimumString = first;

    /* Set minimum string predecessor to list head*/

    Node minimumPred = first;

    /* Set reference node to next node of list head */

    Node refNode = first.next;

    /* Set the reference of predecessor node */

    Node refPred = first;

/* If the reference node "refNode" is not "null", then */

    while (refNode != null)

    {

        /* Check condition */

if (refNode.value.compareTo(minimumString.value) < 0) 

        {

            /* Assign minimum string to "refNode" */

            minimumString = refNode;

/* Set minimum predecessor to reference of predecessor */

            minimumPred = refPred;

        }

        /* Set "refPred" to "refNode*/

        refPred = refNode;

        /* Set "refNode" to next reference node */

        refNode = refNode.next;

    }

    // Compute If the first node is the minimum or not

    String resultantString = minimumString.value;

    /* If the minimum string is list head, then */

    if (minimumString == first)

    {

        //Remove the first string

        first = first.next;

        /* If the list head is "null", then */

        if (first == null)

            /* Set "last" to "null" */

            last = null;

    }

    //Otherwise

    else

    {

        //Remove an element with a predecessor

        minimumPred.next = minimumString.next;

        // If the last item removed, then

        if (minimumPred.next == null)

            /* Assign "last" to "minimumPred" */

            last = minimumPred;

    }

    /* Finally returns the resultant string elements */

    return resultantString;

}

Explanation:

The above method definition is used to remove a minimum element from a list.

  • If the list head is null, then returns null.
  • Set minimum string and minimum predecessor to “first” node.
  • Set reference node to next node of list head and also set the reference of predecessor node.
  • Performs “while” loop. This loop will perform up to the “refNode” becomes “null”.
    • Check condition using “if” loop.
      • If the given condition is true, then assign minimum string to “refNode”.
      • Set minimum predecessor to reference of predecessor.
    • Set “refPred” to “refNode”.
    • Set “refNode” to next reference node.
  • Compute if the first node is minimum or not.
  • If the minimum string is list head, then
    • Remove the first string.
    • If the list head is “null”, then set “last” to “null”.
  • Otherwise,
    • Remove an element with a predecessor.
    • If the last item removed, then assign “last” to “minimumPred”.
  • Finally returns the resultant string elements.

Complete code:

The complete executable code for remove a minimum string element from a linked list is given below:

//Define "LinkedList1" class

class LinkedList1

{

/** The code for this part is same as the textbook of "LinkedList1" class */

    /* Method definition for "removeMin()" */

    String removeMin()

    {

        /* If the list head is null, then */

        if (first == null)

            //Returns null

            return null;

        /* Set minimum string to "first" node */

        Node minimumString = first; 

/* Set minimum string predecessor to list head*/

        Node minimumPred = first;  

/* Set reference node to next node of list head */

        Node refNode = first...

Blurred answer
Students have asked these similar questions
Complete the method printSkipper below. Use a loop to print every other element of a linked list parameter words. Print a space after every word and a newline after all elements have been printed. For example, if the list contains the strings "A", "B", and "C", print A C import java.util.Iterator;import java.util.LinkedList; public class Lists{   public static void printSkipper(LinkedList<String> words)   {      Iterator<String> iter = words.iterator();      //code here       System.out.println();   }}
Apart from the main list in the list above, elements with the same value are also linked. According to this; a) Write the Generic Node class. b) Write the method that adds a new element to the end of the list.
Complete the following method that uses a loop to removes all strings with length less than four from its parameter words. import java.util.Iterator;import java.util.LinkedList; public class Lists{   public static void removeShort(LinkedList<String> words)   {      Iterator<String> iter = words.iterator();       /* Your code goes here */    }}
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education