Sams Teach Yourself C in 21 Days

(singke) #1
Pointers: Beyond the Basics 419

15


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
NULL

head

NULL

Before addition

new data
NULL

head

After addition

data
next pointer

data
next pointer
new data
NULL

new data
next pointer

data
NULL

head

Before addition

data
next pointer

data
next pointer

data
NULL

head

After addition

25 448201x-CH15 8/13/02 11:13 AM Page 419

Free download pdf