Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE THE LINKEDLIST CLASS PROVIDED AND ONLY GIVE ME METHOD NOTHING ELSE PLEASE FAST import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException; /** * Class KWLinkedList implements

USE THE LINKEDLIST CLASS PROVIDED AND ONLY GIVE ME METHOD NOTHING ELSE PLEASE FAST import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException; /** * Class KWLinkedList implements a double linked list and a ListIterator. **/ class KWLinkedList { // Data Fields public Node head; public Node tail; // A reference to the end of the list. public int size; // number of nodes // constructor  KWLinkedList public KWLinkedList() { // initially empty list head = null; tail = null; size = 0; } /** * Insert an object at the beginning of the list. * @param item - the item to be added */ public void addFirst(E item) { Node node = new Node(item); if(head != null) { node.next = head; head.prev = node; head = node; } else { head = node; tail = node; } size++; } // end addFirst /** * Insert an object at the end of the list. * @param item - the item to be added */ public void addLast(E item) { Node node = new Node(item); if(tail != null) { node.prev = tail; tail.next = node; tail = node; } else { head = node; tail = node; } size++; } // end addLast /** * Get the first element of the list. * @return The first element of the list. */ public E getFirst() { return head.data; } /** * Get the last element in the list. * @return The last element in the list. */ public E getLast() { return tail.data; } /** * Return an Iterator to the list * @return an Itertor to the list */ public Iterator iterator() { KWListIter iterator = new KWListIter( ); return iterator; } /** * Return a ListIterator to the list * @return a ListItertor to the list */ public ListIterator listIterator() { KWListIter iterator = new KWListIter( ); return iterator; } /** Return a ListIterator that begins at index * @param index - The position the iteration is to begin * @return a ListIterator that begins at index */ public ListIterator listIterator(int index) { KWListIter iterator = new KWListIter(index); return iterator; } /** * Return a ListIterator that begins at the same * place as an existing ListIterator * @param iter - The other ListIterator * @return a ListIterator that is a copy of iter */ public ListIterator  listIterator(ListIterator  iter) { KWListIter iterator = new KWListIter((KWListIter)iter); return iterator; } /** * Add an item at the specified index. * @param index The index at which the object is to be inserted * @param obj The object to be inserted * @throws IndexOutOfBoundsException if the index is out * of range (i  size()) */ public void add(int index, E obj) { listIterator(index).add(obj); } /** * Get the element at position index. * @param index Position of item to be retrieved * @return The item at index */ public E get(int index) { return listIterator(index).next(); } /** * Return the size of the list * @return the size of the list */ public int size() { return size; } // Inner Classes /** * A Node is the building block for a double-linked list. */ private static class Node { public E data; // The data value public Node next; // The link to the next node public Node prev; // The link to the previous node /** * Construct a node with the given data value. * @param dataItem The data value */ private Node(E dataItem) { data = dataItem; next = null; prev = null; } /** * Returns String equivalent of data * @return String equivalent of data */ public String getData() { if(data == null) return "null"; else return (String)data; } } //end class Node /** Inner class to implement the ListIterator interface. */ private class KWListIter implements ListIterator { private Node nextItem; // A reference to the next item private Node lastItemReturned; //A reference to the last item returned. private int index = 0; // The index of the current item. /** * Construct a KWListIter that will begin just before the first item of the list. * It will reference the first node of the list. */ public KWListIter( ) { lastItemReturned = null; // No item returned yet. index = 0; nextItem = head; // Start at the beginning } /** * Construct a KWListIter that will reference the ith item. * @param i The index of the item to be referenced */ public KWListIter(int i) { // Validate parameter i. if (i  size) throw new IndexOutOfBoundsException("Invalid index " + i); lastItemReturned = null; // No item returned yet. if (i == size) // Special case of last item { index = size; nextItem = null; } else { // Start at the beginning nextItem = head; for (index = 0; index (obj); tail = head; } else if (nextItem == head) // Insert a new node at front of the list. { Node newNode = new Node(obj); // Create a new node. newNode.next = nextItem; // Step 1 - Link it to the nextItem. nextItem.prev = newNode; // Step 2 - Link nextItem to the new node. head = newNode; // Step 3 - The new node is now the head. } else if (nextItem == null) // Insert at tail. { Node newNode = new Node(obj); // Create a new node. tail.next = newNode; // Step 1 - Link the tail to the new node. newNode.prev = tail; // Step 2 - Link the new node to the tail. tail = newNode; // Step 3 - The new node is the new tail. } else // Insert into the middle. { Node newNode = new Node(obj); // Create a new node. newNode.prev = nextItem.prev; // Step 1 - Link it to nextItem.prev. nextItem.prev.next = newNode; // Step 2 newNode.next = nextItem; // Step 3 - Link it to the nextItem. nextItem.prev = newNode; // Step 4 } // Increase size and index and set lastItemReturned. size++; index++; lastItemReturned = null; } // End of method add. /** Remove the last item returned. This can only be * done once per call to next or previous. * @throws IllegalStateException if next or previous * was not called prior to calling this method */ public void remove() { if(lastItemReturned == null) // can not remove throw new IllegalStateException( ); else { if (head == tail) // Remove the only node { head = tail = null; } else if (lastItemReturned == head) // Remove the first node { head = head.next; head.prev = null; } else if (lastItemReturned == tail) // Remove the last node { tail = tail.prev; tail.next = null; } else // Remove some in-between node { lastItemReturned.prev.next = lastItemReturned.next; lastItemReturned.next.prev = lastItemReturned.prev; } if (lastItemReturned != nextItem) { // method next was called before calling remove index--; } else { // method previous was called before calling remove // nextItem is same as lastItemReturned and this node // is going to be deleted nextItem = nextItem.next; } lastItemReturned = null; size--; } // end outer else } // end remove /** Replace the last item returned with a new value * @param item The new value * @throws IllegalStateException if next or previous * was not called prior to calling this method */ public void set(E item) { if(lastItemReturned == null) throw new IllegalStateException( ); else{ lastItemReturned.data = item; lastItemReturned = null; } } } //end class KWListIter /** * Method to find the index of the first occurrence of an item in the list * @param target The item being sought * @return The index of the first occurrence of the target item * or -1 if the item is not found. */ public int indexOf(E target) { int index = 0; Node node = head; while (node != null) { if ( target.equals( node.data) ) return index; else { node = node.next; index++; } } return -1; // target not found } /** * Method to find the index of the last occurrence of an item in the list * @param target The item being sought * @return The index of the last occurrence of the target item * or -1, if the item is not found. */ public int lastIndexOf(E target) { int index = size - 1; Node node = tail; while (node != null) { if ( target.equals( node.data) ) return index; else { node = node.prev; index--; } } return -1; // target not found } /** * Method to return the index of the minimum item in the list * It is assumed that each item in the list implements Comparable * @return Index of the minimum item in the list * or -1 if the list is empty * @throws ClassCastException if the list elements do not implement * Comparable */ public int indexOfMin() { return 0; // method not implemented } } 

image text in transcribed

Question 9 7 Points 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: list (before method call): 7 10 2 3 5 39 list (after method call): 7 10 3 5 3 9 Use the editor to format your

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data In Just 7 Chapters

Authors: Prof Marcus Vinicius Pinto

1st Edition

B09NZ7ZX72, 979-8787954036

More Books

Students also viewed these Databases questions

Question

How to solve maths problems with examples

Answered: 1 week ago

Question

What is the relationship between humans and nature?

Answered: 1 week ago