How to Program for Inserting a Node in a Linked List
In this article, we explored different approaches to insert a node in a linkedlist by using Java programming language.
A linked list is a linear data structure that consists of a sequence of elements called nodes, where each node contains a data element and a reference (or pointer) to the next node in the sequence. The importance of linked lists lies in their flexibility, as nodes can be inserted or removed at any position in the list, unlike arrays which require elements to be shifted over to make space.
Linked lists are also dynamic, meaning they can grow or shrink as needed, unlike fixed-size arrays. Finally, linked lists can be used to implement more complex data structures such as stacks, queues, and graphs.
Here, we will explore how to insert a node in a Linked List.
Let’s start!
To show you some instances
Instance-1
Linked list − Music playlist − Stairway to Heaven, Bohemian Rhapsody, Smells Like Teen Spirit
Add a new node at the end of this linked list.
New linked list − Stairway to Heaven, Bohemian Rhapsody, Smells Like Teen Spirit, Billie Jean
Instance-2
Linked list − Bike names − Harley-Davidson Fat Boy, Honda Gold Wing, Kawasaki Ninja, Yamaha YZF-R1
Add a new node at the 2nd position of this linked list.
New linked list − Harley-Davidson Fat Boy, Ducati Panigale V4, Honda Gold Wing, Kawasaki Ninja, Yamaha YZF-R1
Instance-3
Linked list − Car names − Toyota Camry, Ford Mustang, Honda Civic, Tesla Model S
Add a new node at the beginning of this linked list.
New linked list − Chevrolet Corvette, Toyota Camry, Ford Mustang, Honda Civic, Tesla Model S
Algorithm
Algorithm-1
Step-1 − Define a Node class and a method to insert a new node at the beginning of the linked list.
Step-2 − The user is prompted to enter the number of elements in the linked list and the elements themselves.
Step-3 − A loop iterates over each element entered, inserting it at the beginning of the linked list using the insertAtBeginning() method.
Step-4 − Then prints out the linked list by iterating through it and printing each element.
Step-5 − The program closes the scanner object and terminates.
Algorithm-2
Step-1 − Define a Node class and a method to insert a new node at a given position in the linked list.
Step-2 − The user is prompted to enter the number of elements in the linked list and the elements themselves.
Step-3 − A loop iterates over each element entered, inserting it at the current position using the insertAtPosition() method.
Step-4 − Prompt the user to enter the element and position at which to insert a new node, then calls insertAtPosition() with these parameters.
Step-5 − Print out the updated linked list by iterating through it and printing each element. The scanner object is then closed and the program terminate
Syntax
In a linked list, .next is a reference to the next node in the list.
Below refers to the syntax of it −
new_node.next = head;
Where, ‘new_node’ refers to the object of node class.
Multiple Approaches
We have provided the solution in different approaches.
Inserting node at the beginning of the linked list
Inserting node at a given position in the linked list
Let’s see the program along with its output one by one.
Approach-1: Inserting Node at Beginning of the LinkedList
In this approach, user has to insert the numbers of element of the linked list followed by the elements. Then using our algorithm Insert node elements in the linked list.
Example
import java.util.*; public class Main { static class Node { int data; Node next; Node(int d) { data = d; next = null; } } static Node insertAtBeginning(Node head, int data) { Node new_node = new Node(data); new_node.next = head; head = new_node; return head; } static void printList(Node head) { Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of elements in the linked list: "); int n = sc.nextInt(); Node head = null; for (int i = 0; i < n; i++) { System.out.println("Enter the element " + (i + 1) + " : "); int data = sc.nextInt(); head = insertAtBeginning(head, data); } System.out.println("The linked list is: "); printList(head); sc.close(); } }
Output
Enter the number of elements in the linked list: 5 Enter the element 1 : 1 Enter the element 2 : 3 Enter the element 3 : 5 Enter the element 4 : 4 Enter the element 5 : 6 The linked list is: 6 4 5 3 1
Approach-2: Inserting Node at a Given Position of the LinkedList
In this approach, user has to insert the numbers of element of the linked list followed by the elements and the position to add a new element. Then using our algorithm Insert new node at a given position in the linked list.
Example
import java.util.*; public class Main { static class Node { int data; Node next; Node(int d) { data = d; next = null; } } static Node insertAtPosition(Node head, int data, int position) { Node new_node = new Node(data); if (position == 1) { new_node.next = head; head = new_node; return head; } Node prev = head; for (int i = 1; i < position - 1; i++) { prev = prev.next; } new_node.next = prev.next; prev.next = new_node; return head; } static void printList(Node head) { Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of elements in the linked list: "); int n = sc.nextInt(); Node head = null; for (int i = 0; i < n; i++) { System.out.println("Enter the element " + (i + 1) + " : "); int data = sc.nextInt(); head = insertAtPosition(head, data, i + 1); } System.out.println("The linked list is: "); printList(head); System.out.println("\nEnter the element to insert: "); int data = sc.nextInt(); System.out.println("Enter the position to insert: "); int position = sc.nextInt(); head = insertAtPosition(head, data, position); System.out.println("The linked list after insertion is: "); printList(head); sc.close(); } }
Output
Enter the number of elements in the linked list: 4 Enter the element 1 : 11 Enter the element 2 : 22 Enter the element 3 : 33 Enter the element 4 : 44 The linked list is: 11 22 33 44 Enter the element to insert: 55 Enter the position to insert: 2 The linked list after insertion is: 11 55 22 33 44
Comments
Post a Comment