Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

JAVA

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions