Pointers: Beyond the Basics 41915
Figure 15.9 illustrates the procedure for adding a new element to an empty list, and
Figure 15.10 illustrates adding a new first element to an existing list.It’s important to switch the pointers in the correct order. If you reassign the
Caution head pointer first, you will lose the list!FIGURE15.9
Adding a new element
to an empty linked list.FIGURE15.10
Adding a new first ele-
ment to an existing list.new data
NULLheadNULLBefore additionnew data
NULLheadAfter additiondata
next pointerdata
next pointer
new data
NULLnew data
next pointerdata
NULLheadBefore additiondata
next pointerdata
next pointerdata
NULLheadAfter addition25 448201x-CH15 8/13/02 11:13 AM Page 419