Q:

Singly linked list Examples in Java

belongs to collection: Java Singly Linked List Programs

0
  • Linked List can be defined as a collection of objects called nodes that are randomly stored in the memory.
  • A node contains two fields, i.e. data stored at that particular address and the pointer which contains the address of the next node in the memory.
  • The last node of the list contains the pointer to the null.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

public class LinkedListExamples  
{  
 Node head;  // head of list  
 static class Node {  
 int data;  
         Node next;  
 Node(int d)  { data = d;  next=null; }  
     }  
     /* This function prints contents of the linked list starting from head */  
 public void display()  
     {  
         Node n = head;  
 while (n != null)  
         {  
 System.out.print(n.data+" \n");  
             n = n.next;  
         }  
     }  
     /* method to create a simple linked list with 3 nodes*/  
 public static void main(String[] args)  
     {  
         /* Start with the empty list. */  
 LinkedListExamples list = new LinkedListExamples();  
  
 list.head       = new Node(100);  
         Node second      = new Node(200);  
         Node third       = new Node(300);  
  
 list.head.next = second; // Link first node with the second node  
 second.next = third; // Link first node with the second node  
 list.display();  
     }  
}  

Output:

100 
 200 
 300 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Program to create and display a singly linked... >>