Tải bản đầy đủ (.pdf) (10 trang)

Lecture Data structures and other objects using C++ - Chapter 5: Linked lists in action - Trường Đại học Công nghiệp Thực phẩm Tp. Hồ Chí Minh

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


</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>

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;


</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>

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;


</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>

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;


</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>

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>


</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>

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


</div>
<span class='text_page_counter'>(7)</span><div class='page_container' data-page=7>

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


</div>
<span class='text_page_counter'>(8)</span><div class='page_container' data-page=8>

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>


</div>
<span class='text_page_counter'>(9)</span><div class='page_container' data-page=9>

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>


</div>
<span class='text_page_counter'>(10)</span><div class='page_container' data-page=10>

<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;


</div>

<!--links-->

×