A program can keep track of the front node by using a pointer variable such as head_ptr in this example. Notice that head_ptr is not a node it is a pointer to a node[r]
Trang 1Chapter 5 introduces the often used data structure of linked lists This presentation shows how to implement the most common operations on linked lists
CHAPTER 5
Data Structures and Other Objects
Trang 2linked list are objects, as shown here
data_field link_field
10
data_field link_field
15
data_field link_field
7
null
class node
{
public:
typedef double value_type;
private
value_type data_field;
node *link_field;
};
Trang 3value_type, defined by a typedef
data_field
link_field
10
data_field
link_field
15
data_field
link_field
7
null
class node
{
public:
typedef int value_type;
private
value_type data_field;
node *link_field;
};
Trang 4which is a pointer to another node
data_field
link_field
10
data_field
link_field
15
data_field
link_field
7
null
class node
{
public:
typedef int value_type;
private
value_type data_field;
node *link_field;
};
Trang 5node by using a pointer variable such
as head_ptr in this example
Notice that head_ptr is not a node it
is a pointer to a node
head_ptr
data_field link_field
10
data_field link_field
15
data_field link_field
7
null
Trang 6A program can keep track of the front node by using a pointer variable such
as head_ptr
Notice that head_ptr is not a node it
is a pointer to a node
We represent the empty list by storing
null in the head pointer.
head_ptr
null
Trang 7to the front of the linked list
shown here
10
15
7 null
head_ptr entry
13
Trang 8by a local variable insert_ptr
10
15
7
null
head_ptr entry
13
insert_ptr
Trang 9insert_ptr = new node;
10
15
7
null
head_ptr entry
13
insert_ptr
Trang 1015
7 null
head_ptr entry
13
insert_ptr
13
insert_ptr = new node;
Place the data in the new node's
data_field