How to create a linked list in Java
Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked the usage of suggestions as proven beneath. In the Java programming language, Linked List may represent a category and a Node as a separate elegance. The Linked List magnificence includes a reference of Node class kind.
Every element in a connected list is known as a node. It consists of 3 fields-
Prev - Shops and deals with the primary element within the listing. It's miles null for the primary detail.
Next - Store the address of the next part in the list. It's miles null for the closing detail. Facts - Stores the facts.
Algorithm of the linked list in java:
Create a category Node that has attributes –
- Facts and next. Next is a pointer to the next node.
- Create every other class that has attributes: head and tail.
- addNode() will upload a brand new node to the listing:
- Relate a new node.
- It first tests, whether the pinnacle is the same as null which means that the list is empty.
- If the list is empty, both head and tail will point to the newly added node.
- If the listing isn't empty, the brand new node can be added to the quit of the list such that the tail's subsequent will factor to the newly introduced node. This new node becomes the brand new tail of the listing.
Example 1: Here we give an example to create a linked list in Java.
import java.io.*;
public class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public static LinkedList insert (LinkedList list, int data)
{
Node new_node = new Node(data);
if (list.head == null){
list.head = new_node;
}
else
{
Node last = list.head;
While (last.next != null)
{
last = last.next;
}
last.next = new_node;
}
return list;
}
public static void printList (LinkedList list)
{
Node currNode = list.head;
System.out.println (“LinkedList: ”);
while (currNode != null)
{
System.out.println (currNode.data + “ ”);
currNode = currNode.next;
}
}
public static void main (String[] args)
LinkedList list = new LinkedList();
l = insert (l, 1);
l = insert (l, 2);
l= insert (l, 3);
l = insert (l, 4);
l = insert (l, 5);
l = insert (l, 6);
l = insert (l, 7);
l = insert (l, 8);
l = insert (l, 9);
l = insert (l, 10);
printList (l);
}
}
Output: We will compile the program and also run it.
LinkedList: 1 2 3 4 5 6 7 8 9 10
Example 2: Here we give an example to create a linked list in Java.
import java.io.*;
public class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public static LinkedList insert (LinkedList list, int data)
{
Node new_node = new Node(data);
if (list.head == null){
list.head = new_node;
}
else
{
Node last = list.head;
While (last.next != null)
{
last = last.next;
}
last.next = new_node;
}
return list;
}
public static void printList (LinkedList list)
{
Node currNode = list.head;
System.out.println (“LinkedList: ”);
while (currNode != null)
{
System.out.println (currNode.data + “ ”);
currNode = currNode.next;
}
}
public static void main (String[] args)
LinkedList list = new LinkedList();
list = insert (list, 11);
list = insert (list, 12);
list = insert (list, 13);
list = insert (list, 14);
list = insert (list, 15);
printList (list);
}
}
Output: We will compile the program and also run it.
LinkedList: 11 12 13 14 15
Example 2: Here we give an example to create a linked list in Java.
public class SinglyLinkedList {
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
public void addNode(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("The Singly linked list are: ");
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
SinglyLinkedList sList = new SinglyLinkedList();
sList.addNode(111);
sList.addNode(112);
sList.addNode(113);
sList.addNode(114);
sList.addNode(115);
sList.display();
}
}
Output: We will compile the program and also run it.
The Singly linked list are: 111 112 113 114 115