Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In the existing CircularlyLinkedList class, Write a non - static method named getMax for finding the maximum element in a circularly linked list. Write the
In the existing CircularlyLinkedList class, Write a nonstatic method named getMax for finding the maximum element in a circularly linked list. Write the testing code in the main method of the class CircularlyLinkedList. For this purpose, you must only use and update the CircularlyLinkedList.java file provided :
package linkedlists;
import java.util.Iterator;
public class CircularlyLinkedListusingIterable implements Iterable This line is changed from original
nested Node class
Singly linked node, which stores a reference to its element and
to the subsequent node in the list.
private static class Node
The element stored at this node
private E element; an element stored at this node
A reference to the subsequent node in the list
private Node next; a reference to the subsequent node in the list
Creates a node with the given element and next node.
@param e the element to be stored
@param n reference to a node that should follow the new node
public NodeE e Node n
element e;
next n;
public E getElement return element;
public Node getNext return next;
public void setNextNode n next n;
private Node tail null;
private int size ;
public CircularlyLinkedListusingIterable
public int size return size;
public boolean isEmpty return size ;
public E first returns but does not remove the first element
if isEmpty return null;
return tail.getNextgetElement; the head is after the tail
public E last returns but does not remove the last element
if isEmpty return null;
return tail.getElement;
public void rotate rotate the first element to the back of the list
if tail null if empty, do nothing
tail tail.getNext; the old head becomes the new tail
public void addFirstE e
if size
tail new Nodee null;
tail.setNexttail;
else
Node newest new Nodee tail.getNext;
tail.setNextnewest;
size;
public void addLastE e
addFirste;
tail tail.getNext;
public E removeFirst
if isEmpty return null;
Node head tail.getNext;
if head tail tail null;
else tail.setNextheadgetNext;
size;
return head.getElement;
public String toString
if tail null return ;
StringBuilder sb new StringBuilder;
Node walk tail;
do
walk walk.getNext;
sbappendwalkgetElement;
if walk tail
sbappend;
while walk tail;
sbappend;
return sbtoString;
Change to the class begins
public Iterator iterator
return new ElementIteratorthis;
private class ElementIterator implements Iterator
private CircularlyLinkedListusingIterable cLL;
private int cursor;
private Node cursorPointer;
public ElementIteratorCircularlyLinkedListusingIterable m
cLL m;
cursor ;
cursorPointer cLLtail;
Checks if next element exists
public boolean hasNext
return cursor cLLsize;
public E next
cursor cursor ;
cursorPointer cursorPointer.getNext;
return cursorPointer.getElement;
Change to the class ends
main method
public static void mainString args
LAX MSP ATL, BOS
CircularlyLinkedListusingIterable circularList new CircularlyLinkedListusingIterable;
circularList.addFirstLAX;
circularList.addLastMSP;
circularList.addLastATL;
circularList.addLastBOS;
example loop directly using "hasNext
Iterator it circularList.iterator;
whileithasNext
System.out.printlnitnext;
System.out.println;
example loop using "special for" often called "foreach" loop
forString s : circularList
System.out.printlns;
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