For data structure- doubly linked list in Java. I am trying here to create a new
ID: 3569535 • Letter: F
Question
For data structure- doubly linked list in Java. I am trying here to create a new node(called q) and copy the content of the tail to this new node and then delete the tail and make the new node is the tail..But my code is not working.
private void f(Node tail){
Node q = new Node(numOfElementsPerNode, tail, null);
for (int i = 0; i < t; i++) { // t here the last index I want to copy to it
q.data[i] = tail.data[i];
}
removeNode(tail);
tail.next = q;
tail = q;
}
private void removeNode(Node n) { // to remove the tail node
if (n.next == null) {
tail = n.prev;
tail.next = null;
n.prev = null;
} else {
Node k = n.prev;
Node j = n.next;
k.next = j;
j.prev = k;
n.next = null;
n.prev = null;
}
}
Explanation / Answer
import java.util.ListIterator; import java.util.NoSuchElementException; public class DoublyLinkedList implements Iterable { private int N; // number of elements on list private Node pre; // sentinel before first item private Node post; // sentinel after last item public DoublyLinkedList() { pre = new Node(); post = new Node(); pre.next = post; post.prev = pre; } // linked list node helper data type private class Node { private Item item; private Node next; private Node prev; } public boolean isEmpty() { return N == 0; } public int size() { return N; } // add the item to the list public void add(Item item) { Node last = post.prev; Node x = new Node(); x.item = item; x.next = post; x.prev = last; post.prev = x; last.next = x; N++; } public ListIterator iterator() { return new DoublyLinkedListIterator(); } // assumes no calls to DoublyLinkedList.add() during iteration private class DoublyLinkedListIterator implements ListIterator { private Node current = pre.next; // the node that is returned by next() private Node lastAccessed = null; // the last node to be returned by prev() or next() // reset to null upon intervening remove() or add() private int index = 0; public boolean hasNext() { return index 0; } public int previousIndex() { return index - 1; } public int nextIndex() { return index; } public Item next() { if (!hasNext()) throw new NoSuchElementException(); lastAccessed = current; Item item = current.item; current = current.next; index++; return item; } public Item previous() { if (!hasPrevious()) throw new NoSuchElementException(); current = current.prev; index--; lastAccessed = current; return current.item; } // replace the item of the element that was last accessed by next() or previous() // condition: no calls to remove() or add() after last call to next() or previous() public void set(Item item) { if (lastAccessed == null) throw new IllegalStateException(); lastAccessed.item = item; } // remove the element that was last accessed by next() or previous() // condition: no calls to remove() or add() after last call to next() or previous() public void remove() { if (lastAccessed == null) throw new IllegalStateException(); Node x = lastAccessed.prev; Node y = lastAccessed.next; x.next = y; y.prev = x; N--; if (current == lastAccessed) current = y; else index--; lastAccessed = null; } // add element to list public void add(Item item) { Node x = current.prev; Node y = new Node(); Node z = current; y.item = item; x.next = y; y.next = z; z.prev = y; y.prev = x; N++; index++; lastAccessed = null; } } public String toString() { StringBuilder s = new StringBuilder(); for (Item item : this) s.append(item + " "); return s.toString(); } // a test client public static void main(String[] args) { int N = Integer.parseInt(args[0]); // add elements 1, ..., N StdOut.println(N + " random integers between 0 and 99"); DoublyLinkedList list = new DoublyLinkedList(); for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.