Question: For Java / Eclipse package linkedlists; import java.util.Iterator; private static class Node { private E element; / / an element stored at this node private

For Java/Eclipse
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 Node(E e, Node n){
element = e;
next = n;
}
public E getElement(){ return element; }
public Node getNext(){ return next; }
private Node tail = null;
private int size =0;
public CircularlyLinkedList_using_Iterable(){}
public int size(){ return size; }
public boolean isEmpty(){ return size ==0; }
public E first(){
if (isEmpty()) return null;
return tail.getNext().getElement();
}
public E last(){
if (isEmpty()) return null;
return tail.getElement();
}
public void rotate(){
if (tail != null)
tail = tail.getNext();
}
public void addFirst(E e){
if (size ==0){
tail = new Node>(e, null);
tail.setNext(tail);
} else {
Node newest = new Node>(e, tail.getNext());
tail.setNext(newest);
}
size++;
}
public void addLast(E e){// adds element e to the end of the list
addFirst(e); // 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.setNext(head.getNext()); // 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();
sb.append(walk.getElement());
if (walk != tail)
sb.append(",");
} while (walk != tail);
sb.append(")");
return sb.toString();
}
public Iterator iterator(){
return new ElementIterator(this);
}
private class ElementIterator implements Iterator {
private CircularlyLinkedList_using_Iterable cLL;
private int cursor;
private Node cursorPointer;
public ElementIterator(CircularlyLinkedList_using_Iterable m){
cLL = m;
cursor =0;
cursorPointer = cLL.tail;
}
// Checks if next element exists
public boolean hasNext(){
return ( cursor cLL.size());
}
public E next(){
cursor = cursor +1;
cursorPointer = cursorPointer.getNext();
return cursorPointer.getElement();
}
}
public static void main(String[] args)
{
//(LAX, MSP, ATL, BOS)
CircularlyLinkedList_using_Iterable circularList = new CircularlyLinkedList_using_Iterable();
circularList.addFirst("LAX");
circularList.addLast("MSP");
circularList.addLast("ATL");
circularList.addLast("BOS");
//
//example loop directly using "hasNext()"
Iterator it = circularList.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println();
//example loop using "special for" (often called "for-each" loop)
for(String s : circularList){
System.out.println(s);
}
}
}Exercise 1
In the existing CircularlyLinkedList class, Write a non-static 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 Lesson3Examples posted in the
eCentennial module "Lesson Examples (from textbook)"
 For Java/Eclipse package linkedlists; import java.util.Iterator; private static class Node {

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!