Class LinkdList

java.lang.Object
  |
  +--LinkdList
Direct Known Subclasses:
SortedList

public class LinkdList
extends Object

LinkdList.java
Illustrates the implementation of a linked list with a null object.
Based on Linked List

Created: Mon Feb 24 21:26:57 2003

Author:
Stuart C. Shapiro
Documentation, in part, from Linked List

Field Summary
protected  boolean empty
           
protected  Object first
           
protected  LinkdList rest
           
 
Constructor Summary
LinkdList()
          Constructs an empty LinkdList.
LinkdList(Object f, LinkdList list)
          Constructs a LinkdList consisting of an Object prepended to another list.
 
Method Summary
 boolean add(Object o)
          Appends the specified element to the end of this list.
 Object get(int index)
          Returns the element at the specified position in this list, the first element having index == 1.
 Object getFirst()
          Returns the first element in this list.
 LinkdList getRest()
          Returns this list omitting the first element.
 LinkdList insert(Object obj, int ndx)
          Returns a LinkdList just like this one, but with obj inserted as the new ndxth element.
 boolean isEmpty()
          Returns true if this list contains no elements
 Object remove(int index)
          Removes the element at the specified position in this list.
 int size()
          Returns the number of elements in this list.
 String toString()
          Returns a string representing this list as its elements, separated by commas, surrounded by parentheses.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

first

protected Object first

rest

protected LinkdList rest

empty

protected boolean empty
Constructor Detail

LinkdList

public LinkdList()
Constructs an empty LinkdList.


LinkdList

public LinkdList(Object f,
                 LinkdList list)
Constructs a LinkdList consisting of an Object prepended to another list.

Parameters:
f - the Object to be the first element of this list.
list - a LinkdList to be the rest of this list.
Method Detail

getFirst

public Object getFirst()
Returns the first element in this list.

Returns:
the first element in this list.
Throws:
NoSuchElementException - if this list is empty.

getRest

public LinkdList getRest()
Returns this list omitting the first element.

Returns:
this list omitting the first element.
Throws:
NoSuchElementException - if this list is empty.

isEmpty

public boolean isEmpty()
Returns true if this list contains no elements

Returns:
true if this list contains no elements

get

public Object get(int index)
           throws IndexOutOfBoundsException
Returns the element at the specified position in this list, the first element having index == 1.

Parameters:
index - index of element to return.
Returns:
the element at the specified position in this list.
Throws:
IndexOutOfBoundsException - if the specified index is is out of range (index < 1 || index > size()).

size

public int size()
Returns the number of elements in this list.

Returns:
the number of elements in this list.

add

public boolean add(Object o)
Appends the specified element to the end of this list.

Parameters:
o - element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).

remove

public Object remove(int index)
              throws IndexOutOfBoundsException
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Parameters:
index - the index of the element to removed.
Returns:
the element previously at the specified position.
Throws:
IndexOutOfBoundsException - if the specified index is out of range (index < 1 || index > size()).

insert

public LinkdList insert(Object obj,
                        int ndx)
Returns a LinkdList just like this one, but with obj inserted as the new ndxth element.

Parameters:
obj - the Object to be inserted.
ndx - the new position for obj.
Returns:
a LinkdList just like this one, but with obj inserted as the new ndxth element.
Throws:
IndexOutOfBoundsException - if ndx < 1 || ndx > this.size()+1.

toString

public String toString()
Returns a string representing this list as its elements, separated by commas, surrounded by parentheses.

Overrides:
toString in class Object
Returns:
a string representing the elements of this list.