I wrote a program to implement LinkedList :
package JavaApplication;
import JavaApplication.MyExciption;
public class LinkedList<ListElementType>
{
class Node
{
ListElementType elem;
Node next;
}
private Node head;
private Node tail;
private Node current;
public LinkedList()
{
head=null;
tail=null;
current=null;
}
public void insert(ListElementType elem)throws MyExciption
{
Node addedNode=new Node();
if(addedNode==null)
throw new MyExciption("An ERROR happened : There is no enough memory to add new element.");
addedNode.elem=elem;
if(head==null)
head=addedNode;
else
tail.next=addedNode;
tail=addedNode;
addedNode.next=null;
}
public boolean first(ListElementType elem)
{
if(head==null)
return false;
elem=head.elem;
current=head;
return true;
}
public boolean next(ListElementType elem)throws MyExciption
{
if(current==null)
throw new MyExciption("An ERROR happened : The list is Empty.");
if(current.next==null)
return false;
current=current.next;
elem=current.elem;
return true;
}
}
and then I wrote a main program to store char items and then displaying them again on console:
package Example;
import java.util.Scanner;
import org.omg.CORBA.CharHolder;
import JavaApplication.LinkedList;
import JavaApplication.MyExciption;
import static java.lang.System.*;
public class LinkedListTest
{
public static void main(String[]args) throws MyExciption
{
LinkedList<CharHolder> L=new LinkedList<CharHolder>();
CharHolder elem =new CharHolder();
out.println("Enter items to add to add to List, add q to stop");
Scanner read=new Scanner(in);
boolean iterator=true;
do
{
String s=read.next();
char[]a=s.toCharArray();
for(int j=0;j<a.length;j++)
{
if(a[j]!='q')
{
CharHolder e=new CharHolder();
e.value=a[j];
L.insert(e);
}
else
{
iterator=false;
break;
}
}
}while(iterator);
out.println("Here are the items in the list : ");
boolean notEmpty=L.first(elem);
while(notEmpty)
{
out.print(elem.value+" ");
notEmpty=L.next(elem);
}
}
}
After running the results is:
Enter items to add to add to List, add q to stop
ytrewq
Here are the items in the list :
As you can see, the result is blanks...
please, need help.