Professional Documents
Culture Documents
Singly LL Algo Insert Search Print
Singly LL Algo Insert Search Print
Singly LL Algo Insert Search Print
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