Program to create, insert, delete and display operations on singly linked list in java
Linked List Operations: Traverse, Insert and DeleteIn this tutorial, you will learn different operations on a linked list. Also, you will find implementation of linked list operations in C/C++, Python and Java. Show
There are various linked list operations that allow us to perform different actions on linked lists. For example, the insertion operation adds a new element to the linked list. Here's a list of basic linked list operations that we will cover in this article.
Before you learn about linked list operations in detail, make sure to know about Linked List first. Things to Remember about Linked List
In all of the examples, we will assume that the linked list has three nodes 1 --->2 --->3 with node structure as below: struct node { int data; struct node *next; };Menu driven program for all operations on singly linked list in CA Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list is discussed in one menu-driven program. Operations to be performed:
Below is the implementation of the above operations: C
Output: Menu: Insertion at the starting: Insertion at the end: Insertion at specific position: Print the Linked List: Maximum among Linked List: Sorting the Linked List: Reverse the Linked List: Delete the first and last element with choice 5 and 6:
Article Tags :
Data Structures Linked List Technical Scripter
Delete a Linked List Linked Lists Linked-List-Sorting Menu driven programs Technical Scripter 2020 Practice Tags :
Data Structures Linked List
Read Full Article
Singly Linked List Example Program in CSingly Linked List Operations
Singly Linked List Example Program ( Insert, Delete, Display and Count)/* Singly Linked List Example - All Operations Example Program Using Functions in C*/ /* Data Structure Programs,C Linked List Examples */ /* Singly Linked List Example - Insert,Delete,Display and Count Operations*/ #includeSingly Linked List Java ExamplePosted by: Anmol Deep in Core Java May 26th, 2020 1 Comment In this example, we shall discuss how to create a Singly Linked List in Java. We will also go through some live code demonstrating different operations on a Singly Linked List. You can also check this tutorial in the following video: Java LinkedList Tutorial – videoTypes of Linked List and Operation on Linked ListIn the previous blog, we have seen the structure and properties of a Linked List. In this blog, we will discuss the types of a linked list and basic operations that can be performed on a linked list. Types of Linked ListFollowing are the types of linked list
Singly Linked ListA Singly-linked list is a collection of nodes linked together in a sequential way where each node of the singly linked list contains a data field and an address field that contains the reference of the next node. The structure of the node in the Singly Linked List is class Node {
int data // variable to store the data of the node
Node next // variable to store the address of the next node
} The nodes are connected to each other in this form where the value of the next variable of the last node is NULL i.e. next = NULL, which indicates the end of the linked list. Doubly Linked ListA Doubly Linked List contains an extra memory to store the address of the previous node, together with the address of the next node and data which are there in the singly linked list. So, here we are storing the address of the next as well as the previous nodes. The following is the structure of the node in the Doubly Linked List(DLL): class DLLNode {
int val // variable to store the data of the node
DLLNode prev // variable to store the address of the previous node
DLLNode next // variable to store the address of the next node
} The nodes are connected to each other in this form where the first node has prev = NULL and the last node has next = NULL. Advantages over Singly Linked List-
Disadvantages over Singly Linked List-
Circular Linked ListA circular linked list is either a singly or doubly linked list in which there are no NULL values. Here, we can implement the Circular Linked List by making the use of Singly or Doubly Linked List. In the case of a singly linked list, the next of the last node contains the address of the first node and in case of a doubly-linked list, the next of last node contains the address of the first node and prev of the first node contains the address of the last node. Advantages of a Circular linked list
Disadvantages of Circular linked list
Basic Operations on Linked List
We will see the various implementation of these operations on a singly linked list. Following is the structure of the node in a linked list: class Node{ int data // variable containing the data of the node Node next // variable containing the address of next node }Linked List TraversalThe idea here is to step through the list from beginning to end. For example, we may want to print the list or search for a specific node in the list. The algorithm for traversing a list
Linked List node InsertionThere can be three cases that will occur when we are inserting a node in a linked list.
Insertion at the beginning Since there is no need to find the end of the list. If the list is empty, we make the new node as the head of the list. Otherwise, we we have to connect the new node to the current head of the list and make the new node, the head of the list. // function is returning the head of the singly linked-list
Node insertAtBegin(Node head, int val)
{
newNode = new Node(val) // creating new node of linked list
if(head == NULL) // check if linked list is empty
return newNode
else // inserting the node at the beginning
{
newNode.next = head
return newNode
}
} Insertion at end We will traverse the list until we find the last node. Then we insert the new node to the end of the list. Note that we have to consider special cases such as list being empty. In case of a list being empty, we will return the updated head of the linked list because in this case, the inserted node is the first as well as the last node of the linked list. // the function is returning the head of the singly linked list
Node insertAtEnd(Node head, int val)
{
if( head == NULL ) // handing the special case
{
newNode = new Node(val)
head = newNode
return head
}
Node temp = head
// traversing the list to get the last node
while( temp.next != NULL )
{
temp = temp.next
}
newNode = new Node(val)
temp.next = newNode
return head
} Insertion after a given node We are given the reference to a node, and the new node is inserted after the given node. void insertAfter(Node prevNode, int val)
{
newNode = new Node(val)
newNode.next = prevNode.next
prevNode.next = newNode
} NOTE: If the address of the prevNode is not given, then you can traverse to that node by finding the data value. Linked List node DeletionTo delete a node from a linked list, we need to do these steps
In the deletion, there is a special case in which the first node is deleted. In this, we need to update the head of the linked list. // this function will return the head of the linked list
Node deleteLL(Node head, Node del)
{
if(head == del) // if the node to be deleted is the head node
{
return head.next // special case for the first Node
}
Node temp = head
while( temp.next != NULL )
{
if(temp.next == del) // finding the node to be deleted
{
temp.next = temp.next.next
delete del // free the memory of that Node
return head
}
temp = temp.next
}
return head // if no node matches in the Linked List
} Linked List node SearchingTo search any value in the linked list, we can traverse the linked list and compares the value present in the node. bool searchLL(Node head, int val) { Node temp = head // creating a temp variable pointing to the head of the linked list while( temp != NULL) // traversing the list { if( temp.data == val ) return true temp = temp.next } return false }Linked List node UpdationTo update the value of the node, we just need to set the data part to the new value. Below is the implementation in which we had to update the value of the first node in which data is equal to val and we have to set it to newVal. void updateLL(Node head, int val, int newVal) { Node temp = head while(temp != NULL) { if( temp.data == val) { temp.data = newVal return } temp = temp.next } }Suggested Problems to solve in Linked List
Happy coding! Enjoy Algorithms. |