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

CS510: Java Singly Link List I need some help with a single link list implementa

ID: 3602766 • Letter: C

Question

CS510: Java Singly Link List

I need some help with a single link list implementation in Java Programming. The program must implement the following cases:

-Public void Adds(E object)

: adds object to the linked list, appending any other after when adding in main.

-Public void RemoveSpecific(int index)

: removes the first element chosen if it is in the linked list using the corresponding index value. Ex [1, 1, 2,] If user removes index 0, it will remove the first number 1 at position 0.

-Public void RemoveNormal:

   -removes the first element from linked list regardless of position.

-Public boolean Contains(E object)

: returns the index of the first element chosen if it’s in the linked list

-Public int List()

: returns the linked list implemented from beginning to the end in correct order and returns the linked list in an array

-Public int index(E object)

: looks to see if the linked list implemented is empty or not


When running program in main see example below:

Public class SinglyLinkList<E >

{

Public static void main (String[] x)

   {

       Elements<Integer> z = new SinglyLinkList<>();

       z.add(2);

       z.add(7);

z.add(11);

…. //etc

Explanation / Answer

Given below is the completed code for the question. The main shows usage of the class. Please don't forget to rate the answer if it helped. Thank you.

To indent code in eclispe , select code by pressing Ctrl+A and then Ctrl+i


import java.util.Arrays;
public class SinglyLinkList<E> {
class Node
{
E data;
Node next;
Node(E d)
{
data = d;
next = null;
}
Node(E d, Node n)
{
data = d;
next = n;
}
}
private Node head, tail;
private int size;
public void add(E obj)
{
if(head == null)
head = tail = new Node(obj);
else
{
tail.next = new Node(obj, null);
tail = tail.next;
}
size++;
}
public void removeSpecific(int index)
{
if(index < 0 || index >= size)
return ;
Node prev = null, curr = head;
for(int i = 0; i < index; i++)
{
prev = curr;
curr = curr.next;
}
if(curr == head)
{
head = head.next;
if(head == null )
tail = null;
}
else
{
prev.next = curr.next;
if(curr == tail) //remove tail node
tail = prev;
}
size--;
}
//removes 1st element in the list
public void removeNormal()
{
removeSpecific(0);
}
public boolean contains(E obj)
{
Node curr = head;
while(curr != null)
{
if(curr.data.equals(obj))
return true;
curr = curr.next;
}
return false;
}
public int index(E obj)
{
Node curr = head;
int index = 0;
while(curr != null)
{
if(curr.data.equals(obj))
return index;
index++;
}
return -1;
}
public E[] list()
{
E[] list = (E[]) new Object[size];
Node curr = head;
for(int i = 0; i < size; i++)
{
list[i] = curr.data;
curr = curr.next;
}
return list;
}
public int size()
{
return size;
}
public boolean empty()
{
return size == 0;
}
public static void main(String[] args) {
SinglyLinkList<Integer> z = new SinglyLinkList<Integer>();
System.out.println("Adding 2 7 11");
z.add(2);
z.add(7);
z.add(11);
System.out.println(Arrays.toString(z.list()));
System.out.println("size " + z.size());
System.out.println("empty = " + z.empty());
System.out.println("Removing at index 1");
z.removeSpecific(1);
System.out.println(Arrays.toString(z.list()));
System.out.println("Removing normal");
z.removeNormal();
System.out.println(Arrays.toString(z.list()));
System.out.println("contains 2 = " + z.contains(2));
}
}

output

Adding 2 7 11
[2, 7, 11]
size 3
empty = false
Removing at index 1
[2, 11]
Removing normal
[11]
contains 2 = false