Question: Im having trouble implenting these methods , any help would be appreciated. Thank you in advance DoublyLinkedList clone() This method returns a shallow copy of

Im having trouble implenting these methods , any help would be appreciated. Thank you in advance

DoublyLinkedList clone()

This method returns a shallow copy of this DoublyLinkedList. The nodes of this DoublyLinkedList are cloned but the values themselves are not cloned.

boolean contains(AnyType value)

This method returns true if this list contains the specified value, otherwise it returns false.

int indexOf(AnyType value)

This method returns the index of the first occurrence of the specified value in this list, or -1 if this list does not contain the value.

int lastIndexOf(AnyType value)

This method returns the index of the last occurrence of the specified value in this list, or -1 if this list does not contain the value

boolean removeFirstOccurrence(AnyType value)

This method removes the first occurrence of the specified value in this list (when traversing the list from head to tail). If the list does not contain the value, it is unchanged. The method returns true if the list contained the specified value, otherwise it returns false.

boolean removeLastOccurence(AnyType value)

This method removes the last occurrence of the specified value in this list (when traversing the list from head to tail). If the list does not contain the value, it is unchanged. The method returns true if the list contained the specified value, otherwise it returns false.

Object[] toArray()

This method returns an array containing all of the values in this list in proper sequence (from first to last value)

import java.util.Iterator; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; public class DoublyLinkedList implements List { private static class Node { private AnyType data; private Node prev; private Node next; public Node(AnyType d, Node p, Node n) { setData(d); setPrev(p); setNext(n); } public AnyType getData() { return data; } public void setData(AnyType d) { data = d; } public Node getPrev() { return prev; } public void setPrev(Node p) { prev = p; } public Node getNext() { return next; } public void setNext(Node n) { next = n; } } private int theSize; private int modCount; private Node header; private Node trailer; public DoublyLinkedList() { header = new Node(null, null, null); trailer = new Node(null, null, null); modCount = 0; clear(); } public void clear() { header.setNext(trailer); trailer.setPrev(header); theSize = 0; } public int size() { return theSize; } public boolean isEmpty() { return (size() == 0); } private Node getNode(int index) { return (getNode(index, 0, size()-1)); } private Node getNode(int index, int lower, int upper) { Node currNode; if (index < lower || index > upper) throw new IndexOutOfBoundsException(); int n = size(); if (index < n/2) { currNode = header.getNext(); for (int i = 0; i < index; i++) currNode = currNode.getNext(); } else { currNode = trailer; for (int i = n; i > index; i--) currNode = currNode.getPrev(); } return currNode; } public AnyType get(int index) { Node indexNode = getNode(index); return indexNode.getData(); } public AnyType set(int index, AnyType newValue) { Node indexNode = getNode(index); AnyType oldValue = indexNode.getData(); indexNode.setData(newValue); return oldValue; } public boolean add(AnyType newValue) { add(size(), newValue); return true; } public void add(int index, AnyType newValue) { addBefore(getNode(index, 0, size()), newValue); } private void addBefore(Node nextNode, AnyType newValue) { Node prevNode = nextNode.getPrev(); Node newNode = new Node<>(newValue, prevNode, nextNode); prevNode.setNext(newNode); nextNode.setPrev(newNode); theSize++; modCount++; } public AnyType remove(int index) { return remove(getNode(index)); } private AnyType remove(Node currNode) { Node prevNode = currNode.getPrev(); Node nextNode = currNode.getNext(); prevNode.setNext(nextNode); nextNode.setPrev(prevNode); theSize--; modCount++; return currNode.getData(); } public DoublyLinkedList clone() { } public boolean contains(AnyType value) { } public int indexOf(AnyType value) { } public int lastIndexOf(AnyType value) { } public boolean removeFirstOccurrence(AnyType value) { } public boolean removeLastOccurence(AnyType value) { } public Object[] toArray() { } public Iterator iterator() { return new LinkedListIterator(); } private class LinkedListIterator implements Iterator { private Node cursor; private int expectedModCount; private boolean okToRemove; LinkedListIterator() { cursor = header.getNext(); expectedModCount = modCount; okToRemove = false; } public boolean hasNext() { return (cursor != trailer); } public AnyType next() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (!hasNext()) throw new NoSuchElementException(); AnyType nextValue = cursor.getData(); cursor = cursor.getNext(); okToRemove = true; return nextValue; } public void remove() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (!okToRemove) throw new IllegalStateException(); DoublyLinkedList.this.remove(cursor.getPrev()); expectedModCount++; okToRemove = false; } } }

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!