Question: Write an instance method public boolean isPalindrome() of the DLL class that returns true if the invoking list is a palindrome; otherwise it returns false


Write an instance methodpublic boolean isPalindrome()of theDLLclass that returnstrueif the invoking list is a palindrome; otherwise it returnsfalse. Your method must throw an appropriate exception if the invoking list is empty.

Note: Your method must not use another list, another data structure or recursion; but it may call anotherDLLinstance method you implement.

Task02

Write an instance methodpublic void delete(T e, int k)of theDLLclass which deletesknodes following the node that has the first occurrence of datae. Your method must throw appropriate exceptions if the list is empty or ifkis negative or if there is no node with datae, or if there are noknodes following the node that has the first occurrence of datae.

For example, the original list is: [ 4 1 2 5 8 7 9 0],

then, the calldelete(5, 3)will result in the list [4 1 2 5 0],

the calldelete(7, 2)will result in the list [4 1 2 5 8 7],

the calldelete(4, 6)will result in the list [4 0],

the calldelete(4, 0)will result in the original list [ 4 1 2 5 8 7 9 0]

the calldelete(8, 4)will throw an exception because there are no 4 nodes after the node with element 8.

Note: Your method must not use any other DLL method except isEmpty().

Task03

Write an instance methodpublic void insertListBefore(DLL list1, T e)of theDLLclass that takes a doubly linked listlist1as a parameter and inserts it into the invoking list before the node with the first occurrence of datae. Your method must throw appropriate exceptions if any of the lists is empty or if a node with dataedoes not exist in the invoking list.

For example, if the invoking list is [8 2 9 4] andlist1is [30 60 7 80 50],

then after the method callinsertBefore(list1, 9)the invoking list becomes [8 230 60 7 80509 4],

after the method callinsertBefore(list1, 8)the invoking list becomes [30 60 7 80508 2 9 4],

The method callinsertBefore(list1, 20)throws an exception because the invoking list does not have node with data 20.

Note: Your method must not use any other DLL method except isEmpty().

Task04

Write a test class to test the DLL instance methods that you are required to write in Task01, Task02, and Task03.

The output of your test program must be in the following format:

TASK01 TEST: [ m a d a m ] is palindrome is true [ c u p s ] is palindrome is false TASK02 TEST: List before deleting 5 nodes after node 4: [ 4 1 2 5 8 7 9 0 ] List after deleting 5 nodes after node 4: [ 4 9 0 ] TASK03 TEST: The invoking list before inserting the list [30 60 7 80 50] before node 9: [ 8 2 9 4 ] The invoking list after inserting the list [30 60 7 80 50] before node 9: [ 8 2 30 60 7 80 50 9 4 ]

//**************************** DLL.java ******************************* // generic doubly linked list class

public class DLL {

private class DLLNode { private T info; private DLLNode next, prev; private DLLNode() { next = null; prev = null; } public DLLNode(T e) { info = e; next = null; prev = null; } public DLLNode(T e, DLLNode n, DLLNode p) { info = e; next = n; prev = p; } }

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 T getLast() { if (tail != null) return tail.info; else return null; } public void addToHead(T e) { if (head != null) { head = new DLLNode(e,head,null); head.next.prev = head; } else head = tail = new DLLNode(e); } public void addToTail(T e) { if (tail != null) { tail = new DLLNode(e,null,tail); tail.prev.next = tail; } else head = tail = new DLLNode(e); } public T deleteFromHead() { if (isEmpty()) return null; T e = 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 e; } public T deleteFromTail() { if (isEmpty()) return null; T e = 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 e; } @Override public String toString() { if(head == null) return \"[ ]\"; String str = \"[ \"; DLLNode tmp = head; while(tmp != null){ str += tmp.info + \" \"; tmp = tmp.next; } return str + \"]\"; } public boolean contains(T e) { if(head == null) return false; DLLNode tmp = head; while(tmp != null){ if(tmp.info.equals(e)) return true; tmp = tmp.next; } return false; } }




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 Programming Questions!