From 150bf2ba9b0d49df6b15ee364850790f3ccb1b0b Mon Sep 17 00:00:00 2001 From: Aditya Date: Mon, 12 Sep 2022 18:59:21 +0530 Subject: [PATCH] finish DList --- java/DList/DLink.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/java/DList/DLink.java b/java/DList/DLink.java index 064f51c..eb3d970 100644 --- a/java/DList/DLink.java +++ b/java/DList/DLink.java @@ -11,11 +11,46 @@ class DLink { /** Get and set methods for the data members */ DLink next() { return next; } + DLink setNext(DLink nextval) { return next = nextval; } + DLink prev() { return prev; } + DLink setPrev(DLink prevval) { return prev = prevval; } + E element() { return element; } + E setElement(E it) { return element = it; } + + /** Insert "it: at current position */ + public void insert(E it) { + curr.setNext(new DLink(it, curr, curr.next())); + curr.next().next().setPrev(curr.next()); + ++cnt; + } + + /** Append "it" to the list */ + public void append(E it) { + tail.setPrev(new DLink(it, tail.prev(), tail)); + tail.prev().prev().setNext(tail.prev()); + ++cnt; + } + + /** Remove and return current element */ + public E remove() { + if (curr.next() == tail) return null; // Nothing to remove + E it = curr.next().element(); // Remember value + curr.next().next().setPrev(curr); + curr.setNext(curr.next().next()); // Remove from list + --cnt; // Decrement the count + return it; + } + + /** Move curr one step leftl no change if at front */ + public void prev() { + if (curr != head) // Can't back up from list head + curr = curr.prev(); + } }