1.

Write down an algorithm for adding a node to a linked list sorted in ascending order(maintaining the sorting property).

Answer»

An algorithm for adding a node to a link list sorted in ASCENDING order (maintaining the sorting property) is given below:

  • Step 1: Check if the linked list has no value (or is empty). If yes, then set the new node as the HEAD and return it.
  • Step 2: Check if the value of the node to be INSERTED is smaller than the value of the head node. If yes, place it at the beginning and make it the head node.
  • Step 3: Find the suitable node after which the input node should be added in a loop. To discover the required node, BEGIN at the head and work your way forward until you reach a node whose value exceeds the input node. The preceding node is the correct node.
  • Step 4: After the correct node is found in step 3, insert the node.


Discussion

No Comment Found