Answered step by step
Verified Expert Solution
Link Copied!

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 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)"
image text in transcribed

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

More Books

Students also viewed these Databases questions

Question

Enhance your listening skills.

Answered: 1 week ago

Question

List the components of the strategic management process. page 72

Answered: 1 week ago