Question: write swap method based on this code public class DLL { //what are the differences (SLL) (DLL) private DLLNode head, tail; public DLL() { head
write swap method based on this code
public class DLL
//what are the differences (SLL) (DLL) private DLLNode
public DLL() { head = tail = null; }
public boolean isEmpty() { return head == null; }
public void clear() { head = tail = null; } //An example for solving the task for the SLL public T getFirst() { if (head != null) { return head.info; } else { return null; } }
//Observe the differences (SLL) (DLL)
public void addToHead(T el) { if (head != null) { head = new DLLNode
public void addToTail(T el) { if (tail != null) { tail = new DLLNode
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 T delete(T el) { if (isEmpty()) { return null; } else if (head.info.equals(el)) { return deleteFromHead(); //we can use any method that we alredy decleared } else if (tail.info.equals(el)) { return deleteFromTail(); } else { DLLNode
public void printAll() { for (DLLNode
public T find(T el) { DLLNode
// node of generic doubly linked list class class DLLNode
public T info; public DLLNode
public DLLNode() { next = null; prev = null; }
public DLLNode(T el) { info = el; next = null; prev = null; }
public DLLNode(T el, DLLNode
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
