-Fill in the reverse method below to return a new DoublyLinkedList consisting of the same elements in reverse order.
-The reverse method must not modify the original DoublyLinkedList.
-The reverse method must run in linear time.
Can someone answer this for me, please? In Java
public class DoublyLinkedList {
int size;
Node firstNode;
Node lastNode;
public DoublyLinkedList() {
size = 0;
firstNode = null;
lastNode = null;
}
// Problem 1 (15 pts)
// Fill in the method below to add a new node at the given index with the given element.
// If index is out of bounds, you must throw an IndexOutOfBoundsException.
// You must appropriately update all next and prev's.
// You must appropriately update the firstNode and lastNode.
// Hint: It's recommended that you use if statements to split into four or five different cases.
public void add(int index, E element) {
if(index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
Node adding = new Node(element, firstNode, lastNode);
if (size == 0) {
this.firstNode = adding;
this.lastNode = adding;
}
else if (index == 0) {
adding.next = firstNode;
firstNode.prev = adding;
firstNode = adding;
}
else if(index == size) {
adding.prev = lastNode;
lastNode.next = adding;
lastNode = adding;
}
else {
Node before = this.getNode(index - 1);
adding.next = before.next;
adding.prev = before;
before.next.prev = adding;
before.next = adding;
}
size++;
}
// Problem 2 (15 pts)
// Fill in the method below to remove a new node at the given index.
// If index is out of bounds, you must throw an IndexOutOfBoundsException.
// You must appropriately update all next and prev's.
// You must appropriately update the firstNode and lastNode.
// Hint: It's recommended that you use if statements to split into four or five different cases.
public E remove(int index) {
E toReturn = null;
if(index < 0 || index >=size) {
throw new IndexOutOfBoundsException();
}
if(size == 1) {
toReturn = firstNode.element;
firstNode = null;
lastNode= null;
} else if(index == 0) {
toReturn = firstNode.element;
firstNode = firstNode.next;
firstNode.prev = null;
} else if(index == size -1) {
toReturn = lastNode.element;
lastNode= lastNode.prev;
lastNode.next = null;
} else {
}
size--;
return toReturn;
}
// Problem 3 (10 pts)
// Fill in the getNode, getNodeFromFront, and getNodeFromBack methods below to return the node at the given index.
// The getNodeFromFront method starts from firstNode going forward.
// The getNodeFromBack method starts from the lastNode going backwards.
// The getNode method calls the getNodeFromFront method if index < size / 2 and it calls the getNodeFromBack method otherwise.
// If index is out of bounds, you must throw an IndexOutOfBoundsException.
public Node getNode(int index){
Node prevNode = null;
Node nextNode = firstNode;
while(index > 0){
prevNode = nextNode;
if(nextNode.next == null) {
throw new IndexOutOfBoundsException("Node index out of bounds");
}
nextNode = nextNode.next;
index--;
}
return nextNode;
}
private Node getNodeFromFront(int index) {
return this.getNode(index);
}
private Node getNodeFromBack(int index) {
return this.getNode(this.size() - index);
}
// Problem 4 (5 pts)
// Fill in the reverse method below to return a new DoublyLinkedList consisting of the same elements in reverse order.
// The reverse method must not modify the original DoublyLinkedList.
// The reverse method must run in linear time.
public DoublyLinkedList reverse(){
DoublyLinkedList newList = new DoublyLinkedList();
return null;
}
*Node Class*
public class Node<E> {
public E element;
public Node<E> prev;
public Node<E> next;
public Node(E element, Node<E> prev, Node<E> next) {
this.element = element;
this.prev = prev;
this.next = next;
}
}
Analyse the below code to reverse a double linked list . It doesn't effect the list and runs in a linear time.
private TwoWayNode next() {
if(head.next != null) return head.next;
else return head.prev;
}
public void reverse()
{
if(head.next == null)
return;
TwoWayNode previous = head;
TwoWayNode current = head.next;
head.next = null;
head.prev = current;
while(current != null)
{
TwoWayNode next = current.next;
current.next = previous;
current.prev = next;
previous = current;
current = next;
}
//Switch head and tail.
}
-Fill in the reverse method below to return a new DoublyLinkedList consisting of the same elements...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For example: If the elements of a list is 1, 2, 3, 4, 5, 6 the reverse string should be 6, 5, 4, 3, 2, 1 implement reverse method you have two steps: 1- you should start traversing from the last element of DoublyLinkedList (the previous of the trailer) 2- you should add the element inside each node to string don't forget the space in...
Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...
Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of a potential element in the doubly linked list and returns the string value of the predecessor node (or an appriopriate message String if there is no such node or predecessor. class DLinkedList { /** The Node class stores a list element and a reference to the next node. */ private class Node { String value; // Value of a list element Node next; //...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...
Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods @Override public int lastIndexOf(Object arg0) { required code } @Override public ListIterator<R> listIterator() { required code } @Override public ListIterator<R> listIterator(int arg0) { required code } PLEASE NOTE: Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should...
Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> { /** * Append an item to the end of the list * * @param item – item to be appended */ public void append(E item); /** * Insert an item at a specified index position * * @param item – item to be...