Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help! I need the methods for the single linked list class IN JAVA! Here are the

ID: 3756411 • Letter: H

Question

Help! I need the methods for the single linked list class IN JAVA! Here are the requirements:

THE SKELETON IS PROVIDED BELOW THE PICTURES:

Here is the SKELETON TO ADD TO:

public class SimpleLinkedList {

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

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

//the reference to the first Node in the linked list.
private Node head;

//create an empty linked list
public SimpleLinkedList() {
head = null;
}

//append newItem
public void add(int newItem) {
Node temp = new Node(newItem);
if (head == null) { //empty list
head = temp;
} else { //non-empty list
//locate last node
Node current = head; //start with the first node
while (current.next != null) { //check if current node is not the last node
current = current.next; //move on to the next node on the list
}
current.next = temp; //append the new node immediately following the current node
}
}

//return a string that contains all integers (in the original sequence) in the linked list.
@Override
public String toString() {
String result = ""; //result string

//solution 1:
Node current = head; //start with first Node
while (current != null) { //check if there is still nodes remaining
result += current.data; //add the integer in current Node to the result string
result += "==>";   
current = current.next; //move on to the next Node
}
return result;
}
}

. Add the following methods: o public int getHowMany() o public boolean belongs(int item) o public int get(int index) Find currently how many integers are in the list and return the count. Find out if the specified integer is in the linked list. Returns true if yes, false otherwise 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: [o] (1] [21 [3] Data: 10 20 30 Then add(1, 40) will make the calling list become Position: [o] (1] [21 [3] Data 10 40 20 30 The indices of all existing elements from [index] to [size-1] are increased by1 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

Explanation / Answer

public class SimpleLinkedList {

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

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

//getter method for data
public int getData()
{
return data;

}


}

//the reference to the first Node in the linked list.
private Node head;

//create an empty linked list
public SimpleLinkedList() {
head = null;
}

//method that returns the number of nodes in the list
public int getHowMany()
{
Node temp = head;//assigning to head
int count = 0;
while(temp!=null)//runs upto the end of the array
{
  count++;//and increasing count for each node
  temp=temp.next;
}

//returning count
return count;

}


//method that checks whether the item belongs to linked are not

public boolean belongs(int item)
{
Node temp = head;//assigning to head

while(temp!=null)//runs upto the end of the array
{
  if(temp.getData() == item)//if item found
  return true;//then returning true
  temp=temp.next;
}
//if not found after traversing the entire array
return false;//means not found, so returning false
}

//returns the element at given index
public int get(int index)
{
//first finding how many numbers are in linked list
int l = getHowMany();
if(index<l)//if index is in l then
{
  int i=0;
  Node temp = head;//assigning to head
  while(temp!=null)//runs upto the end of the array
  {
   if(i==index)//if index found then
   return temp.getData();//then returning data
   i++;
   temp=temp.next;
  }
  
  //if not found
  return -1;//means not found
}
else
return -1;
}

//method that adds node at the given index
public boolean add(int index,int item)
{
//first finding how many numbers are in linked list
int l = getHowMany();
if(index<l)//if index is in l then
{
  int i=0;
  if(index==0)//means first node
  {
   Node n =new Node(item);
   
   n.next = head;
   head = n;//updating head
   return true;
  }
  else
  {
  Node temp = head;//assigning to head
  Node prev = head;
  while(temp!=null)//runs upto the end of the array
  {
   if(i==index)//if index found then
   {
    Node n =new Node(item);
    prev.next = n;
    n.next =temp;
    //linked
    return true;//inserted
   }
   i++;
   prev = temp;
   temp=temp.next;
  }
  
  }
}
return false;//means insertion not possible
}

public boolean removeByValue(int item)
{
Node temp = head;//assigning to head
Node prev = head;
while(temp!=null)//runs upto the end of the array
{
  if(temp.getData() == item)//if item found
  {
   prev.next = temp.next;//delinking
   
   return true;//removing successful
   
    
  }
  prev = temp;
  temp=temp.next;
}
//if not found after traversing the entire array
return false;//means not found, so returning false  

}

//append newItem
public void add(int newItem) {
Node temp = new Node(newItem);
if (head == null) { //empty list
head = temp;
} else { //non-empty list
//locate last node
Node current = head; //start with the first node
while (current.next != null) { //check if current node is not the last node
current = current.next; //move on to the next node on the list
}
current.next = temp; //append the new node immediately following the current node
}
}

//return a string that contains all integers (in the original sequence) in the linked list.
@Override
public String toString() {
String result = ""; //result string

//solution 1:
Node current = head; //start with first Node
while (current != null) { //check if there is still nodes remaining
result += current.data; //add the integer in current Node to the result string
result += "==>";  
current = current.next; //move on to the next Node
}
return result;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote