0
29kviews
Write algorithm to insert a new node in linked list at the beginning, and at the end.

PROCEDURE (insert-node-as-a-first-node) ( list, link, info,new, item)

//List = given single link list //Link = link part of the node //info = information part of the node //new = newly created node //item = item to be inserted at first position of a linked list

Step 1[checking for overflow] If (new==NULL) a. print overflow b. Goto step 4 [End of if statement]

Step 2 new -> info =item New->link = NULL Step 3 New->link=start Start= new Step 4 Exit


1 Answer
1
2.6kviews
// algorithm to insert new node at the beginning
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL NEXT
Step 4: SET DATA = VAL
Step 5: SET NEW_NODE NEXT = START
Step 6: SET START = NEW_NODE
Step 7: EXIT

// algorithm to insert new node at the end

Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT != NULL
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: EXIT
Please log in to add an answer.