/**
* Write a description of class IntSLList here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class IntSLList {
private IntNode head, tail;
public IntSLList(){
head = tail = null;
}
public void addToTail(int number) {
if ( head == null) {
head = tail = new IntNode(number);
}
else {
tail.next = new IntNode(number);
tail = tail.next;
}
}
public void addToHead(int number) {
head = new IntNode(number,head);
if (tail == null)
tail = head;
}
public int length()
{
int length = 1;
if( head == null )
{
return 0;
}
IntNode temp;
for( temp = head.next; temp != null; temp = temp.next )
{
length += 1;
}
return length;
}
public void addNode( int number , int place )
{
if( length() < place )
{
addToTail( number );
}
else
{
IntNode temp = head;
for( int i = 1; i < place; i++)
{
temp = temp.next;
}
temp = new IntNode(number,temp);
}
}
public void print()
{
IntNode temp = head;
for( int i = 0; i < length(); i++ )
{
System.out.print(temp.info + ", ");
temp = temp.next;
}
}
}
///////////////////////////////////////////////////////////////////////
public class IntNode {
public int info;
public IntNode next;
public IntNode (int i) { // constructor 1
this(i,null);
}
public IntNode (int i, IntNode n) { // constructor 2
info = i;
next = n;
}
}
///////////////////////////////////////////////////////////////////////