Sams Teach Yourself C in 21 Days

(singke) #1
Deleting from a Linked List
The capability to add information to a linked list is essential, but there will be times
when you will want to remove information too. Deleting links, or elements, is similar to
adding them. You can delete links from the beginning, middle, or end of linked lists. In
each case, the appropriate pointers need to be adjusted. Also, the memory used by the
deleted link needs to be freed.

434 Day 15

Note Don’t forget to free memory when deleting links!


DOunderstand the difference between
calloc()andmalloc(). Most important,
remember that malloc()doesn’t initial-
ize allocated memory—calloc()does.

DON’Tforget to free any memory allo-
cated for links when deleting them.

DO DON’T


Summary ............................................................................................................


Today’s lesson covered some of the advanced uses of pointers. As you probably realize
by now, pointers are a central part of the C language. C programs that don’t use pointers
are rare. You saw how to use pointers to pointers and how arrays of pointers can be very
useful when dealing with strings. You also learned how C treats multidimensional arrays
as arrays of arrays, and you saw how to use pointers with such arrays. You learned how
to declare and use pointers to functions, an important and flexible programming tool.
Finally, you learned how to implement linked lists, a powerful and flexible data storage
method.
This has been a long and involved lesson. Although some of its topics are a bit compli-
cated, they’re exciting as well. With this chapter, you’re really getting into some of the
sophisticated capabilities of the C language. Power and flexibility are among the main
reasons C is such a popular language.

Q&A ....................................................................................................................


Q How many levels deep can I go with pointers to pointers?
AYou need to check your compiler manuals to determine whether there are any limi-
tations. There is rarely any reason to go more than three levels deep with pointers
(pointers to pointers to pointers). Most programs rarely go over two levels.

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

Free download pdf