Question: Using IntelliJ, fix the errors in the 2 given code files SinglyLinkedList File: public class SinglyLinkedList { Node front, rear; Node

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 toAppend = new Node(data);
        if(this.head == null){
            this.head = toAppend;
        }
        else{
            Node tempNode = this.head;
            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 tempNode = this.head;
            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());
    }

 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Answer To fix the errors and improve the given SinglyLinkedList implementation in Java using IntelliJ several modifications need to be made The original code has issues such as missing generics incorr... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Operating System Questions!