Tuesday, 7 February 2017

Singly Linked List in java for beginners

Linked List is a dynamic data structure which is used to store data in the form of a list. Linked List is very common and is used in maintaining data storage and management more convenient.

Linked List are of many type such as

Singly Linked List
Doubly Linked List

Here we would be discussing about the most basic Linked List which is singly linked list.

import java.io.*;
public class SingleLinkedList
{
public LinkedListNode head;
public static class LinkedListNode
{
private int data;
private ListNode next;
public LinkedListNode (int data)
{
this.data =data;
this.next = null;
}
}
public static void main(String a[])
{
LinkedListNode head = new LinkedListNode(10);

LinkedListNode second = new LinkedListNode(20);

LinkedListNode last = new LinkedListNode(30);

head.next = second;
second.next = last;

}
}

The above code is represents a Linked List which consist of three node, the first node is the head node.

Here we have made a custom data structure node which consist of a data and the address of next node.

We have made a class LinkedListNode which consist of two properties first one is data and another one is the address of another node of its own type.

We have made three nodes with different data values and then we have arranged accordingly to the address of one another.

No comments:

Post a Comment