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
private int size;
/** private inner class */
private static class Node
{
private E data;
private Node
/** 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
{
data = dataItem;
next = nodeRef;
}
} //end class Node
// constructor SingleLinkedList
public SingleLinkedList( )
{
head = null;
size = 0;
}
private void addFirst (E item)
{
Node
// create a new node
head = temp; // link to the first node
size++;
}
private void addAfter (Node
{
Node
node.next = temp;
size++;
}
private E removeAfter (Node
{
Node
if (temp != null) {
node.next = temp.next;
size--;
return temp.data;
}
else
return null;
}
// Method to delete the first node
private E removeFirst ()
{
Node
if (head != null) {
head = head.next;
size--;
return temp.data;
}
else
return null;
}
private Node
{
Node
for (int i = 0; i < index && node != null; i++) {
node = node.next;
}
return node;
}
public String toString( )
{
String str = "";
Node
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
return node.data;
}
public E set (int index, E anEntry)
{
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
Node
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
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
item = removeAfter(node);
}
return item; // return the deleted value
}
public int indexOf(E item)
{
int index = 0;
Node
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
Get step-by-step solutions from verified subject matter experts
