import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; //Needed for scanner class
/**
* class TestLinkedList -
*/
public class TestLinkedList {
//
public static void main(String[] args) throws FileNotFoundException{
//
IntLinkedList list = getListFrom("project2.txt");
System.out.println( "The original list is:" );
System.out.println();
//displayList( list );
// list = sortList( list );
System.out.println( list.size() );
IntLinkedList list2 = null;
list2.addAfter(2);
System.out.println( list2.size() );
System.out.println();
System.out.println( "The final list is:" );
System.out.println();
//displayList( list );
}//end main
//
//
// getListFrom() method populates the linked list with elements
//
private static IntLinkedList getListFrom(String path) throws FileNotFoundException{
//Get file
File file = new File(path);
Scanner inputFile = new Scanner(file);
IntLinkedList linkedList = null;
//Add elements
while (inputFile.hasNext()){
linkedList.addAfter(Integer.parseInt(inputFile.nextLine()));
}
inputFile.close();
return linkedList;
}//end method getListFrom
//
// displayList() method prints the elements of the linked list
//
// private void displayList( IntLinkedList list ){
// IntLinkedList.printList();
// System.out.println();
// System.out.println(IntLinkedList.size());
// }//end displayList
//
// private static IntLinkedList mergeSortedLists(
// IntLinkedList first,
// IntLinkedList second
// ){
// }//end mergeSortedLists
//
// private static IntLinkedList sortList( IntLinkedList list ){
// }//end sortList
//
}//end class TestIntLInkedList
/**
* class IntLinkedList -
*/
class IntLinkedList{
private int size; //the number of elements in the list
private IntNode head;
private IntNode tail;
private IntNode cursor;
private IntNode precursor;
//
//IntLinkedList
//
public IntLinkedList(){
head=null;
}//end IntLinkedList
//
//compute the number of nodes
//
public int size(){
size = 0;
for ( cursor = head; cursor != null; cursor = cursor.getLink() ){
size++;
}
return size;
}//end size
//
//addAfter
//
public void addAfter( int value ){
if ( cursor == null ) { // either no current element or current element is first
// add element to the head of the list
head = new IntNode( value, head );
cursor = head;
} else { // cursor points to the middle of the list
cursor.setLink( new IntNode( value, cursor.getLink()) );
//cursor = precursor.getLink();
}
if ( size() == 0 ) // the list was empty and tail needs to be initialized
tail = head;
}
//
//Prints list elements
//
public void printList() {
IntNode curr = head;
while (curr != null) {
System.out.print(curr.getData() + " ");
curr = curr.getLink();
}
}
}
/**
* class IntNode -
*/
class IntNode{
//
//Variables
//
private int data; //variable to store an integer value.
private IntNode link; //variable to store an IntNode object.
//
//Non-default constructor to initialize the data fields
//
public IntNode( int initData, IntNode initLink ){
data = initData;
link = initLink;
}
//
//Accessor and mutator methods for data and link fields
//
public int getData(){
return data;
}
public IntNode getLink(){
return link;
}
public void setData(int newData){
data = newData;
}
public void setLink(IntNode newLink){
link = newLink;
}
}//end class IntNode