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;
}
}
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.