Singly LL Algo Insert Search Print

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SSS

DATA STRUCTURES
- Algorithms(Insert/Search/Delete) for SINGLY LINKED LISTS
Algorithm 1 : Write an algorithm to insert an element at the end of a Singly
linked list
Insert (value)
Step 1: Start
Step 2: Set PTR = addressof (New Node)
Step 3: Set PTR->INFO = value;
Step 4: If FIRST = NULL, then goto Step 5 else goto Step 7
Step 5: Set FIRST=PTR and LAST=PTR
Step 6: Set PTR->NEXT = NULL and goto Step 8
Step 7: Set LAST->NEXT=PTR, PTR->NEXT=NULL and
LAST=PTR
Step 8: Stop
Explanation of insert(value) function :
Step 2 -> Allocates a new node and assign its address to the pointer PTR
Step 3 -> Stores the element value to be inserted in the INFO part of the
new node
Step 4 -> Checks whether the existing list is empty
If existing list is empty, Step 5 and Step 6 to be executed and
goes to end (Step 8)
Step 5 -> Updates the FIRST and LAST pointers as the new
node
Step 6 -> Sets the new node to point to NULL as it is the last
node
Step 8 -> End of the Process
If existing list is not empty, Step 7 to be executed
Step 7 -> Link the newly created node at the end of the list
Step 8 -> End of Process

Algorithm 2: Write an algorithm to search a specific element in the linked list.


Search (value)
Step 1: Start
Step 2: If FIRST=NULL goto Step 3 else goto Step 4
Step 3: Return (“Search unsuccessful: Element not present”)
and Stop
Step 4: Set PTR=FIRST
Step 5: Repeat Steps 6-8 until PTR!=NULL
Step 6: If PTR->INFO=value goto Step 7 else goto Step 8
Step 7: Return (“Search successful”, PTR) and Stop
Step 8: Set PTR=PTR->NEXT
Step 9: Return (“Search unsuccessful: Element not present”)
Step 10: Stop

Explanation of Search(value) function :


Step 2 -> Checks if the linked list is empty
If the linked list is empty,
Step 3 -> Return a relevant message
Stop
If the linked list is not empty,
Search from the first node to last node, and compare the
value to be searched with each node.
Step 4 -> Start searching from FIRST node
Step 5 -> Repeat Steps 6-8 until PTR is not equal to NULL
Step 6 -> Check if the current node value is equal to the
search value,
If the current node value and search value are same,
Step 7 -> Print relevant message and STOP
else
Step 8 -> Move to the next node and continue
search. Go to Step 6
Step 9 -> this step will be executed when PTR becomes
NULL which means the Search value is not found in the Linked List. Print
the relevant message and STOP

Algorithm 3: Write an algorithm to print all the linked list elements


Print ()
Step 1: Start
Step 2: If FIRST=NULL goto Step 3 else goto Step 4
Step 3: Display (“Empty List”) and Stop
Step 4: Set PTR=FIRST
Step 5: Repeat Steps 6-7 until PTR!=NULL
Step 6: Display (PTR->INFO)
Step 7: Set PTR=PTR->NEXT
Step 8: Stop
Explanation of print() function :
Step 2 -> Check if the linked list is empty
If the linked list is empty,
Step 3 -> Return a relevant message
Stop
If the linked list is not empty,
Traverse through each node and display the INFO
Step 4 -> Start searching from FIRST node
Step 5 -> Repeat Steps 6-7 until PTR is not equal to
NULL
Step 6 -> Displays list elements
Step 7 -> Move to the next node
Step 8 -> Stops after displaying all node values

You might also like