Question: JAVA 3 Write a method compareHalfs to be considered within the SingleLinkedList class and has no parameters. The method returns true if the first half

JAVA 3 Write a method compareHalfs to be considered within the SingleLinkedList class and has no parameters. The method returns true if the first half of the list contains the same elements of the second half in the same order. If the list does not have even number of element, so halfs are not equal, the method returns false, otherwise, it returns true. The method head is

public boolean compareHalfs()

head

3 5 6 3 5 6

public boolean compareHalfs()

{ ONLY DO METHOD IMPLEMENTATION OF SLL WILL BE GIVEN

class SingleLinkedList

{

private Node head;

private int size;

/** private inner class */

private static class Node

{

private E data;

private Node next;

/** Creates a new node with a null next field

@param dataItem The data stored

*/

private Node(E dataItem)

{

data = dataItem;

next = null;

}

private Node(E dataItem, Node nodeRef)

{

data = dataItem;

next = nodeRef;

}

} //end class Node

// constructor SingleLinkedList

public SingleLinkedList( )

{

head = null;

size = 0;

}

private void addFirst (E item)

{

Node temp = new Node(item, head);

// create a new node

head = temp; // link to the first node

size++;

}

private void addAfter (Node node, E item)

{

Node temp = new Node(item, node.next);

node.next = temp;

size++;

}

private E removeAfter (Node node)

{

Node temp = node.next;

if (temp != null) {

node.next = temp.next;

size--;

return temp.data;

}

else

return null;

}

// Method to delete the first node

private E removeFirst ()

{

Node temp = head;

if (head != null) {

head = head.next;

size--;

return temp.data;

}

else

return null;

}

private Node getNode(int index)

{

Node node = head;

for (int i = 0; i < index && node != null; i++) {

node = node.next;

}

return node;

}

public String toString( )

{

String str = "";

Node nodeRef = head;

for(int i = 0; i < size; i++) {

str = str + nodeRef.data + " ";

nodeRef = nodeRef.next;

}

return str;

}

public boolean isEmpty( )

{

return head == null;

}

public int size( )

{

return size;

}

public E get(int index)

{

if (index < 0 || index >= size) {

throw new

IndexOutOfBoundsException(Integer.toString(index));

}

Node node = getNode(index);

return node.data;

}

public E set (int index, E anEntry)

{

if (index < 0 || index >= size) {

throw new IndexOutOfBoundsException(Integer.toString(index));

}

Node node = getNode(index); // calling private method

E result = node.data;

node.data = anEntry;

return result;

}

// Method to insert a new item at the specified index in

// the list

public void add (int index, E item)

{

if (index < 0 || index > size) {

throw new

IndexOutOfBoundsException(Integer.toString(index));

}

if (index == 0)

addFirst(item);

else {

Node node = getNode(index - 1);

addAfter(node, item);

}

}

// Method to insert a new item (node) at the end of the list

public boolean add (E item)

{

add(size, item); // calling public method add

return true;

}

// Remove the node at the given index and return its data

public E remove (int index)

{

if (index < 0 || index >= size) {

throw new

IndexOutOfBoundsException(Integer.toString(index));

}

E item;

if (index == 0)

item = removeFirst( );

else {

Node node = getNode(index - 1);

item = removeAfter(node);

}

return item; // return the deleted value

}

public int indexOf(E item)

{

int index = 0;

Node node = head;

while (node != null)

{

if ( item.equals( node.data) )

return index;

else {

node = node.next;

index++;

}

}

return -1; // item not found

}

public boolean remove (E item)

{

int index = indexOf(item);

if (index != -1) {

remove(index); // remove node at the index

return true; // item found

}

else

return false; // item not found

}

// Method to check whether an object is in the list. If found,

// return true, else return false.

public boolean contains(E item)

{

int index = indexOf(item);

if (index != -1)

return true; // item found

else

return false; // item not found

}

} // end SingleListList class

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!