Package cs2110
Class DLinkedSeq<T>
java.lang.Object
cs2110.DLinkedSeq<T>
A sequence of elements of type `T` implemented as a doubly-linked list. Null elements are not
allowed.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classA forward iterator over a doubly-linked sequence.private classA node of a doubly-linked sequence whose elements have type `T`. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate DLinkedSeq<T>.DNodeFirst node of the doubly-linked sequence (null for empty sequence).private intNumber of elements in the sequence.private DLinkedSeq<T>.DNodeLast node of the doubly-linked sequence (null for empty sequence). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd element `elem` to the end of this list.private voidAssert that this object satisfies its class invariants.booleanReturn whether this list contains an element equal to `elem` (according to `equals()`).booleanReturn whether this and `other` are `DLinkedSeq`s containing the same elements in the same order.private DLinkedSeq<T>.DNodefirstNodeWith(T elem) Return the first node `n` such that `n.data.equals(elem)`, or null if this sequence does not contain `elem`.get(int index) Return the element at index `index` in this list.inthashCode()Returns a hash code value for the object.voidinsertBefore(T elem, T successor) Insert element `elem` into the list just before the first occurrence of element `successor`.iterator()Return an iterator over the elements of this sequence (in order).voidInsert element `elem` at the beginning of this list.booleanRemove the first occurrence of element `elem` (if any) from this list.intsize()Return the number of elements in this list.toString()Return a text representation of this sequence with the following format: the string starts with '[' and ends with ']'.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
size
private int sizeNumber of elements in the sequence. Equal to the number of linked nodes reachable from `head` (including `head` itself) using `next` arrows. -
head
First node of the doubly-linked sequence (null for empty sequence). `head.prev` must be null -
tail
Last node of the doubly-linked sequence (null for empty sequence). `tail.next` must be null.
-
-
Constructor Details
-
DLinkedSeq
public DLinkedSeq()Create an empty sequence.
-
-
Method Details
-
assertInv
private void assertInv()Assert that this object satisfies its class invariants. -
size
public int size()Description copied from interface:SeqReturn the number of elements in this list. -
prepend
Description copied from interface:SeqInsert element `elem` at the beginning of this list. Example: if the list is [8, 7, 4], prepend(2) would change the list to [2, 8, 7, 4]. Requires `elem` is not null. -
append
Description copied from interface:SeqAdd element `elem` to the end of this list. Example: if the list is [8, 7, 4], append(2) would change the list to [8, 7, 4, 2]. Requires `elem` is not null. -
get
Description copied from interface:SeqReturn the element at index `index` in this list. The index of the first element is 0. Requires0 <= index < size(). Will not return null. -
firstNodeWith
Return the first node `n` such that `n.data.equals(elem)`, or null if this sequence does not contain `elem`. -
contains
Description copied from interface:SeqReturn whether this list contains an element equal to `elem` (according to `equals()`). Requires `elem` is not null. -
insertBefore
Description copied from interface:SeqInsert element `elem` into the list just before the first occurrence of element `successor`. Requires that `successor` is contained in the list and that `elem` and `successor` are not null.Example: If the list is [3, 8, 2], then insertBefore(1, 8) would change the list to [3, 1, 8, 2].
- Specified by:
insertBeforein interfaceSeq<T>
-
remove
Description copied from interface:SeqRemove the first occurrence of element `elem` (if any) from this list. Return whether the list changed. Requires `elem` is not null. -
equals
Return whether this and `other` are `DLinkedSeq`s containing the same elements in the same order. Two elements `e1` and `e2` are "the same" if `e1.equals(e2)`. Note that `DLinkedSeq` is mutable, so equivalence between two objects may change over time. See `Object.equals()` for additional guarantees. -
hashCode
public int hashCode()Returns a hash code value for the object. See `Object.hashCode()` for additional guarantees. -
toString
Return a text representation of this sequence with the following format: the string starts with '[' and ends with ']'. In between are the string representations of each element, in sequence order, separated by ", ".Example: a sequence containing 4 7 8 in that order would be represented by "[4, 7, 8]".
Example: a sequence containing two empty strings would be represented by "[, ]".
The string representations of elements may contain the characters '[', ',', and ']'; these are not treated specially.
-
iterator
Return an iterator over the elements of this sequence (in order). By implementing `Iterable`, clients can use Java's "enhanced for-loops" to iterate over the elements of the sequence. Requires that the sequence not be mutated while the iterator is in use (except via a single active Iterator's `remove()` method).
-