Question: //**************************** DLL.java ******************************* // generic doubly linked list class import java.util.*; public class DLL implements Iterable { private DLLNode head, tail; public DLL() { head

 //**************************** DLL.java ******************************* // generic doubly linked list class import java.util.*;

//**************************** DLL.java *******************************

// generic doubly linked list class

import java.util.*;

public class DLL implements Iterable {

private DLLNode head, tail;

public DLL() {

head = tail = null;

}

public boolean isEmpty() {

return head == null;

}

public void clear() {

head = tail = null;

}

public T getFirst() {

if (head != null)

return head.info;

else return null;

}

public void addToHead(T el) {

if (head != null) {

head = new DLLNode(el,head,null);

head.next.prev = head;

}

else head = tail = new DLLNode(el);

}

public void addToTail(T el) {

if (tail != null) {

tail = new DLLNode(el,null,tail);

tail.prev.next = tail;

}

else head = tail = new DLLNode(el);

}

public T deleteFromHead() {

if (isEmpty())

return null;

T el = head.info;

if (head == tail) // if only one node on the list;

head = tail = null;

else { // if more than one node in the list;

head = head.next;

head.prev = null;

}

return el;

}

public T deleteFromTail() {

if (isEmpty())

return null;

T el = tail.info;

if (head == tail) // if only one node on the list;

head = tail = null;

else { // if more than one node in the list;

tail = tail.prev;

tail.next = null;

}

return el;

}

public void delete(T el) { // delete the node with an element el;

DLLNode tmp;

for (tmp = head; tmp != null && !el.equals(tmp.info); tmp = tmp.next); //locate the item

if (tmp != null) { // item found

if (head == tail) //the found item was the only one

head = tail = null;

else if (head == tmp) { //the found item is the first

head = head.next;

head.prev = null;

}

else if (tail == tmp) { // the found item is the last

tail = tail.prev;

tail.next = null;

}

else { // the found item is in the middle

tmp.prev.next = tmp.next;

tmp.next.prev = tmp.prev;

}

}

}

public void printAll() {

for (DLLNode tmp = head; tmp != null; tmp = tmp.next)

System.out.print(tmp.info + " ");

}

public T find(T el) {

DLLNode tmp;

for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);

if (tmp == null)

return null;

else return tmp.info;

}

public Iterator iterator() {

return new DLLIterator();

}

private class DLLIterator implements Iterator {

DLLNode tmp = head;

public boolean hasNext() {

return tmp != null;

}

public T next() {

T info = tmp.info;

tmp = tmp.next;

return info;

}

public void remove() {

// not implemented

}

}

// node of generic doubly linked list class

private class DLLNode {

public T info;

public DLLNode next, prev;

public DLLNode() {

next = null; prev = null;

}

public DLLNode(T el) {

info = el; next = null; prev = null;

}

public DLLNode(T el, DLLNode n, DLLNode p) {

info = el; next = n; prev = p;

}

}

}

iv) public void addToTail(DLL list That appends the list received as argument to the end of the current (this) linked list

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!