LInkList-code

//******************************************************************* // Java compiler created in PHP to quickly and safely test code. // NOTE: please read the 'More Info' tab to the right for shortcuts. //******************************************************************* import java.lang.Math; public class Run { public static void main(String[] args) { System.out.println("creating linklist for below 5 element"); int[] arr = {1,2,4,5,6}; Node head=null; Node dummy1 = head; for( int i=0;i<arr.length;i++) { Node myNewNode = getNewNode(arr[i]); if(head == null) { System.out.println("assigning value to head"); head = myNewNode; } else { dummy1=head; while(dummy1.nextref !=null) { dummy1 = dummy1.nextref; } dummy1.nextref = myNewNode; } } //printting the entire linklist System.out.println("Linklist : "); dummy1=head; while(dummy1.nextref !=null) { System.out.println("-->"+dummy1.data); dummy1 = dummy1.nextref; } } public static Node getNewNode(int data) { Node newNode = new Node(); newNode.data = data; newNode.nextref = null; System.out.println("New node created.."); return newNode; } } public class Node { int data; Node nextref; public int getData() { return data; } public Node getNext() { return nextref; } public void setData(int data) { this.data= data; } public void setNext(Node nextref) { this.nextref = nextref; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.