3. Create a randomly generated Doubly linked list of 10 integers using the class
ID: 3650078 • Letter: 3
Question
3. Create a randomly generated Doubly linked list of 10 integers using the class IntDLList.java. For example, your list could be:
[ 3 1 2 5 8 7 9 0 1 6 ].
Now delete every 7th element from the list and print it. Note that if you reach the end then you have to reverse the direction of counting. Your output could be as follows:
After deleting 7th element:
[ 3 1 2 5 8 7 9 0 1 6 ] => [ 3 1 2 5 8 7 0 1 6 ].
After deleting 7th element again (counting in the same direction, then moving reverse),
[ 3 1 2 5 8 7 0 1 6 ] => [ 3 1 2 5 7 0 1 6 ].
After deleting 7th element again (counting in the same direction, then moving reverse),
[ 3 1 2 5 7 0 1 6 ] => [ 3 1 2 7 0 1 6 ]
Continue doing this until there is only one element left in the list.
Add any necessary methods required in the IntDLList.java file.
TheIntDLList.java and all other files, SLLNode and other are here.
https://dl.dropbox.com/u/3075114/LinkedLists-1.rar
Explanation / Answer
public class LinkRecursion { class Node { public int num; public Node next; public Node(int num) { this.num = num; next = null; } } public String listString(Node head) { if (head == null) return ""; String out = "[ " + head.num; for(Node p = head.next; p != null; p = p.next) { out += ", " + p.num; } return out += " ]"; } public Node deleteNum(Node currentNode, int target, int pos) { if(currentNode == null) // empty list, or reached end of list return currentNode; //Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.