0% found this document useful (0 votes)
191 views

Singly Link List Operations

The document describes algorithms for performing common operations on singly linked lists: 1) Insertion at the beginning, end, and a specific position. Deletion at the beginning, end, and a specific position. 2) Traversal of the entire linked list to print out or search element values. 3) Each algorithm is broken down into sequential steps to modify pointers and data values to achieve the desired operation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views

Singly Link List Operations

The document describes algorithms for performing common operations on singly linked lists: 1) Insertion at the beginning, end, and a specific position. Deletion at the beginning, end, and a specific position. 2) Traversal of the entire linked list to print out or search element values. 3) Each algorithm is broken down into sequential steps to modify pointers and data values to achieve the desired operation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithm to insert in Singly link list At

beginning
o Step 1: IF PTR = NULL

Write OVERFLOW
Go to Step 7
[END OF IF]

o Step 2: SET NEW_NODE = PTR


o Step 3: SET NEW_NODE → DATA = VAL
o Step 4: SET NEW_NODE → NEXT = HEAD
o Step 5: SET HEAD = NEW_NODE
o Step 6: EXIT

Algorithm To insert at end in singly link list


o Step 1: IF PTR = NULL Write OVERFLOW
Go to Step 1
[END OF IF]
o Step 2: SET NEW_NODE = PTR
o Step 3: SET NEW_NODE - > DATA = ITEM
o Step 4: SET NEW_NODE - > NEXT = NULL
o Step 5: SET TEMP = HEAD
o Step 6: Repeat Step 8 while TEMP - > NEXT != NULL
o Step 7: SET TEMP = TEMP - > NEXT
[END OF LOOP]
o Step 8: SET PTR - > NEXT = NEW_NODE
o Step 9: EXIT

Algorrithm to insert at specific position in


Singly Link list
o STEP 1: IF PTR = NULL

WRITE OVERFLOW
GOTO STEP 12
END OF IF

o STEP 2: SET NEW_NODE = PTR


o STEP 3: NEW_NODE → DATA = VAL
o STEP 4:SET NEW_NODE->NEXT=NULL
o STEP 4: SET TEMP = HEAD
o STEP 5: SET I = 0
o STEP 6: REPEAT STEP 7 UNTIL I<pos-1
o STEP 7: TEMP = TEMP → NEXT
o STEP 8:I++
END OF LOOP
o STEP 9: NEW_NODE → NEXT = TEMP → NEXT
o STEP 10: TEMP_NEXT = NEW_NODE
o STEP 11: EXIT

ALgorithm to delete at beg

o Step 1: IF HEAD = NULL


Write UNDERFLOW
Go to Step 5
[END OF IF]

o Step 2: SETTEMP = HEAD


o Step 3: SET HEAD = HEAD -> NEXT
o Step 4: FREE TEMP
o Step 5: EXIT
o

Algorithm to del at end


o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET TEMP = HEAD


o Step 3: Repeat Steps 4 and 5 while TEMP -> NEXT!=
NULL
o Step 4: SET PREV_NODE=TEMP
o Step 5: SET TEMP=TEMP -> NEXT

[END OF LOOP]

o Step 6: SET PREV_NODE-> NEXT = NULL


o Step 7: FREE TEMP
o Step 8: EXIT

Algorithm to end at specific position


o STEP 1: IF HEAD = NULL

WRITE UNDERFLOW
GOTO STEP 10
END OF IF
o STEP 2: SET TEMP = HEAD
o STEP 3: SET I = 1
o STEP 4: REPEAT STEP 5 TO 6 UNTIL I<pos-1
o STEP 5: TEMP = TEMP → NEXT
o STEP 6 :I++
o End Loop
o STEP 9: NEXT_NODE=TEMP_>NEXT
o STEP 10:TEMP->NEXT=NEXT_NODE->NEXT
o STEP 10: FREE (NEXT_NODE)
o STEP 11: EXIT

Algorithm to traverse Singly Link List


o STEP 1: SET TEMP = HEAD
o STEP 2: IF TEMP = NULL

WRITE "EMPTY LIST"


GOTO STEP 7
END OF IF

o STEP 4: REPEAT STEP 5 AND 6 UNTILTEMP != NULL


o STEP 5: PRINT TEMP→ DATA
o STEP 6: TEMP = TEMP → NEXT

[END OF LOOP]

o STEP 7: EXIT

Algorithm to search element in singly link


list
o Step 1: SET TEMP = HEAD
o Step 2: Set I = 1
o STEP 3: IF HEAD = NULL

WRITE "EMPTY LIST"


GOTO STEP 8
END OF IF

o STEP 4: REPEAT STEP 5 TO 7 UNTIL TEMP != NULL


o STEP 5: if TEMP → data = item

write i+1
End of IF

o STEP 6: I = I + 1
o STEP 7: TEMP =TEMP → NEXT

[END OF LOOP]

o STEP 8: EXIT

You might also like