mirror of
https://git.adityakumar.xyz/data-structures.git
synced 2024-12-22 19:52:52 +00:00
finish DList
This commit is contained in:
parent
ee19e2974c
commit
150bf2ba9b
1 changed files with 35 additions and 0 deletions
|
@ -11,11 +11,46 @@ class DLink<E> {
|
|||
|
||||
/** Get and set methods for the data members */
|
||||
DLink<E> next() { return next; }
|
||||
|
||||
DLink<E> setNext(DLink<E> nextval)
|
||||
{ return next = nextval; }
|
||||
|
||||
DLink<E> prev() { return prev; }
|
||||
|
||||
DLink<E> setPrev(DLink<E> 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<E>(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<E>(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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue