Saturday 10 February 2018

Traversing each node of list and print link list data in java

Traversing each node of list and print link list data in java,create a linkedlist and print the data in java,create a linkedlist and print the data,

Traversing each node of linkedlist: 
     
                    In tutorial we are going to create a linkedlist and print the data  of linkedlist. or we can also say "Traversing each node of list and print link list data in java".


package com.dashzin.linklist;
public class TraverseList {
     Node head;
    
     static class Node{
          int data;
          Node next;
     Node(int data){
          this.data=data;
          next=null;
     }
     }
 /* This method will prints data of linked list starting from head */
     public void printList(){
          while(head!=null){
              System.out.print(head.data+" ");
              head=head.next;
          }
     }
    
     public static void main(String args[]){
         
          // creating four node for linklist
          TraverseList lkl=new TraverseList();
          lkl.head=new Node(1);
         
          Node first=new Node(2);
          Node sec=new Node(3);
          Node third=new Node(4);
         
          //Link all the node with each other.....
         
          lkl.head.next=first;
          first.next=sec;
          sec.next=third;
         
          //calling method to print_list.....
          lkl.printList();
         
     }
}



If you are not getting how it has done then please just see the previous post of this topic... 





DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment