CJCoding With Joseph
15free / daySign inCreate account

Carleton University - SYSC 2006

insert_node_sorted()

Assignment 2 - Spring 2025

The goal of insert_node_sorted() is to take a new node and insert it into an existing linked list of nodes in alphabetical order based on the node name.

For example, if your list already has:

nodeA -> nodeC -> nodeD

and you insert nodeB, the result will be:

nodeA -> nodeB -> nodeC -> nodeD


How do we do this?

To insert a node alphabetically, we need to:

  1. Compare Node Names:

    • Use strcmp() to compare the new node’s name with existing nodes.

  2. Insertion Scenarios:

    • Empty List: If the list is empty, insert the new node at the head.

    • Insert at Head: If the new node's name comes before the current head, insert it at the beginning.

    • Insert in Middle or End: Walk through the list and find the correct position for the new node.

Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 25 more lessons in Carleton University - SYSC 2006.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.