Question: JAVA SinglyLinkedList.java public class SinglyLinkedList { //---------------- nested Node class ---------------- private static class Node { private E element; private Node next; public Node(E e,

JAVA

JAVA SinglyLinkedList.java public class SinglyLinkedList { //---------------- nested Node class ---------------- private

SinglyLinkedList.java

public class SinglyLinkedList {

//---------------- nested Node class ----------------

private static class Node {

private E element;

private Node next;

public Node(E e, Node n) {

element = e;

next = n;

}

public E getElement() {

return element;

}

public Node getNext() {

return next;

}

public void setNext(Node n) {

next = n;

}

} //----------- end of nested Node class -----------

private Node head = null;

private Node tail = null;

private int size = 0;

public SinglyLinkedList() {

}

public int size() {

return size;

}

public boolean isEmpty() {

return size == 0;

}

public E first() {

if (isEmpty()) {

return null;

}

return head.getElement();

}

public E last() {

if (isEmpty()) {

return null;

}

return tail.getElement();

}

// update methods

public void addFirst(E e) {

head = new Node(e, head);

if (size == 0) {

tail = head;

}

size++;

}

public void addLast(E e) {

Node newest = new Node(e, null);

if (isEmpty()) {

head = newest;

} else {

tail.setNext(newest);

}

tail = newest;

size++;

}

public E removeFirst() {

if (isEmpty()) {

return null;

}

E answer = head.getElement();

head = head.getNext();

size--;

if (size == 0) {

tail = null;

}

return answer;

}

Lab2_Driver.java

public class Lab2_Driver {

public static void main(String[] args) {

}

Code the SinglyLinkedList class from our class notes (L03_). Override the toString method by using StringBuilder to list every element on a new line, in order from head to tail. In the Lab2_Driver file, create a SinglyLinkedList instance that stores integer values. e.g., SinglyLinkedListnums=newSinglyLinkedList(); Add a list of integers, and display the list in this format using an implicit call to the toString method: [6 0 7 2 1]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!