Question
Program 14_01:Modify the class LinkedListin Exercise 13_01 to make it a doubly linked list:-Name your class DoublyLinkedListand create a tester class Main.-Use dummy header and
Program 14_01:Modify the class LinkedListin Exercise 13_01 to make it a doubly linked list:-Name your class DoublyLinkedListand create a tester class Main.-Use dummy header and trailer nodes.-Add a printInReversemethod.-Add an appendmethod to add an item at the end of the list.
public class LinkedList implements ListInterface {
public class Node {
private int info; private Node next;
public Node() { info = 0; next = null; }
public void setInfo(int i) { info = i; }
public void setNext(Node l) { next = l; }
public int getInfo() { return info; }
public Node getNext() { return next; } }
private Node first;
public LinkedList() { first = new Node(); }
public boolean isEmpty() { return (first.getNext() == null); }
public void display() { Node current = first.getNext();
while (current != null) { System.out.print(current.getInfo() + " "); current = current.getNext(); }
System.out.println(); }
public boolean search(int x) { Node current = first.getNext();
while (current != null) { if (current.getInfo() == x) { return true; } current = current.getNext(); }
return false; }
public void add(int x) { Node p = new Node();
p.setInfo(x); p.setNext(first.getNext());
first.setNext(p); }
public void remove(int x) { Node old = first.getNext(), p = first;
boolean found = false; while (old != null && !found) { if (old.getInfo() == x) { found = true; } else { p = old; old = p.getNext(); } }
if (found) { p.setNext(old.getNext()); } }
public void insert(int x, int loc) {
}
public void removeItemAt(int loc) {
} }
//Interface
public interface ListInterface {
public boolean isEmpty();
public void display();
public boolean search(int x);
public void add(int x);
public void remove(int x);
public void insert(int x, int loc);
public void removeItemAt(int loc); }
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