EBK STARTING OUT W/JAVA:...DATA...
EBK STARTING OUT W/JAVA:...DATA...
4th Edition
ISBN: 9780134757179
Author: GADDIS
Publisher: PEARSON CO
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
LAB: Finding an integer in a list (singly-linked list)   Given main() and an IntNode class, complete the IntList class by writing the append() and search() methods. The search() method should return the IntNode whose data value matches a given key, and null if the key is not found. The search() method should also set the position of each IntNode searched in the IntList, starting with 1. Ex: If the input is: 12 23 59 37 923 2 -1 12 the output is: 12 found in list at position 1. If the input is: 12 23 59 37 923 2 -1 68 the output is: 68 not found in list.   ______________________________________   import java.util.Scanner; public class SearchList {public static void main (String[] args) {Scanner scnr = new Scanner(System.in);IntList intList = new IntList();IntNode curNode, foundNode;int num, searchNum;num = scnr.nextInt();while (num != -1) {// Insert into linked listcurNode = new IntNode(num);intList.append(curNode);num = scnr.nextInt();}searchNum = scnr.nextInt();foundNode =…
Question 2: Linked List Implementation You are going to create and implement a new Linked List class. The Java Class name is "StringLinkedList". The Linked List class only stores 'string' data type. You should not change the name of your Java Class. Your program has to implement the following methods for the StringLinked List Class: 1. push(String e) - adds a string to the beginning of the list (discussed in class) 2. printList() prints the linked list starting from the head (discussed in class) 3. deleteAfter(String e) - deletes the string present after the given string input 'e'. 4. updateToLower() - changes the stored string values in the list to lowercase. 5. concatStr(int p1, int p2) - Retrieves the two strings at given node positions p1 and p2. The retrieved strings are joined as a single string. This new string is then pushed to the list using push() method.
If N represents the number of elements in the collection, then the contains method of the ArrayCollection class is O(1). True or False If N represents the number of elements in the list, then the index-based add method of the ABList class is O(N). True or False
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT