Singly Linked List With Java Program.
What is Linked List ? : When we want to work with an unknown number of data values, we use a linked list data structure to organize that data. The linked list is a linear data structure that contains a sequence of elements such that each element links to its next element in the sequence. Each element in a linked list is called “Node”.
What is a Singly Linked List ?: A singly Linked List is one in which all the Nodes are linked together in some sequential manner.
- First Node is accessed by External Pointer (Head).
- Last Node contains Null Value.
Example :
Here Address is not in a Contigeus manner, Like Array.
Every Linked List have 5 property.
1 Head: Head is Pointer pointing to the first node.
2 Link: Link is the Address part of the Node.
3 Avail: Avail List use to creating a new node. If I Explain in real-life scenarios Avail is a “ Dukan” where we buy a node according to our needs for creating a node. that’s all.
4 Data: Every Node has Data. Data may be Int or String it depends on us.
5 Item: item is data that we want to insert into our Node.
Insertion at First In Linked List.
package com.company.SinglyLinklist;
import java.util.Scanner;
public class Linkedlist {
static Node head;
static Scanner scanner= new Scanner(System.in);
int counter=0;
// insertion at First node
static void InsertionatFirst(int data)
{
Node newnode= new Node(data);
if(head==null)
{
return;
}
else
{
newnode.next=head;
head=newnode;
} }
public static void main(String args[])
{
Linkedlist linkedlist= new Linkedlist();
System.out.print("Enter The number of node");
int elment= scanner.nextInt();
for(int i=0;i<elment;i++)
{
System.out.print("Enter The data of the Node");
int value= scanner.nextInt();
linkedlist.InsertionatFirst(value);
}
}
}
class Node{
int data; // this is the data of the node
Node next; // This is the next part of the node
Node(int data)
{
this.data=data;
next=null;
}
}
Next Part Coming Soon. Follow me for more updates.
Written By Kalka Prasad