Package dsa.lab02.exercises
Class SinglyLinkedList<Item>
java.lang.Object
dsa.lab02.exercises.SinglyLinkedList<Item>
- Type Parameters:
Item
- the item type
- All Implemented Interfaces:
Container<Item>
,DynamicSequence<Item>
,LinkedList<Item>
,StaticSequence<Item>
,Iterable<Item>
A singly-linked list.
Nodes are only directly linked to their successors. Holds references to both the first and last nodes (if non-empty).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A node in a singly-linked list.Nested classes/interfaces inherited from interface dsa.lab02.base.LinkedList
LinkedList.ForwardNodeIterator<Item>, LinkedList.NodeIterator<Item>, LinkedList.ReverseNodeIterator<Item>
Nested classes/interfaces inherited from interface dsa.lab02.base.StaticSequence
StaticSequence.ForwardIterator<Item>, StaticSequence.ReverseIterator<Item>
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct an empty singly-linked list.SinglyLinkedList
(Item... items) Construct a singly-linked list containing the given items.SinglyLinkedList
(Iterable<Item> items) Construct a singly-linked list containing the given items. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface dsa.lab02.base.DynamicSequence
insertFirst, insertLast, removeFirst, removeLast
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface dsa.lab02.base.LinkedList
firstNode, get, items, lastNode, nodes, reversed, reversedNodes, set
-
Constructor Details
-
SinglyLinkedList
public SinglyLinkedList()Construct an empty singly-linked list. -
SinglyLinkedList
Construct a singly-linked list containing the given items.- Parameters:
items
- the items
-
SinglyLinkedList
Construct a singly-linked list containing the given items.- Parameters:
items
- the items
-
-
Method Details
-
size
public int size()Description copied from interface:Container
Get the number of contained items. -
node
Description copied from interface:LinkedList
Get the node at the given index.- Specified by:
node
in interfaceLinkedList<Item>
- Parameters:
index
- the index- Returns:
- the node that is at that index
- Throws:
IndexOutOfBoundsException
- ifindex
< 0 orindex
>=n
(wheren
is the size)
-
insert
Description copied from interface:DynamicSequence
Insert the given item at the given index.Increments the size, as well as the indices of all items that had the same index or higher.
- Specified by:
insert
in interfaceDynamicSequence<Item>
- Parameters:
index
- the indexitem
- the new item- Throws:
IndexOutOfBoundsException
- ifindex
< 0 orindex
>n
(wheren
is the size)
-
remove
Description copied from interface:DynamicSequence
Remove and return the item at the given index.Decrements the size, as well as the indices of all items that had the same index or higher.
- Specified by:
remove
in interfaceDynamicSequence<Item>
- Parameters:
index
- the index- Returns:
- the item that was at that index
- Throws:
IndexOutOfBoundsException
- ifindex
< 0 orindex
>=n
(wheren
is the size)
-