Tuesday, May 3, 2022

Question 47 : Implement singly linked list in java

You need to implement singly linked list data structures. You need to write a simple program to demonstrate insert, and delete operations.


In this post, we will see how to implement a singly linked list in java.


It is one of the most used data structures. In a singly linked list, Node has data and a pointer to the next node. It does not have a pointer to the previous node.

The last node‘s next points to null, so you can iterate over the linked list by using this condition.


Node for the linked list can be presented as below:

class Node { public int data; public Node next; public void displayNodeData() { System.out.println("{ " + data + " } "); } }

An example of a linked list:



Let’s implement Linked List in java.


Create a java file named SinglyLinkedList.java.

class Node { public int data; public Node next; public void displayNodeData() { System.out.println("{ " + data + " } "); } } public class SinglyLinkedList { private Node head; public boolean isEmpty() { return (head == null); } // used to insert a node at the start of linked list public void insertFirst(int data) { Node newNode = new Node(); newNode.data = data; newNode.next = head; head = newNode; } // used to delete node from start of linked list public Node deleteFirst() { Node temp = head; head = head.next; return temp; } // Use to delete node after particular node public void deleteAfter(Node after) { Node temp = head; while (temp.next != null && temp.data != after.data) { temp = temp.next; } if (temp.next != null) temp.next = temp.next.next; } // used to insert a node at the start of linked list public void insertLast(int data) { Node current = head; while (current.next != null) { current = current.next; // we'll loop until current.next is null } Node newNode = new Node(); newNode.data = data; current.next = newNode; } // For printing Linked List public void printLinkedList() { System.out.println("Printing LinkedList (head --> last) "); Node current = head; while (current != null) { current.displayNodeData(); current = current.next; } System.out.println(); } }

Let's create a Main class named LinkedListMain.java to create LinkedList.


public class LinkedListMain {
public static void main(String args[])
{
SinglyLinkedList myLinkedlist = new SinglyLinkedList();
myLinkedlist.insertFirst(5);
myLinkedlist.insertFirst(6);
myLinkedlist.insertFirst(7);
myLinkedlist.insertFirst(1);
myLinkedlist.insertLast(2);
// Linked list will be
// 2 -> 1 -> 7 -> 6 -> 5
Node node=new Node();
node.data=1;
myLinkedlist.deleteAfter(node);
// After deleting node after 1,Linked list will be
// 2 -> 1 -> 6 -> 5
myLinkedlist.printLinkedList();
}
}


When you run the above program, you will get the below output:
Printing LinkedList (head --> last) { 1 } { 6 } { 5 } { 2 }

Don't miss the next article! 
Be the first to be notified when a new article or Kubernetes experiment is published.                            

 

 Share This

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS