From the course: C Programming: Exploring Advanced Concepts and Practical Applications

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Adding and listing nodes

Adding and listing nodes

- [Instructor] To build a linked list, you start with the first node, the base. This node is referenced by variable portfolio. It's a special case because the variable should not be altered throughout the code, or you lose the linked list in memory. Portfolio is initialized to null here. Item three on the menu is add, which is handled by case three here in the code. If portfolio is null, the first item must be added to the list. The add function returns the address of a new node, which is assigned to portfolio. Otherwise, the else condition, the code must hunt for the end of the list. It starts at the base, the start of the list portfolio. The while loop processes the list, moving to each subsequent node. The final node has null as its next member. When it's reached, the add function is called, and the address returned is saved in the current node's next pointer. Most of the time, the else part of this decision is executed for adding a new node. The add function returns the address of…

Contents