Question: Can you please read this code and documented ( i mean each line write what it does please ) public class CDoublyLinkedList { private class

Can you please read this code and documented ( i mean each line write what it does please )

public class CDoublyLinkedList {

private class Node { private Object data; //Assume data implemented Comparable private Node next, prev; private Node(Object data, Node pref, Node next) { this.data = data; this.prev = pref; this.next = next; } }

private Node head; private int size;

public CDoublyLinkedList() { this.head = new Node(null, null, null ); this.head.next = this.head; this.head.prev=this.head; this.size = 0; }

public boolean isEmpty() { return this.head == this.head.next; } public void addFirst(Object data) { Node nn = new Node(data, this.head, this.head.next); this.head.next.prev = nn; this.head.next = nn; this.size ++; }

public void addLast(Object data) { Node nn = new Node(data, this.head.prev, this.head); this.head.prev.next = nn; this.head.prev = nn; this.size++; }

public CDoublyLinkedList subListOfSmallerValues(Comparable data) { CDoublyLinkedList small = new CDoublyLinkedList(); if (data == null) { return small; } else { Node cur = this.head.next; while (cur != this.head) { if (cur != null && ((Comparable)cur.data).compareTo(data) < 0) { small.addLast(cur.data); } cur = cur.next; } } return small; } public boolean removeStartingAtBack(Object dataToRemove) { Node cur = this.head.prev; boolean toRem = false; while (cur != this.head) { if (cur.data == null || dataToRemove == null) { if (cur.data == null && dataToRemove == null) { toRem = true; } } else if (cur.data.equals(dataToRemove)) { toRem = true; } if (toRem) { cur.prev.next = cur.next; cur.next.prev = cur.prev; this.size--; return true; } cur = cur.prev; } return false;//change this as needed. } public int lastIndexOf(Object o) { Node cur = this.head.prev; int index = this.size - 1; while (cur != this.head) { if (cur.data == null && o == null) { return index; } else if (cur.data != null && o != null && cur.data.equals(o)) { return index; } cur = cur.prev; index--; } return -1; //change this as needed. } public boolean retainAll(CDoublyLinkedList other) throws NullPointerException { if (other == null) { throw new NullPointerException("Input list is null"); } boolean del = false; Node cur = this.head.next; while(cur != this.head) { if (!other.contains(cur.data)) { cur.prev.next = cur.next; cur.next.prev = cur.prev; if (del == false) { del = true; } } cur = cur.next; } return del; }

private boolean contains(Object data) { Node cur = this.head.next; while(cur != this.head) { if(cur.data == null && data == null) { return true; } else if (cur.data != null && data != null && cur.data.equals(data)) { return true; } cur = cur.next; } return false; } public void insertionSort() { Node curBack = this.head.next, fUnsort = curBack.next, nn; while (fUnsort != this.head) { while (curBack != this.head && ((Comparable)curBack.data).compareTo(fUnsort.data) > 0) { curBack = curBack.prev; } if (curBack != fUnsort.prev) { nn = new Node(fUnsort.data, curBack, curBack.next); curBack.next.prev = nn; curBack.next = nn; fUnsort.prev.next = fUnsort.next; fUnsort.next.prev = fUnsort.prev; } fUnsort = fUnsort.next; curBack = fUnsort.prev; } } @Override public String toString() { String result = "{"; for (Node node = this.head.next; node != this.head; node = node.next) { if(node.next != this.head) result += node.data + "->"; else result += node.data; } return result + "}"; } }

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!