I have a quick question regarding a linked list data structure in C. So the node contains a balance field, a name field and a next pointer to the next node. I want to keep the linked list sorted by the balance (i.e lowest first). If I update the balance of a node, I need to keep moving it to the right to get a sorted list. Any ideas on how to go about this? I'm stuck.
You read the first node and compare the balance with the thing you want to add. If the balance of the node is lower, you read the next node (using the pointer to the next node). Repeat these steps until the balance you want to add is lower than the balance of the node. Finally, add the node in the linked list. If you get stuck with linked lists, try drawing a diagram. They can really help. |dw:1361287829049:dw|
Join our real-time social learning platform and learn together with your friends!