Question: Do the following task in java language. Write a method insertAlternate(DLL newList) that takes a doubly linked list newList as a parameter and inserts the
Do the following task in java language.
Write a method insertAlternate(DLL newList) that takes a doubly linked list newList as a parameter and inserts the elements of newList into the invocating list alternately. For example, if the invocating list is [1 2 3 4] and the newList is [5 6 7 8], then after the method call the invocating list is [1 5 2 6 3 7 4 8]. Assume that the lengths of the invocating (calling) list and newList are the same.
/// DLL.java
public class DLL { private DLLNode head, tail; public DLL() { head = tail = null; } public boolean isEmpty() { return head == null; } public void setToNull() { head = tail = null; } public T firstEl() { 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 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; } }
/// DLLNode.java
public 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; } }
/// DLLTest.java
public class DLLTest { public static void main(String[] args) { DLL test = new DLL(); for(int i = 0; i < 5; i++) test.addToTail("a" + i); test.printAll(); } }