From the course: Master C Language Pointers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Adding structures

Adding structures - C Tutorial

From the course: Master C Language Pointers

Start my 1-month free trial

Adding structures

- [Instructor] To add or insert a new structure into an existing linked list, you update the pointer of the previous structure. First allocate the new structure. Copy the previous structure's next member into the new structure. Then update the previous structure's next member to reference the new structure. This code builds a five structure linked list. It's created in the main function in a for loop down at line 62. After the list is output at line 84, a new structure is inserted after the second structure in the list. The while loop bases its search on the id member of each structure. Pointer current references the second structure when this loop is complete. The new structure is allocated at line 102 by using the temp variable. The structure is filled starting at line 105. For the structure's next member, the current structure's next member pointer is copied. This is the first step to inserting the structure.…

Contents