Question: To class DoublyLinkedList, add method removeThird() which removes the node number 3 (assume the first node in the list is number 1), and returns its

To class DoublyLinkedList, add method removeThird() which removes the node number 3 (assume the first node in the list is number 1), and returns its data. You should ensure that the list has at least five nodes, otherwise, the method returns null.

(Use Java to do the code).

This is the DoublyLinkedList class:

public class DoublyLinkedList { 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().getData(); } public E last() { if (isEmpty()) return null; return trailer.getPrev().getData(); } private void addBetween(E e, Node predecessor, Node successor) { Node newest=new Node<>(e,predecessor,successor); predecessor.setNext(newest); successor.setPrev(newest); size++; } private E remove(Node node) { Node predecessor=node.getPrev(); Node successor=node.getNext(); predecessor.setNext(successor); successor.setPrev(predecessor); size--; return node.getData(); } 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()); } public void printForward() { for (Node tmp=header.getNext();tmp!=trailer;tmp=tmp.getNext()) System.out.println(tmp.getData()); } public void printBackward() { for (Node tmp=trailer.getPrev();tmp!=header;tmp=tmp.getPrev()) System.out.println(tmp.getData()); } }

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!