Question: please solve quickly, the deadline is soon. //************************** SLL.java ********************************* // a generic singly linked list class public class SLL { private class SLLNode {
please solve quickly, the deadline is soon.
//************************** SLL.java *********************************
// a generic singly linked list class
public class SLL
private class SLLNode
private T info;
private SLLNode
public SLLNode() {
this(null, null);
}
public SLLNode(T el) {
this(el, null);
}
public SLLNode(T el, SLLNode
info = el;
next = ptr;
}
}
protected SLLNode
public SLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void addToHead(T el) {
head = new SLLNode
if (tail == null)
tail = head;
}
public void addToTail(T el) {
if (!isEmpty()) {
tail.next = new SLLNode
tail = tail.next;
} else
head = tail = new SLLNode
}
public T deleteFromHead() { // delete the head and return its info;
if (isEmpty())
return null;
T el = head.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else
head = head.next;
return el;
}
public T deleteFromTail() { // delete the tail and return its info;
if (isEmpty())
return null;
T el = tail.info;
if (head == tail) // if only one node in the list;
head = tail = null;
else { // if more than one node in the list,
SLLNode
for (tmp = head; tmp.next != tail; tmp = tmp.next)
;
tail = tmp; // the predecessor of tail becomes tail;
tail.next = null;
}
return el;
}
public void delete(T el) { // delete the node with an element el;
if (!isEmpty())
if (head == tail && el.equals(head.info)) // if only one
head = tail = null; // node on the list;
else if (el.equals(head.info)) // if more than one node on the list;
head = head.next; // and el is in the head node;
else { // if more than one node in the list
SLLNode
for (pred = head, tmp = head.next; tmp != null
&& !tmp.info.equals(el); pred = pred.next, tmp = tmp.next)
;
if (tmp != null) { // if el was found;
pred.next = tmp.next;
if (tmp == tail) // if el is in the last node;
tail = pred;
}
}
}
@Override
public String toString() {
if (head == null)
return "[ ]";
String str = "[ ";
SLLNode
while (tmp != null) {
str += tmp.info + " ";
tmp = tmp.next;
}
return str + "]";
}
public boolean contains(T el) {
if (head == null)
return false;
SLLNode
while (tmp != null) {
if (tmp.info.equals(el))
return true;
tmp = tmp.next;
}
return false;
}
public int size() {
if (head == null)
return 0;
int count = 0;
SLLNode
while (p != null) {
count++;
p = p.next;
}
return count;
}
// Please write the methods of Task02 here:
// below are the methods that are used to impelement in the main class
// inserts the newElem before the index
public void insertBefore(int index, T newElem) throws Exception {
// throws exception if the list is empty
if (isEmpty()) {
throw new Exception("list is empty");
}
// if the index is valid then inserts
if (index
if (index == 0) {
SLLNode
head = newnode;
} else {
SLLNode
SLLNode
for (int i = 0; i
temp = temp.next;
}
newnode.next = temp.next;
temp.next = newnode;
}
}
// throws exception for invalid index
else {
throw new Exception("index is not valid");
}
}
// deletes the index from the list
public T delete(int index) throws Exception {
// throws exception if the list is empty
if (isEmpty()) {
throw new Exception("list is empty");
}
// if index is valid then removes
if (index
if (index == 0) {
return deleteFromHead();
} else if (index == size() - 1) {
return deleteFromTail();
}
SLLNode
for (int i = 0; i
temp = temp.next;
}
T el = temp.next.info;
temp.next = temp.next.next;
return el;
}
// invalid index
throw new Exception("index is not valid");
}
// inserts the e1 after second occurrence of the e2
public void insertAfterSecondOccurrence(T e1, T e2) throws Exception {
// list is empty
if (isEmpty()) {
throw new Exception("list is empty");
}
int count = 0, pos = -1;
SLLNode
for (int i = 0; i
if (temp.info == e2) {
count++;
}
if (count == 2) {
pos = i;
break;
}
temp = temp.next;
}
if (pos != -1) {
if (pos + 1 == size()) {
addToTail(e1);
} else {
insertBefore(pos + 1, e1);
}
}
// invalid case
if (pos == -1) {
throw new Exception("list has no second occurrence of " + e2);
}
}
}

Task03 Write a test class to test each one of the methods that were implemented in the previous code. Sample program output: Original Integer array: [ 7535079] After inserting 20 before index 4:[753502079] Element deleted from index 4: 20 After deleting element from index 4:[7535079] After inserting 30 after the second occurence of 7:[753507309]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
