Can you make a driver program for the DoublyLinkedList given bellow, how the output of the driver should look like will be attached bellow. public class DoublyLinkedList { private 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) { prev = 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(); } 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 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.getElement(); } public String toString() { StringBuilder sb = new StringBuilder("("); Node walk = header.getNext(); while (walk != trailer) { sb.append(walk.getElement()); walk = walk.getNext(); if (walk != trailer) sb.append(", "); } sb.append(")"); return sb.toString(); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Can you make a driver program for the DoublyLinkedList given bellow, how the output of the driver should look like will be attached bellow.

public class DoublyLinkedList<E> {
private static class Node<E> {
private E element;
private Node<E> prev;
private Node<E> next;
public Node(E e, Node<E> p, Node<E> n) {
element = e;
prev = p;
next = n;
}
public E getElement() { return element; }
public Node<E> getPrev() { return prev; }
public Node<E> getNext() { return next; }
public void setPrev(Node<E> p) { prev = p; }
public void setNext(Node<E> n) { next = n; }
}
private Node<E> header;
private Node<E> 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();
}
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 void addBetween(E e, Node<E> predecessor, Node<E> successor) {
Node<E> newest = new Node<>(e, predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
private E remove(Node<E> node) {
Node<E> predecessor = node.getPrev();
Node<E> successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement();
}
public String toString() {
StringBuilder sb = new StringBuilder("(");
Node<E> walk = header.getNext();
while (walk != trailer) {
sb.append(walk.getElement());
walk = walk.getNext();
if (walk != trailer)
sb.append(", ");
}
sb.append(")");
return sb.toString();
}
}

The output of the driver file should look something like this:

The text illustrates the operations on a linked list with a series of node additions and deletions.

1. **Initial State:**
   - Head: null
   - Current: null
   - Tail: null
   - Items: 0

2. **Add a Node to an Empty List:**
   - Action: Add `CSC201`
   - Head: `CSC201`
   - Current: null
   - Tail: `CSC201`
   - Items: 1

3. **Add Nodes to the End of the List:**
   - Action: Add `CSC202`
   - Head: `CSC201`
   - Current: `CSC201`
   - Tail: `CSC202`
   - Items: 2
   - List: `CSC201`, `CSC202`
   
   - Action: Add another `CSC202`
   - Head: `CSC201`
   - Current: `CSC202`
   - Tail: `CSC202`
   - Items: 3
   - List: `CSC201`, `CSC202`, `CSC202`

4. **Add Node to the Middle of the List:**
   - Action: Add `ENG211` between `CSC202` nodes
   - Head: `CSC201`
   - Current: `CSC202`
   - Tail: `CSC202`
   - Items: 4
   - List: `CSC201`, `CSC202`, `ENG211`, `CSC202`

5. **Delete the First Node:**
   - Action: Remove `CSC201`
   - List: `CSC202`, `ENG211`, `CSC202`

6. **Add More Nodes to the End of the List:**
   - Action: Add `COM110`, `MTH166`
   - Head: `CSC202`
   - Current: `COM110`
   - Tail: `MTH166`
   - Items: 5
   - List: `CSC202`, `ENG211`, `CSC202`, `COM110`, `MTH166`

This demonstrates various linked list operations such as insertion at the end, insertion in the middle, and deletion from the beginning, with updates to the head, current, and tail pointers.
Transcribed Image Text:The text illustrates the operations on a linked list with a series of node additions and deletions. 1. **Initial State:** - Head: null - Current: null - Tail: null - Items: 0 2. **Add a Node to an Empty List:** - Action: Add `CSC201` - Head: `CSC201` - Current: null - Tail: `CSC201` - Items: 1 3. **Add Nodes to the End of the List:** - Action: Add `CSC202` - Head: `CSC201` - Current: `CSC201` - Tail: `CSC202` - Items: 2 - List: `CSC201`, `CSC202` - Action: Add another `CSC202` - Head: `CSC201` - Current: `CSC202` - Tail: `CSC202` - Items: 3 - List: `CSC201`, `CSC202`, `CSC202` 4. **Add Node to the Middle of the List:** - Action: Add `ENG211` between `CSC202` nodes - Head: `CSC201` - Current: `CSC202` - Tail: `CSC202` - Items: 4 - List: `CSC201`, `CSC202`, `ENG211`, `CSC202` 5. **Delete the First Node:** - Action: Remove `CSC201` - List: `CSC202`, `ENG211`, `CSC202` 6. **Add More Nodes to the End of the List:** - Action: Add `COM110`, `MTH166` - Head: `CSC202` - Current: `COM110` - Tail: `MTH166` - Items: 5 - List: `CSC202`, `ENG211`, `CSC202`, `COM110`, `MTH166` This demonstrates various linked list operations such as insertion at the end, insertion in the middle, and deletion from the beginning, with updates to the head, current, and tail pointers.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education