Class LinkList

java.lang.Object
  |
  +--LinkList

public class LinkList
extends Object

LinkList.java
Illustrates the implementation of a linked list.
Based on Linked List

Created: Thu Feb 20 20:14:03 2003

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

Constructor Summary
LinkList(Object f)
          Creates a new LinkList containing one Object.
LinkList(Object f, LinkList list)
          Creates a new LinkList 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.
 LinkList getRest()
          Returns this list omitting the first element.
 LinkList insert(Object obj, int ndx)
          Returns a LinkList just like this one, but with obj inserted as the new ndxth element.
 Object remove(int index)
          Removes the element at the specified position (must be between 2 and the size of this list, inclusive) 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
 

Constructor Detail

LinkList

public LinkList(Object f)
Creates a new LinkList containing one Object.

Parameters:
f - the Object to be put into this list.

LinkList

public LinkList(Object f,
                LinkList list)
Creates a new LinkList consisting of an Object prepended to another list.

Parameters:
f - the Object to be the first element of this list.
list - a LinkList 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.

getRest

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

Returns:
this list omitting the first element.

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 (must be between 2 and the size of this list, inclusive) 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 < 2 || index > size()).

insert

public LinkList insert(Object obj,
                       int ndx)
Returns a LinkList 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 LinkList 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.