Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For Java / Eclipse package linkedlists; import java.util.Iterator; private static class Node { private E element; / / an element stored at this node private
For JavaEclipse
package linkedlists;
import java.util.Iterator;
private static class Node
private E element; an element stored at this node
private Node next; a reference to the subsequent node in the list
public NodeE e Node n
element e;
next n;
public E getElement return element;
public Node getNext return next;
private Node tail null;
private int size ;
public CircularlyLinkedListusingIterable
public int size return size;
public boolean isEmpty return size ;
public E first
if isEmpty return null;
return tail.getNextgetElement;
public E last
if isEmpty return null;
return tail.getElement;
public void rotate
if tail null
tail tail.getNext;
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 adds element e to the end of the list
addFirste; insert new element at front of list
tail tail.getNext; now new element becomes the tail
public E removeFirst removes and returns the first element
if isEmpty return null; nothing to remove
Node head tail.getNext;
if head tail tail null; must be the only node left
else tail.setNextheadgetNext; removes "head" from the list
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;
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;
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;
Exercise
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 in LessonExamples posted in the
eCentennial module "Lesson Examples from textbook
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