Class LinkedList<E>

java.lang.Object
  extended by LinkedList<E>
All Implemented Interfaces:
IList<E>, Iterable<E>
Direct Known Subclasses:
LinkedListSolution

public class LinkedList<E>
extends Object
implements IList<E>


Constructor Summary
LinkedList()
           
 
Method Summary
 void add(E item)
          Add an item to the end of this list.
 void addFirst(E item)
          add item to the front of the list.
 void addLast(E item)
          add item to the end of the list.
 boolean equals(Object other)
          check if this list is equal to another Object.
 E get(int pos)
          Get an element from the list.
 IList<E> getSubList(int start, int stop)
          Return a sublist of elements in this list from start inclusive to stop exclusive.
 int indexOf(E item)
          Find the position of an element in the list.
 int indexOf(E item, int pos)
          find the position of an element in the list starting at a specified position.
 void insert(int pos, E item)
          Insert an item at a specified position in the list.
 Iterator<E> iterator()
          return an Iterator for this list.
 void makeEmpty()
          return the list to an empty state.
 boolean remove(E obj)
          Remove the first occurrence of obj in this list.
 E remove(int pos)
          Remove an element in the list based on position.
 E removeFirst()
          remove and return the first element of this list.
 E removeLast()
          remove and return the last element of this list.
 void removeRange(int start, int stop)
          Remove all elements in this list from start inclusive to stop exclusive.
 E set(int pos, E item)
          Change the data at the specified position in the list.
 int size()
          Return the size of this list.
 String toString()
          Return a String version of this list enclosed in square brackets, [].
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LinkedList

public LinkedList()
Method Detail

add

public void add(E item)
Description copied from interface: IList
Add an item to the end of this list.
pre: none
post: size() = old size() + 1, get(size() - 1) = item

Specified by:
add in interface IList<E>
Parameters:
item - the data to be added to the end of this list

insert

public void insert(int pos,
                   E item)
Description copied from interface: IList
Insert an item at a specified position in the list.
pre: 0 <= pos <= size()
post: size() = old size() + 1, get(pos) = item, all elements in the list with a positon >= pos have a position = old position + 1

Specified by:
insert in interface IList<E>
Parameters:
pos - the position to insert the data at in the list
item - the data to add to the list

set

public E set(int pos,
             E item)
Description copied from interface: IList
Change the data at the specified position in the list. the old data at that position is returned.
pre: 0 <= pos < size()
post: get(pos) = item, return the old get(pos)

Specified by:
set in interface IList<E>
Parameters:
pos - the position in the list to overwrite
item - the new item that will overwrite the old item
Returns:
the old data at the specified position

get

public E get(int pos)
Description copied from interface: IList
Get an element from the list.
pre: 0 <= pos < size()
post: return the item at pos

Specified by:
get in interface IList<E>
Parameters:
pos - specifies which element to get
Returns:
the element at the specified position in the list

remove

public E remove(int pos)
Description copied from interface: IList
Remove an element in the list based on position.
pre: 0 <= pos < size()
post: size() = old size() - 1, all elements of list with a positon > pos have a position = old position - 1

Specified by:
remove in interface IList<E>
Parameters:
pos - the position of the element to remove from the list
Returns:
the data at position pos

remove

public boolean remove(E obj)
Description copied from interface: IList
Remove the first occurrence of obj in this list. Return true if this list changed as a result of this call, false otherwise.
pre: none
post: if obj is in this list the first occurence has been removed and size() = old size() - 1. If obj is not present the list is not altered in any way.

Specified by:
remove in interface IList<E>
Parameters:
obj - The item to remove from this list.
Returns:
Return true if this list changed as a result of this call, false otherwise.

getSubList

public IList<E> getSubList(int start,
                           int stop)
Description copied from interface: IList
Return a sublist of elements in this list from start inclusive to stop exclusive. This list is not changed as a result of this call.
pre: 0 <= start < size(), start <= stop <= size()
post: return a list whose size is stop - start and contains the elements at positions start through stop - 1 in this list.

Specified by:
getSubList in interface IList<E>
Parameters:
start - index of the first element of the sublist.
stop - stop - 1 is the index of the last element of the sublist.
Returns:
a list with stop - start elements, The elements are from positions start inclusive to stop exclusive in this list.

size

public int size()
Description copied from interface: IList
Return the size of this list. In other words the number of elements in this list.
pre: none
post: return the number of items in this list

Specified by:
size in interface IList<E>
Returns:
the number of items in this list

indexOf

public int indexOf(E item)
Description copied from interface: IList
Find the position of an element in the list.
pre: none
post: return the index of the first element equal to item or -1 if item is not present

Specified by:
indexOf in interface IList<E>
Parameters:
item - the element to search for in the list
Returns:
return the index of the first element equal to item or a -1 if item is not present

indexOf

public int indexOf(E item,
                   int pos)
Description copied from interface: IList
find the position of an element in the list starting at a specified position.
pre: 0 <= pos < size()
post: return the index of the first element equal to item starting at pos or -1 if item is not present from position pos onward

Specified by:
indexOf in interface IList<E>
Parameters:
item - the element to search for in the list
pos - the position in the list to start searching from
Returns:
starting from the specified position return the index of the first element equal to item or a -1 if item is not present between pos and the end of the list

makeEmpty

public void makeEmpty()
Description copied from interface: IList
return the list to an empty state.
pre: none
post: size() = 0

Specified by:
makeEmpty in interface IList<E>

iterator

public Iterator<E> iterator()
Description copied from interface: IList
return an Iterator for this list.
pre: none
post: return an Iterator object for this List

Specified by:
iterator in interface IList<E>
Specified by:
iterator in interface Iterable<E>

removeRange

public void removeRange(int start,
                        int stop)
Description copied from interface: IList
Remove all elements in this list from start inclusive to stop exclusive.
pre: 0 <= start < size(), start <= stop <= size()
post: size() = old size() - (stop - start)

Specified by:
removeRange in interface IList<E>
Parameters:
start - position at beginning of range of elements to be removed
stop - stop - 1 is the position at the end of the range of elements to be removed

addFirst

public void addFirst(E item)
add item to the front of the list.
pre: none
post: size() = old size() + 1, get(0) = item

Parameters:
item - the data to add to the front of this list

addLast

public void addLast(E item)
add item to the end of the list.
pre: none
post: size() = old size() + 1, get(size() -1) = item

Parameters:
item - the data to add to the end of this list

removeFirst

public E removeFirst()
remove and return the first element of this list.
pre: size() > 0
post: size() = old size() - 1

Returns:
the old first element of this list

removeLast

public E removeLast()
remove and return the last element of this list.
pre: size() > 0
post: size() = old size() - 1

Returns:
the old last element of this list

toString

public String toString()
Description copied from interface: IList
Return a String version of this list enclosed in square brackets, []. Elements are in are in order based on position in the list with the first element first. Adjacent elements are seperated by comma's

Specified by:
toString in interface IList<E>
Overrides:
toString in class Object
Returns:
a String representation of this IList

equals

public boolean equals(Object other)
check if this list is equal to another Object.
pre: none

Specified by:
equals in interface IList<E>
Overrides:
equals in class Object
Returns:
true if other is a non null LinkedList object with the same elements as this LinkedList in the same order.