Home » Data Structure & Algorithms » insert , Search and delete Operation in LinkedList

insert , Search and delete Operation in LinkedList

 

Question :

WAP to do insert , Search and delete Operation in LinkedList

Algorithm :

Iterative Solution :

newNode(int data)
          Step 1 : Create an object of LinkedListNode class by passing data as argument.
          Step 2 : Return the object.

insert(int data)
          Step 1 : Make current node as head node.
          Step 2 : Call newNode(int data) method and assign the returned node as the next node of current node.

search(int data)
          Step 1 : If head is null,then return false.
          Step 2 : Traverse each node of the Linked List.If we find and node’s data equal to the element to be                             searched,then return true.

display()
            Step 1 : If head is null,then print negative message.
            Step 2 : 
Else , traverse each node of the Linked List.Print each element till we reach the end of Linked list.

delete()
          Step 1 : Store head of the Linked List in node current.
          Step 2 : If head node itself holds the data to be deleted,then make head as next of current.
          Step 3 : Search for the data to be deleted by traversing each element of the LinkedList.
          Step 4 : If data not found in linked list,then return.
          Step 5 : Remove the current node from linked list

 

Program :

OutPut :

 
 


Leave a comment

Your email address will not be published. Required fields are marked *

3 × 2 =