And the java list is. Please send me the full answer pls. Page 1 of 6 Circular L
ID: 3586730 • Letter: A
Question
And the java list is.
Please send me the full answer pls. Page 1 of 6 Circular Linked List Assignment Overview A circular linked list is essentially a singly linked list in which the next pointer of the tail node is set to point to the head node of the linked list rather than set to nul. The first figure below shows a linked list as a singly linked list. The second figure below shows the singly linked list from the first figure as a circular linked list. In this assignment, you will write code for an implementation of a circular linked list 12 Head Tail Singly Linked List 12 Tail Circular Linked List
Explanation / Answer
1.
package list;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
public class CircularLinkedList<AnyType> implements List<AnyType> {
private static class Node<AnyType> {
private AnyType data;
private Node<AnyType> next;
public Node(AnyType d, Node<AnyType> n) {
// TODO Auto-generated constructor stub
this.data = d;
this.next = n;
}
public AnyType getData() {
return data;
}
public void setData(AnyType data) {
this.data = data;
}
public Node<AnyType> getNext() {
return next;
}
}
private int thesize;
private int modCount;
private Node<AnyType> tail;
public int getThesize() {
return thesize;
}
public void setThesize(int thesize) {
this.thesize = thesize;
}
public int getModCount() {
return modCount;
}
public void setModCount(int modCount) {
this.modCount = modCount;
}
public Node<AnyType> getTail() {
return tail;
}
public void setTail(Node<AnyType> tail) {
this.tail = tail;
}
@Override
public void clear() {
// TODO Auto-generated method stub
this.setThesize(0);
this.setTail(null);
this.setModCount(modCount++);
}
@Override
public int size() {
// TODO Auto-generated method stub
return thesize;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
if (size() == 0) {
return true;
}
return false;
}
@Override
public AnyType get(int index) {
if (index > size() || index < 0) {
return null;
}
Node<AnyType> n = tail;
int i = 0; // zero-indexing
while (i++ != index) { // you can increment i at the end too
n = n.getNext();
}
return n.getData();
}
@Override
public AnyType set(int index, AnyType newValue) {
// TODO Auto-generated method stub
if (index > size() || index < 0) {
return null;
}
Node<AnyType> n = tail;
AnyType oldData;
int i = 0; // zero-indexing
while (i++ != index) { // you can increment i at the end too
n = n.getNext();
}
oldData = n.getData();
n.setData(newValue);
return oldData;
}
@Override
public void add(int index, AnyType newValue) {
// TODO Auto-generated method stub
if (index > size() || index < 0) {
return;
}
Node<AnyType> n = tail;
int i = 0; // zero-indexing
index = index - 1; // we need to add element at position index.
while (i++ != index) { // you can increment i at the end too
n = n.getNext();
}
Node<AnyType> newNode = new Node<AnyType>(newValue, n.getNext());
n.next = newNode;
}
@Override
public AnyType remove(int index) {
// TODO Auto-generated method stub
if (index > size() || index < 0) {
return null;
}
Node<AnyType> n = tail;
Node<AnyType> previous = null;
int i = 0; // zero-indexing
while (i++ != index) { // you can increment i at the end too
previous = n;
n = n.getNext();
}
previous.next = n.getNext();
return n.getData();
}
public void rotate() {
Node<AnyType> head = tail.getNext();
AnyType tailData = tail.getData();
tail.setData(head.getData());
head.setData(tailData);// This need to done if we are replacing value.
}
public Node<AnyType> getNode(int index, int lower, int upper) {
if (index > size()) {
return null;
}
Node<AnyType> n = tail;
int i = 0; // zero-indexing
while (i++ != index) { // you can increment i at the end too
n = n.getNext();
}
return n;
}
@Override
public Iterator<AnyType> iterator() {
// TODO Auto-generated method stub
return new LinkedListIterator(tail.getNext());
}
@Override
public boolean add(AnyType newValue) {
// TODO Auto-generated method stub
return false;
}
private class LinkedListIterator implements Iterator<AnyType> {
private Node<AnyType> previous;
private Node<AnyType> current;
private int expectedModCount;
private boolean okToRemove;
public LinkedListIterator(Node<AnyType> head) {
// TODO Auto-generated constructor stub
current = head;
okToRemove = true;
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if (current != null && current.getNext() != null) {
return true;
}
return false;
}
@Override
public AnyType next() {
// TODO Auto-generated method stub
if (current != null && current.getNext() != null) {
previous = current;
current = current.getNext();
return previous.getData();
}
return null;
}
public void remove() {
if (this.previous == null) {
throw new IllegalStateException();
} else {
if (isOkToRemove()) {
try {
previous.next = current.getNext();
current = previous.getNext(); // cursor moved to next
// element.
this.expectedModCount++;
CircularLinkedList.this.modCount = this.expectedModCount;
} catch (IndexOutOfBoundsException arg1) {
throw new ConcurrentModificationException();
}
}
}
}
public boolean isOkToRemove() {
if (CircularLinkedList.this.modCount != this.expectedModCount) {
okToRemove = false;
} else {
okToRemove = true;
}
return okToRemove;
}
}
}