Question: Modify the class SimpleLinkedList given in class (containing the private Node class) in the following ways. Add the following methods: o public int getHowMany()- Find

Modify the class SimpleLinkedList given in class (containing the private Node class) in the following ways.

Add the following methods:

o public int getHowMany()- Find currently how many integers are in the list and return the count.

o public boolean belongs(int item)- Find out if the specified integer is in the linked list. Returns true if yes, false otherwise.

o public int get(int index)-Get the integer item at the specified position and return the integer value. If the index is not valid, print an error message.

public boolean add(int index, int item) -Try to insert the specified integer: item at the specified position: index in the current linked list. If there is an integer, say intA, currently at that position, do not replace it. Instead insert the new item before intA, which effectively increases the indices of intA and subsequent integers by 1. If the specified position is greater than the current size of the linked list, display an error message and do not insert the item. Returns true if the insertion is successful, false otherwise. It adds the integer: item into the calling linked list so that the item is at location [index].

So if calling list is:

Position: [0] [1] [2] [3]

Data: 10 20 30

Then add(1, 40) will make the calling list become:

Position: [0] [1] [2] [3]

Data: 10 40 20 30

The indices of all existing elements from [index] to [size-1] are increased by 1.

When the given position: index is equal to the linked list size, insert becomes append.

So add(4, 50) will make the calling list become:

Position: [0] [1] [2] [3][4]

Data: 10 40 20 30 50

o public boolean equals(Object list) - Check if this linked list and the specified list: list has the same integer sequence. In another word, check if the list of integers stored in this linked list equals to the list of integers stored in the specified list: list. Returns true if yes, false otherwise.

o public boolean removeByValue(int Item)- Remove the specified integer: item from this linked list. If success, return true. Otherwise, return false. Assume that there are no duplicate values in this linked list.

SimpleLinkedList code:

package chp2example;

public class SimpleLinkedList {

private static class Node { private int data; private Node next;

private Node(int data) { this.data = data; next = null; } }

private Node head;

public SimpleLinkedList() { head = null; }

public void add(int newItem) { Node temp = new Node(newItem); if (head == null) { head = temp; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = temp; } }

@Override public String toString() { String result = "";

Node current = head;

while (current != null) { result += current.data; result += "==>"; current = current.next; } return result; } }

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!