I\'m sorry that the picture is blurred, but can someone give me an idea of this
ID: 3807598 • Letter: I
Question
I'm sorry that the picture is blurred, but can someone give me an idea of this code?
This method has cods chocking that obj is an instance of, and then either returns false or performs the typecast. You will now need to perform a list traversal and check that the element in each Entry is equal to the element at the identical index in otherList. The algorithm you must implement (in the area labelled//ADD CODE HERE) is: Set tray to the first Entry in the linked list for i = 0 to size Set elem equal to the element at index 1 in otherList If (trav's element and clan are equal elements) return false End if Set trav equal to the next Entry in the list End For return trueExplanation / Answer
Please find the required program and output below: Please find the comments against each line for the description:
class DoublyLinkedList {
Node head; //head of the list
int size; //size of the list
public boolean equals(Object obj) {
if(obj instanceof DoublyLinkedList){ //checking whether obj is instance of DoublyLinkedList
DoublyLinkedList otherList = (DoublyLinkedList)obj; //typecast the obj to DoublyLinkedList
Node trav = this.head; //set trav to the first node in this list, ie, head
for(int i=0; i<size; i++){ //iterate through all the elements in this list
int j=0,elem = 0;
Node temp = otherList.head;
while(j<=i){ //iterate through the otherList to get the element data at position at i
elem = temp.getData();
temp = temp.getNext();;
}
if(!(trav.getData() == elem)) //if both are not equal then return false
return false;
trav = trav.getNext(); //point to the next node
}
return true;
}else{
return false;
}
}
}
class Node {
int data;
Node next;
Node previous;
public Node(int data) {
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPrevious() {
return previous;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
}