Question: Hello I have JAVA DATA STRUCTURE lab assignment could you help me and add each clone methods steps Part 1. You will first add a
Hello I have JAVA DATA STRUCTURE lab assignment could you help me and add each clone methods steps Part 1. You will first add a new method to DoublyLinkedList.java from Lab1 (from LinkedLists Add the clone() method to the DoublyLinkedList class and test it by adding appropriate statements into Main class. 1. clone() : write a clone method , you will make a copy of the linked list, the copy will have its own independent nodes, but the element references that are stored in the copy nodes will be the same references as in the original list. See the textbook for this kind of clone(). This is NOT a deep copy. 2. Now, you will rewrite the clone method such that it makes a deep copy of the linked list (both nodes and the elements of the copy will be independent from the original lists.) First try the following two steps: (i) use the following code when making a new Node that is a copy of the node referenced by variable walk : Node
public class DoublyLinkedList
//---------------- nested Node class ----------------
/**
* Node of a doubly linked list, which stores a reference to its
* element and to both the previous and next node in the list.
*/
private static class Node
private E element; // reference to the element stored at this node
private Node
private Node
public Node(E e, Node
element = e;
prev = p;
next = n;
}
public E getElement() {
return element;
}
/**
* Returns the node that precedes this one (or null if no such node).
* @return the preceding node
*/
public Node
public Node
public void setPrev(Node
public void setNext(Node
}
private Node
/** Sentinel node at the end of the list */
private Node
/** Number of elements in the list (not including sentinels) */
private int size = 0; // number of elements in the list
/** Constructs a new empty list. */
public DoublyLinkedList() {
header = new Node<>(null, null, null); // create header
trailer = new Node<>(null, header, null); // trailer is preceded by header
header.setNext(trailer); // header is followed by trailer
}
int size() { return size; }
public boolean isEmpty() { return size == 0; }
public E first() {
if (isEmpty()) return null;
return header.getNext().getElement(); // first element is beyond header
}
public E last() {
if (isEmpty()) return null;
return trailer.getPrev().getElement(); // last element is before trailer
}
public void addFirst(E e) {
addBetween(e, header, header.getNext()); // place just after the header
}
public void addLast(E e) {
addBetween(e, trailer.getPrev(), trailer); // place just before the trailer
}
public E removeFirst() {
if (isEmpty()) return null; // nothing to remove
return remove(header.getNext()); // first element is beyond header
}
/**
* Removes and returns the last element of the list.
* @return the removed element (or null if empty)
*/
public E removeLast() {
if (isEmpty()) return null; // nothing to remove
return remove(trailer.getPrev()); // last element is before trailer
}
// private update methods
private void addBetween(E e, Node
// create and link a new node
Node
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
/**
* Removes the given node from the list and returns its element.
* @param node the node to be removed (must not be a sentinel)
*/
public E remove(Node
Node
Node
if(node==header){
header=predecessor;
}else if(node==trailer){
trailer=successor;
}else{
predecessor.setNext(successor);
successor.setPrev(predecessor);}
size--;
return node.getElement();
}
/**
* Produces a string representation of the contents of the list.
* This exists for debugging purposes only.
*/
public String toString() {
StringBuilder sb = new StringBuilder("(");
Node
while (walk != trailer) {
sb.append(walk.getElement());
walk = walk.getNext();
if (walk != trailer)
sb.append(", ");
}
sb.append(")");
return sb.toString();
}
public void addBefore(Node
if(node==header){
Node
node.setPrev(newest);
newest.setNext(node);
header=newest;
size++;
}
else if(node.getPrev()==header){
Node
node.setPrev(newest);
header.setNext(newest);
size++;
}else{
Node
Node
node.setPrev(newest);
onceki.setNext(newest);
size++;
}
}
public void removeBefore(Node
if(node.getPrev()==header){
System.out.println("NODE IS THE FIRST NODE");
}else{
Node
Node
node.setPrev(onceki);
onceki.setNext(node);
sil.setPrev(null);
sil.setNext(null);
size--;
}
}
public Node
if(i>=size())
return null;
Node
for(int j = 0;j<=i;j++)
newest = newest.getNext();
return newest;
}
public E get(int i){
Node
for(int a =0;a<=i;a++)
temp = temp.getNext();
return temp.getElement();
}
public void add(int i, E newData){
if(size==0){
Node
header.setNext(newest);
newest.setNext(trailer);
trailer.setPrev(newest);
newest.setPrev(header);
size++;
}else if(i==size){
Node
Node
last.setNext(newest);
newest.setNext(trailer);
trailer.setPrev(newest);
newest.setPrev(last);
size++;
}else if(i>=0 && i Node Node for(int a = 0;a<=i;a++) temp=temp.getNext(); addBefore(temp,newData); } } public E remove(int i){ if(i Node Node removeBefore(newest); return deger.getElement(); }else if(i== size-1){ Node Node deger.setNext(trailer); trailer.setPrev(newest); size--; return newest.getElement(); }else return null; } public int indexOf(E target){ Node for(int i = 0;i if(temp.getElement().equals(target)) return i; else temp = temp.getNext(); } return -1; } public void removeEveryOther(){ if(size != 0){ for(int i = 0;i removeBefore(getNode(i+1)); } }//----------- end of DoublyLinkedList class ---------- Main class: public class Main{ public static void main(String[] args) { DoublyLinkedList dll.addFirst("A"); dll.addLast("B"); dll.addLast("C"); dll.addLast("D"); dll.addLast("E"); dll.addLast("F"); dll.addLast("G"); dll.addLast("H"); System.out.println(dll); System.out.println("is index > size return (null) ="+dll.getNode(10));//null DoublyLinkedList System.out.println("if dll2 is empty return (null) ="+dll2.getNode(0));// if dll2 is empty return null dll.addBefore(dll.getNode(2), "Z"); System.out.println("After addBefore:(A B Z C D E F G H) "+dll);//A B Z C D E F G H dll.removeBefore(dll.getNode(2));//remove B System.out.println("After removeBefore:(A Z C D E F G H) "+dll);//A Z C D E F G H dll.removeBefore(dll.getNode(1));//remove A System.out.println("After removeBefore:(Z C D E F G H) "+dll);//Z C D E F G H dll.removeBefore(dll.getNode(dll.size()-1));//remove G System.out.println("After removeBefore:(Z C D E F H) "+dll);//Z C D E F H System.out.println("index (0) ="+dll.indexOf("Z"));//0 System.out.println("index (2) ="+dll.indexOf("D"));//2 System.out.println("index (5) ="+dll.indexOf("H"));//5 System.out.println("index (-1) ="+dll.indexOf("G"));//-1 System.out.println("Remove Z ="+dll.remove(0));//Z ; System.out.println("Remove H ="+dll.remove(dll.size()-1));//H dll.add(0,"U"); System.out.println("After add U 0.th index:(U C D E F) "+dll); dll.add(dll.size()-1,"T"); System.out.println("After add T last index: (U C D E T F) "+dll); System.out.println("First element (U)= "+dll.get(0));//U System.out.println("Last element (F)= "+dll.get(dll.size()-1));//F dll.removeEveryOther(); System.out.println("After removeEveryOther: "+dll); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
