Using IntelliJ, fix the errors in the 2 given code files SinglyLinkedList File: public class SinglyLinkedList {
Question:
Using IntelliJ, fix the errors in the 2 given code files
SinglyLinkedList File:
public class SinglyLinkedList
Node front, rear;
Node head;
//here we initialize the front and rear with same because in the starting both are null
public SinglyLinkedList(){
this.head = null;
this.front = this.rear = null;
}
void enqueue(int key)
{
// Create new LL node
Node temp = new Node(key) {
};
// If queue is empty, then new node is front and
// rear both
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
// Add the new node at the end of queue and change
// rear
this.rear.next = temp;
this.rear = temp;
}
void dequeue()
{
// If queue is empty, return NULL.
if (this.front == null)
return;
// Store previous front and move front one node ahead
Node Temp = this.front;
this.front = this.front.next;
// If front becomes NULL, then change rear also as NULL
if (this.front == null)
this.rear = null;
}
public SinglyLinkedList append(T data){
//appending a node at the end of the linked list
Node
if(this.head == null){
this.head = toAppend;
}
else{
Node
while(tempNode.next != null){
tempNode = tempNode.next;
}
tempNode.next = toAppend;
}
return this;
}
public boolean contains(T data){
//Checking the elements of a list if includes a node with 'data'
if(this.head == null) return false;
else {
Node
while (tempNode != null) {
if (tempNode.data == data) return true;
else tempNode = tempNode.next;
}
}
return false;
}
public SinglyLinkedList delete(T data) {
//Delete the element of a list if includes a node with 'data'
if (this.head == null) return this;
else {
if (this.contains(data)) {
Node tempNode = head;
if (head.data == data) head = head.next;
else {
while (tempNode.next != null) {
if (tempNode.next.data == data)
tempNode.next = tempNode.next.next;
else tempNode = tempNode.next;
}
}
}
}
return this;
}
@Override
public String toString(){
StringBuilder stb = new StringBuilder();
if(this.head == null){
stb.append("");
}
else {
Node toPrint = head;
while (toPrint != null) {
stb = stb.append(toPrint.data);
stb = stb.append(" --> ");
toPrint = toPrint.next;
}
}
stb = stb.append("NULL");
return stb.toString();
}
}
SinglyLinkedListTest File:
class SinglyLinkedListTest {
&..;@org.junit.jupiter.api.Test
void testAppend() {
SinglyLinkedList sll = new SinglyLinkedList();
sll = sll.append(1);
assertEquals("1 --> NULL", sll.toString());
sll = sll.append(2);
sll = sll.append(3);
assertEquals("1 --> 2 --> 3 --> NULL", sll.toString());
}
&..;@org.junit.jupiter.api.Test
void testContains() {
SinglyLinkedList sll = new SinglyLinkedList();
sll = sll.append(1);
sll = sll.append(2);
sll = sll.append(3);
//assertTrue(sll.contains(3));
}
&..;@org.junit.jupiter.api.Test
void testdelete() {
SinglyLinkedList sll = new SinglyLinkedList();
sll = sll.append(1);
sll = sll.append(2);
sll = sll.append(3);
sll = sll.append(4);
sll = sll.delete(1);
assertEquals("2 --> 3 --> NULL", sll.toString());
sll = sll.delete(3);
assertEquals("1 --> 2 --> 4 --> NULL", sll.toString());
}
&..;@org.junit.jupiter.api.Test
void testEnqueue(){
SinglyLinkedList sll = new SinglyLinkedList();
sll = sll.enqueue(1);
sll = sll.enqueue(2);
sll = sll.enqueue(3);
sll = sll.enqueue(4);
assertEquals("1 --> 2 --> 3 --> 4 --> NULL", sll.toString());
}
&..;@org.junit.jupiter.api.Test
void testDequeue(){
sll = sll.enqueue(1);
sll = sll.enqueue(2);
sll = sll.enqueue(3);
sll = sll.enqueue(4);
sll = sll.dequeue(2);
assertEquals("1 --> 3 --> 4 --> NULL", sll.toString());
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill