Question
1) To class DoublyLinkedList , add method reverse which reverse the order of the elements in the list. 2) Our implementation of a doubly list
1) To class DoublyLinkedList, add method reverse which reverse the order of the elements in the list. 2) Our implementation of a doubly list relies on two sentinel nodes, header and trailer. Reemployment
the DoublyLinkedList without using these nodes.
public class DoublyLinkedList
private Node
private Node
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();
}
private void addBetween(E e, Node
{
Node
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
private E remove(Node
{
Node
Node
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return e.getElement();
}
public void addFirst(E e)
{
addBetween(e,header,header.getNext());
}
public void addLast(E e)
{
addBetween(e,trailer.getPrev(),trailer);
}
public E removeFirst()
{
if(isEmpty()) return null;
return remove(header.getNext());
}
public E removeLast()
{
if(isEmpty()) return null;
return remove(trailer.getPrev());
}
private static class Node
private E element;
private Node
private Node
public Node(E e,Node
{
element=e;
prev=p;
next=n;
}
public E getElement(){ return element; }
public Node
public Node
public void setPrev(Node
public void setNext(Node
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started