Question
Solve In Java Write the method only, I will give you the implementation of KWLinkedList Write a method called removeLeastValue in the class KWLinkedList (a
Solve In Java
Write the method only, I will give you the implementation of KWLinkedList
Write a method called removeLeastValue in the class KWLinkedList (a class for doubly linked list as discussed in the lectures). The method will search for the smallest integer in the double linked list and removes the node that contains that value. Assume that the list must contain at least two nodes. Assume further that the smallest value never be in the first node. Do not use Iterators and you are not allowed to call any of the KWLinkedList class methods.
Method heading:
public void removeLeastValue()
Example:
Iist (before method call): 7 10 2 3 5 3 9
Iist (after method call): 7 10 3 5 3 9
implementation of LinkedList
import java.util.NoSuchElementException; /** Implementation of the interface StackInt using a linked list. The * top element of the stack is in the first (front) node of the linked list. */ public class LinkedStack
/** Node: inner class to create nodes for linked list based stack. */ private static class Node
// Constructors /** * Creates a new node with a null next field. * @param dataItem The data to be stored in the node */ private Node(E dataItem) { data = dataItem; next = null; }
/** * Creates a new node that references another node. * @param dataItem The data to be stored in the node * @param nodeRef The node referenced by new node */ private Node(E dataItem, Node
// Constructor public LinkedStack() { topOfStack = null; // Initially the stack is empty } // copy constructor public LinkedStack(LinkedStack
/** * Insert a new item on top of the stack. * @post The new item is the top item on the stack. * @param obj The item to be inserted * @return The item that was inserted */ @Override public E push(E obj) { topOfStack = new Node
/** * Remove and return the top item on the stack. * @pre The stack is not empty. * @post The top item on the stack has been removed and * the stack is one item smaller. * @return The top item on the stack * @throws NoSuchElementException, if the stack is empty */ @Override public E pop() { if (isEmpty()) { throw new NoSuchElementException(); } else { E result = topOfStack.data; topOfStack = topOfStack.next; return result; } }
/** * Return the top item on the stack. * @pre The stack is not empty. * @post The stack remains unchanged. * @return The top item on the stack * @throws NoSuchElementException if the stack is empty */ @Override public E peek() { if (isEmpty()) { throw new NoSuchElementException(); } else { return topOfStack.data; } }
/** * See whether the stack is empty. * @return true if the stack is empty */ @Override public boolean isEmpty() { return (topOfStack == null); }
} // End of class LinkedStack
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started