package newpackage;
import java.util.*;
public class Chain implements LinearList
{
// data members
protected ChainNode firstNode;
protected int size;
private final int initialCapacity;
// constructors
/** create a list that is empty */
public Chain(int initialCapacity)
{
this.initialCapacity = initialCapacity;
// the default initial values of firstNode and size
// are null and 0, respectively
}
public Chain()
{this(0);}
// methods
/** @return true iff list is empty */
public boolean isEmpty()
{return size == 0;}
/** @return current number of elements in list */
public int size()
{return size;}
/** @throws IndexOutOfBoundsException when
* index is not between 0 and size - 1 */
void checkIndex(int index)
{
if (index = size)
throw new IndexOutOfBoundsException
("index = " + index + " size = " + size);
}
/** @return element with specified index
* @throws IndexOutOfBoundsException when
* index is not between 0 and size - 1 */
public Object get(int index)
{
checkIndex(index);
// move to desired node
ChainNode currentNode = firstNode;
for (int i = 0; i size)
// invalid list position
throw new IndexOutOfBoundsException
("index = " + index + " size = " + size);
if (index == 0)
// insert at front
firstNode = new ChainNode(theElement, firstNode);
else
{ // find predecessor of new element
ChainNode p = firstNode;
for (int i = 0; i 0)
s.delete(s.length() - 2, s.length()); // remove last ", "
s.append("]");
// create equivalent String
return new String(s);
}
/** create and return an iterator */
public Iterator iterator()
{return new ChainIterator();}
/** chain iterator */
private class ChainIterator implements Iterator
{
// data member
private ChainNode nextNode;
// constructor
public ChainIterator()
{nextNode = firstNode;}
// methods
/** @return true iff list has a next element */
public boolean hasNext()
{return nextNode != null;}
/** @return next element in list
* @throws NoSuchElementException
* when there is no next element */
public Object next()
{
if (nextNode != null)
{
Object elementToReturn = nextNode.element;
nextNode = nextNode.next;
return elementToReturn;
}
else
throw new NoSuchElementException("No next element");
}
/** unsupported method */
public void remove()
{
throw new UnsupportedOperationException
("remove not supported");
}
}
public int setSize(){
ChainNode currentNode = firstNode;
for (int i=0;i
currentNode =currentNode;
}
return size;
}
public Object set(int theIndex, Object theElement)
{
checkIndex(theIndex);
ChainNode currentNode = firstNode;
for (int i = 0; i < theIndex; i++)
currentNode = currentNode.next;
Object elementToReturn = currentNode.element;
currentNode.element = theElement;
return elementToReturn;
}
/** test program */
public static void main(String [] args)
{
// test default constructor
Chain x = new Chain();
// test size
System.out.println("Initial size is " + x.size());
// test isEmpty
if (x.isEmpty())
System.out.println("The list is empty");
else System.out.println("The list is not empty");
// test put
x.add(0, new Integer(2));
x.add(1, new Integer(6));
x.add(0, new Integer(1));
x.add(2, new Integer(4));
x.set(2,9);
x.set(3,11);
// // test toString
// System.out.println("The list is " + x);
// x.set(0, 5);
// System.out.println("the list is "+ x.set(0, 5));
//
// // test indexOf
// int index = x.indexOf(new Integer(4));
// if (index < 0)
// System.out.println("4 not found");
// else System.out.println("The index of 4 is " + index);
//
// index = x.indexOf(new Integer(3));
// if (index < 0)
// System.out.println("3 not found");
// else System.out.println("The index of 3 is " + index);
//
// // test get
// System.out.println("Element at 0 is " + x.get(0));
// System.out.println("Element at 3 is " + x.get(3));
//
// // test remove
// System.out.println(x.remove(1) + " removed");
// System.out.println("The list is " + x);
// System.out.println(x.remove(2) + " removed");
// System.out.println("The list is " + x);
//
// if (x.isEmpty())
// System.out.println("The list is empty");
// else System.out.println("The list is not empty");
//
// System.out.println("List size is " + x.size());
//
//
//
// output using an iterator
Iterator y = x.iterator();
System.out.print("The list is ");
while (y.hasNext())
System.out.print(y.next() + " ");
System.out.println();
}
}