Question: public class SinglyLinkedList { //stores reference to the first node in the list //head is null if the link is empty private Node head; private

 public class SinglyLinkedList { //stores reference to the first node in

public class SinglyLinkedList { //stores reference to the first node in the list //head is null if the link is empty private Node head; private Node tail; public SinglyLinkedList() { head = null; tail = null; } public boolean isEmpty() { return (head == null); } public void addToFront(int value) { //1) create new node containing the new value Node newNode = new Node(value); //2) link this node to the front of current list newNode.setNext(head); //3) update head to point to new node head = newNode; //4) if the list is empty, make sure to update the tail if(tail ==null) { tail = head; } } // add to back of list public void addToBack(int value) { //special case : if the list is empty if (isEmpty()) { head = new Node(value); tail = head; return; } /* //inneficient method //1)find the last node in the list Node end = head; while(end.getNext() != null) // while end is not the end { end = end.getNext(); } */ //efficient method Node end = tail; //2) add a new node after that node Node newNode = new Node(value); end.setNext(newNode); tail = newNode; } //removes the first node of the list public int removeFront() { // first, check to see if we can remove if (isEmpty()) { throw new IllegalStateException("cannot remove from an empty list"); } //grab value before removal int ret = head.getValue(); //just skip the first node head = head.getNext(); //update tail if necessary // if (head == null) { tail = null; } //return removed values return ret; } //print method public String toString() { //goal: print out everything in the list, separated by spaces //using a StringBuilder // when created, sb is empty //like: String ret = ""; StringBuilder sb = new StringBuilder(); //lets add the first element to our string int firstValue = head.getValue(); /* //ret+= firstvalue sb.append(firstValue); */ for (Node tmp = head; tmp != null; tmp = tmp.getNext()) { sb.append(tmp.getValue() + " "); } return sb.toString(); } //private inner class private class Node{ //private variables private int value; // the value private Node next; // reference to next node //constructors public Node(int value, Node next) { this.value = value; this.next = next; } public Node(int value) { this(value, null); }

//getters and setters public int getValue() { return value; }

public void setValue(int value) { this.value = value; }

public Node getNext() { return next; }

public void setNext(Node next) { this.next = next; } }

}

l. New Methods Add the following methods to the SinglyLinkedList class: A method min) that returns the smallest element in the list. . A method max) that returns the largest element in the list. A method removeLast() that removes the last node in list and returns its value. If the list is empty, calling any of these methods should throw an IllegalStateException

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!