hi, i'm a student(BSIT) and i'm having a hard time figuring out how to make my code work...
about my code: it is called MyLinkedList.java. Unfinished because i got stuck with the loops!
As obvious as its name implies, this code implements linked list. The catch is, is that the input will be coming from a textfile. I used bufferedReader to read the whole file and StringTokenizer to read the individual lines in the files...
the code:
import java.io.*;
import java.util.*;
class Item
{
public Item( Object d, Item n )
{
data = d;
next = n;
}
public Object data;
public Item next;
}
class MyLinkedList
{
private Item head;
private Item tail;
//head is the first element in the list.
//tail is the last element in the list.
public MyLinkedList()
{
head = null;
tail = null;
}
public void addTail( Object data )
{
Item newTail = new Item( data, null );
if( head == null )
{
head = newTail;
tail = head;
}
else
{
tail.next = newTail;
tail = newTail;
}
}
public void addHead( Object data )
{
Item newHead = new Item( data, head );
if( head != null )
head = newHead;
else
{
head = newHead;
tail = head;
}
}
public void add( Object data, int index )
{
if( ( index == 0 ) || ( head == null ) )
addHead( data );
else
{
Item current = head;
while( ( current != null ) && ( index > 1 ) )
{
current = current.next;
index--;
}
if( current != null )
{
current.next = new Item( data, current.next );
if( current == tail )
tail = current.next;
}
}
}
public Object removeHead()
{
if( head == null )
return null;
Object data = head.data;
head = head.next;
return data;
}
public int find( Object data )
{
if( head == null )
return -1;
int index = 0;
Item current = head;
while( ( current != null ) && ( !current.data.equals( data ) ) )
{
index++;
current = current.next;
}
if( current == null )
return -1;
else
return index;
}
public Object remove( int index )
{
if( index == 0 )
return removeHead();
if( head == null )
return null;
Item current = head, previous = null;
while( ( current != null ) && ( index > 0 ) )
{
previous = current;
current = current.next;
index--;
}
if( current == null )
return null;
Object data = current.data;
previous.next = current.next;
return data;
}
public String toString()
{
if( head == null )
return "<empty>";
Item current = head;
String res = "{";
while( current != null ){
res = res + current.data;
if( current.next != null )
res += ",";
current = current.next;
}
res += "}";
return res;
}
public static void main( String args[] )
{
System.out.println("Enter file name:");
String fileName = SavitchIn.readLine();
try
{
BufferedReader inputStream =
new BufferedReader(new FileReader(fileName));
String line;
boolean done = false;
while(!done)// to get to the next line
{
line = inputStream.readLine();
StringTokenizer inputReader = new StringTokenizer(line);
//System.out.println(inputReader);
while(inputReader.hasMoreElements())//figures out every element in the line.
{
if (inputReader.nextElement().equals("d"))
{
Object x = inputReader.nextElement();
System.out.println("x = " + x);
System.out.println("element x is removed");
}
else if(inputReader.nextElement().equals("i"))
System.out.println("the element after the next is inserted");
else if(inputReader.nextElement().equals("ie"))
System.out.println("the next element is inserted at the end of the list");
else
{
boolean finish = true;
finish = inputReader.hasMoreElements();
}
line = inputStream.readLine();
}
done = true;
}
inputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println(fileName + " not found!");
}
catch(IOException e)
{
System.out.println("Error reading" + fileName);
}
}
}
example input from a text file:
d 10 //remove 10
ie 2 //insert 2
the output:
Enter file name:
sample.txt
x = 10
element x is removed.
my problem:
my first while loop seemed to be incorrect, suppose to be, it would switch to every line but it wont switch, my second while loop worked though...as i said earlier, i got stuck with the loops that i can't complete my main...so can some genius help me please!
my life is at stake...