public class LinkedList<V> extends Object
Modifier and Type | Class and Description |
---|---|
static class |
LinkedList.Element<V>
Linked list element containing stored value and links to neighbor nodes.
|
Modifier and Type | Field and Description |
---|---|
private LinkedList.Element<V> |
current
Current element in the list used by internal iterator.
|
private LinkedList.Element<V> |
head
List head (first element in the list).
|
private int |
size
List size.
|
private LinkedList.Element<V> |
tail
List tail (last element in the list).
|
Constructor and Description |
---|
LinkedList()
Constructs an instance of linked list.
|
Modifier and Type | Method and Description |
---|---|
void |
addFirst(V value)
Add element at the beginning (before head) of the list.
|
void |
addLast(V value)
Add element at the end (after tail) of the list.
|
void |
addNext(V value)
Add element after current element in the list.
|
void |
addPrevious(V value)
Add element before current element in the list.
|
void |
clear()
Clear linked list content.
|
boolean |
first()
Set current element to the first (head) element.
|
V |
getCurrent()
Get value stored in current element.
|
V |
getFirst()
Get value stored in first (head) element.
|
V |
getLast()
Get value stored in last (tail) element.
|
V |
getNext()
Get value stored in next element (after current).
|
V |
getPrevious()
Get value stored in previous element (before current).
|
boolean |
isCurrent()
Check if there is a current element.
|
boolean |
isEmpty()
Check if list is empty.
|
boolean |
isNext()
Check if there is an element after current element.
|
boolean |
isPrevious()
Check if there is an element before current element.
|
boolean |
last()
Set current element to the last (tail) element.
|
boolean |
next()
Attempt to move current pointer to next element.
|
boolean |
previous()
Attempt to move current pointer to previous element.
|
V |
removeAndNext()
Remove current element and set current element to next one
if exists or
null if there is no next element. |
V |
removeAndNextOrPrevious()
Remove current element and set current element to next one
if exists or previous element as fallback option.
|
V |
removeAndPrevious()
Remove current element and set current element to previous one
if exists or
null if there is no previous element. |
V |
removeAndPreviousOrNext()
Remove current element and set current element to previous one
if exists or next element as fallback option.
|
V |
removeFirst()
Remove first (head) element from the list.
|
V |
removeLast()
Remove last (tail) element from the list.
|
int |
size()
Get current size of list.
|
String |
toString()
Get string representation of list.
|
private LinkedList.Element<V> head
null
.private LinkedList.Element<V> tail
null
.private LinkedList.Element<V> current
null
.private int size
public LinkedList()
public void addLast(V value)
value
- New value to be added at the end of the list.public void addFirst(V value)
value
- New value to be added at the end of the list.public V removeLast()
null
when list is empty.public V removeFirst()
null
when list is empty.public void addNext(V value)
value
- New value to be added at the end of the list.public void addPrevious(V value)
value
- New value to be added at the end of the list.public V removeAndPrevious()
null
if there is no previous element.
null
when list is empty.public V removeAndNext()
null
if there is no next element.
null
when list is empty.public V removeAndPreviousOrNext()
null
except when list
is empty. Additional check may be required to see which direction
current element was moved.
null
when list is empty.public V removeAndNextOrPrevious()
null
except when list
is empty. Additional check may be required to see which direction
current element was moved.
null
when list is empty.public boolean first()
true
if first element exists
or false
otherwise.public boolean last()
true
if last element exists
or false
otherwise.public boolean next()
true
if current pointer was moved
to next element or false
otherwise.public boolean previous()
true
if current pointer was moved
to previous element or false
otherwise.public boolean isEmpty()
true
when list is empty
(contains no elements) or false
otherwise.public boolean isCurrent()
true
when there is a current element
or false
otherwise.public boolean isNext()
true
if there is an element after current
element or false
otherwise.public boolean isPrevious()
true
if there is an element before current
element or false
otherwise.public int size()
public V getCurrent()
null
when list
is empty.public V getFirst()
null
when
list is empty.public V getLast()
null
when
list is empty.public V getNext()
null
when list
is empty or current element is tail.public V getPrevious()
null
when list
is empty or current element is head.public String toString()
public void clear()
size
to 0
. Also all individual elements are cleared to help
garbage collector.Copyright © 2013. All Rights Reserved.