294 Intermediate C Programming
It may be necessary to remove accounts that have not been used for more than one year.
To manage this type of application, we must be able to allocate memory on an as-needed
basis. Memory usage must grow and shrink as the demands of the application require. This
chapter describes how to use dynamic structures. The book covers only the basic concepts
and does not give enough knowledge required to actually build a social network site. The
information in this chapter, however, provides a foundation.
If data structures, such as arrays, need to change size as programs run, we can create
a new larger (or smaller) array, copy the data, and then free the old array. Section 17.2
gives an example of an auto-resizing array: If too many digits are inserted, the array’s size
doubles. This chapter explains how to create a simple data structure that is designed to
grow without copying the existing data. It supports the following functions:
• Insert: add new data, and allocate memory as needed.
• Search: determine whether a piece of data has already been inserted.
• Delete: remove data and release memory if it is no longer needed.
• Print: print the stored data.
• Destroy: delete everything before the program ends.
The simple data structure is called a linked list, and is an example of dynamic structures
and is also an example of container structures. Such structures may contain different types
of data (int, char, Person ...). The code to insert, search, delete, print, and destroy is
quite similar for each different type. The next chapter will describe another type of container
structure called the binary tree.
18.2 Linked Lists
A linked list is a collection of nodes that are linked together by pointers. Each node is
an object with at least two attributes:
• A pointer to another node. By convention, this attribute is called next. If a given
node does not have a “next” node, then the next attribute stores NULL.
• Some data. This attribute may be a pointer, an object, or a primitive type such as
int, char, or double.
Below is an example structure, with two attributes, used as a node in a linked list. For
simplicity, the data attribute is an integer.
typedef s t ru ct listnode1
{2
st ruc t listnode * next ;3
int value ;4
} Node ; // do not forget ;5
This introduces the concept of a “self-referring structure”. Notice how next is a pointer to
struct listnode? That is what makes it self-referring. Because the C compiler reads source
files from top to bottom, it cannot see the type name Node at the time next is declared.
Thus, we assign a temporary type name: struct listnode. It is possible to refer to the
nodes as struct listnode; however, the structure is called Node from now on. Fig. 18.1
shows three views for the first operation in creating a linked list.
The diagrammatic view in (b) is the most commonly used. In (b), when a pointer’s value
is NULL, it is common to make it point to the “Ground” symbol used in electronics. My
experience working with students suggests that some students are more comfortable with