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 head, tail;

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(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 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 tmp; for (tmp = head.next; tmp != null && !tmp.info.equals(el); tmp = tmp.next); if (tmp != null) { tmp.prev.next = tmp.next;//take care of next tmp.next.prev = tmp.prev;//take care of prev return tmp.info; } else { //el is not found in the list return null; } } }

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; } }

// node of generic doubly linked list class 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; } } //End of DLLNode class } //End of DLL class

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!