Answered step by step
Verified Expert Solution
Link Copied!

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 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 :
package linkedlists;
import java.util.Iterator;
public class CircularlyLinkedList_using_Iterable 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 Node(E e, Node n){
element = e;
next = n;
public E getElement(){ return element; }
public Node getNext(){ return next; }
public void setNext(Node n){ next = n; }
}
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(){// returns (but does not remove) the first element
if (isEmpty()) return null;
return tail.getNext().getElement(); // 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 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){
addFirst(e);
tail = tail.getNext();
}
public E removeFirst(){
if (isEmpty()) return null;
Node head = tail.getNext();
if (head == tail) tail = null;
else tail.setNext(head.getNext());
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();
}
//Change to the class begins
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();
}
}
//Change to the class ends
//main method
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);
}
}
}

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

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

1. Clarify your objectives.

Answered: 1 week ago