Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (622.47 KB, 10 trang )
<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>
Chapter 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
For this presentation, nodes in a
linked list are objects, as shown here.
data_field
link_field
<i>10</i>
data_field
link_field
<i>15</i>
data_field
link_field
<i>7</i>
null
class node
{
public:
typedef double value_type;
...
private
value_type data_field;
node *link_field;
The data_field of each node is a type called
value_type, defined by a typedef.
data_field
link_field
<i>10</i>
data_field
link_field
<i>15</i>
data_field
link_field
<i>7</i>
null
class node
{
public:
typedef int value_type;
...
private
value_type data_field;
Each node also contains a link_field
which is a pointer to another node.
data_field
link_field
<i>10</i>
data_field
link_field
<i>15</i>
data_field
link_field
<i>7</i>
null
class node
{
public:
typedef int value_type;
...
private
value_type data_field;
node *link_field;
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.
head_ptr
data_field
link_field
<i>10</i>
data_field
link_field
<i>15</i>
data_field
link_field
<i>7</i>
A 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
<b>null</b><i> </i>in the head pointer.
head_ptr
We want to add a new entry, 13,
We want to add a new entry, 13,
to the
to the front<sub>front</sub> of the linked list of the linked list
shown here.
shown here.
<i>10</i>
<i>15</i>
<i>7</i>
<i>null</i>
head_ptr
entry
Create a new node, pointed to
by a local variable insert_ptr.
<i>10</i>
<i>15</i>
<i>7</i>
<i>7</i>
null
head_ptr
entry
<i>13</i>
insert_ptr = new node;
<i>10</i>
<i>15</i>
<i>7</i>
<i>7</i>
<i>null</i>
head_ptr
entry
<i>13</i>
<i>10</i>
<i>15</i>
<i>7</i>
<i>null</i>
head_ptr
entry
<i>13</i>
insert_ptr
<i>13</i>
insert_ptr = new node;