LinkedList i Java
Er der nogen der kan hjælpe mig med LinkedList. Jeg kan ikke få den til at køre correct. Den bliver ved med at læse kun sidste element og ikke mere selvom der er 7 elementer på listen.Koden ser sådan ud:
------------------------------
class LinkedList { // LinkedList ADT + Dummy Header Node.
public int nodes=0;
private LLNode head, current, next;
// Constructor.
public LinkedList() {
head = new LLNode(\"Route\");
current = head;
}
// Accessors.
public boolean isEmpty() {
return head.next==null;
}
public boolean isLast(){
return current.next==null;
}
public Object retrieve() {
current.element = head.next.element;
return current.element;
}
public Object retrieveNext() {
return current.next.element;
}
public Object readNext(){
return current.next;
}
public boolean hasNext(){
current = head.next;
if (current.next != null)
return false;
else
return true;
}
/*public void first() throws ItemNotFound {
current=head.next; if (current==null) throw new ItemNotFound(\"LinkedList: No first element.\");
}*/
// Mutators.
public void insertNext(Object element) {
// Inserts an element into the next position but does NOT advance the list.
//if (current != null) {
current.next = new LLNode(element, current.next = head.next);
nodes++;
// }
// else System.out.println(\"LinkedList: Unable to insert new node.\");
}
public void deleteNext() {
if ((current!=null) && (current.next!=null)) {
current.next = current.next;
nodes--;
}
else System.out.println(\"LinkedList: Unable to delete next node.\");
}
}
class LLNode {
public Object element;
public LLNode next;
// Constructors.
public LLNode(Object s) {element=s; next=null;}
public LLNode(Object s, LLNode n) {element=s; next=n;}
}
public class LinkedListTesterNode {
public static void main(String args[]) {
LinkedList route1 = new LinkedList();
route1.insertNext(\"1\");
route1.insertNext(\"2\");
route1.insertNext(\"3\");
route1.insertNext(\"4\");
route1.insertNext(\"5\");
route1.insertNext(\"6\");
route1.insertNext(\"7\");
System.out.println(\"There are \"+route1.nodes+\" Segments in Route 1\");
//if (route1.hasNext()){
System.out.println(\"Results are \" + route1.retrieve());
System.out.println(\"Results are \" + route1.retrieveNext());
System.out.println(\"Results are \" + route1.retrieveNext());
System.out.println(\"Results are \" + route1.retrieveNext());
// }//end if
}
}
----------------------------------------
håber at få svar...
tak