Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

More Books

Students also viewed these Databases questions

Question

3. Comment on how diversity and equality should be managed.

Answered: 1 week ago

Question

describe the legislation that addresses workplace equality

Answered: 1 week ago