java - printing nodes from a singly-linked list -


i made node class linked list class. there way can print out elements in list ? made print() method returns first element 21. how iterate through list ?

public class listnode {     private int item;     private listnode next;      public listnode(int item, listnode next){         this.item = item;         this.next = next;     }      public listnode(int item){         this(item, null);     }      public int print(){         return item;     }      public static void main(string[] args) {                     listnode list = new listnode(21, new listnode(5, new listnode(19, null)));         system.out.println(list.print());     } 

}

public string tostring() {     string result = item + " ";     if (next != null) {         result += next.tostring();     }     return result; } 

and can do

system.out.println(list.tostring()); 

(i renamed function print tostring give more accurate description of does)


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -