Question: Q1) To class SinglyList, add method removeBeforeLast() which removes the node before the last node, consider all possible cases. Q2) To class DoublyLinkedList, add method

Q1) To class SinglyList, add method removeBeforeLast() which removes the node before the last node, consider all possible cases.

Q2) To class DoublyLinkedList, add method reverse which reverse the order of the elements in the list.

Q3) Our implementation of a doubly list relies on two sentinel nodes, header and trailer. Re-implement the DoublyLinkedList without using these nodes. class SinglyList:

public class SinglyLinkedList{

public 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 next) {

this.next = next;

}

}

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();

}

}

class DoublyLinkedList:

public class DoublyLinkedList {

public static class Node{

private E element;

private Node prev;

private Node next;

public Node(E e, Node p, Node n){

element = e;

prev = p;

next = n;

}

public E getElement() {

return element;

}

public Node getPrev() {

return prev;

}

public Node getNext() {

return next;

}

public void setPrev(Node p) {

next = p;

}

public void setNext(Node n) {

next = n;

}

}

private Node header;

private Node trailer;

private int size = 0;

public DoublyLinkedList(){

header = new Node<>(null,null,null);

trailer = new Node<>(null, header, null);

header.setNext(trailer);

}

public int size(){

return size;

}

public boolean isEmpty(){

return size ==0;

}

public E first(){

if(isEmpty())

return null;

return header.getNext().getElement();

}

public E last(){

if(isEmpty())

return null;

return trailer.getPrev().getElement();

} }

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!