Question
USE JAVA SLL CLASS WILL BE PROVIDED I NEED METHOD ONLY FAST class SingleLinkedList { private Node head; private int size; /** private inner class
USE JAVA SLL CLASS WILL BE PROVIDED
I NEED METHOD ONLY FAST
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;
}
/** Creates a new node that references another node
@param dataItem The data stored
@param nodeRef The node referenced by new node
*/
private Node(E dataItem, Node
{
data = dataItem;
next = nodeRef;
}
} //end class Node
// constructor SingleLinkedList
public SingleLinkedList( )
{
// initially empty list
head = null;
size = 0;
}
/** Private methods */
// Method to insert a new node at front of the list
private void addFirst (E item)
{
Node
// create a new node
head = temp; // link to the first node
size++;
}
// Method to insert a new node after the node referenced
// by node
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;
}
// Return reference to the node with the specified index
private Node
{
Node
for (int i = 0; i
node = node.next;
}
return node;
}
/** Public methods */
// Return String representation of the list
public String toString( )
{
String str = "";
Node
for(int i = 0; i
str = str + nodeRef.data + " ";
nodeRef = nodeRef.next;
}
return str;
}
// Method to check whether the list is empty
public boolean isEmpty( )
{
return head == null;
}
// Return actual number of elements (nodes) in the list
public int size( )
{
return size;
}
// Return the object at the specified index
public E get(int index)
{
if (index = size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
Node
return node.data;
}
// Replace the element at the specified index by the
// new value anEntry and return the old value
public E set (int index, E anEntry)
{
if (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 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 = 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
}
} // end SingleListList class
Question 12 7 Points Write a method called CompareAndChangelist to be included in an application class called ListApplication that accepts two parameters list1 and list2 of type SingleLinkedList of integer values, and a third parameter Item of type int. If list1 and list2 are of different sizes, the method returns false, otherwise, the method replaces any element in list2 with a value equal to the last element in list1, if the summation of the element in list 1 and the corresponding element in list2 is equal to item. Assume list1 and list2 are not empty. If at least one replacement happened, the method returns true, otherwise, it returns false. Note: Write this method by calling methods of the class SingleLinkedList. Method head: public static boolean CompareAndChangeList (SingleLinkedList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started