linked list action
TRANSCRIPT
-
8/13/2019 Linked List Action
1/45
Chapter 5 introduces the often-
used data structure of linked lists.
This presentation shows how toimplement the most common
operations on linked lists.
Linked Lists in Action
CHAPTER 5Data Structures and Other Objects
-
8/13/2019 Linked List Action
2/45
For this presentation, nodes in a
linked 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;
};
Declarations for Linked Lists
-
8/13/2019 Linked List Action
3/45
The data_field of each node is a type called
value_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;
};
Declarations for Linked Lists
-
8/13/2019 Linked List Action
4/45
Each node also contains a link_field
which 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;
};
Declarations for Linked Lists
-
8/13/2019 Linked List Action
5/45
Declarations for Linked Lists
A program can keep track of the front
node by using a pointer variable such
as head_ptrin 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
-
8/13/2019 Linked List Action
6/45
Declarations for Linked Lists
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
nullin the head pointer.
head_ptr
null
-
8/13/2019 Linked List Action
7/45
void list_head_insert(node*& head_ptr, const node::value_type& entry);
Inserting a Node at the Front
We want to add a new entry, 13,
to the frontof the linked listshown here.
10
15
7
null
head_ptr
entry
13
-
8/13/2019 Linked List Action
8/45
Inserting a Node at the Front
Create a new node, pointed to
by a local variable insert_ptr.
10
15
7
null
head_ptr
entry
13
insert_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
-
8/13/2019 Linked List Action
9/45
Inserting a Node at the Front
insert_ptr = new node;
10
15
7
null
head_ptrentry
13
insert_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
-
8/13/2019 Linked List Action
10/45
Inserting a Node at the Front
10
15
7
null
head_ptrentry
13
insert_ptr13
insert_ptr = new node;
Place the data in the new node's
data_field.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
-
8/13/2019 Linked List Action
11/45
-
8/13/2019 Linked List Action
12/45
Inserting a Node at the Front
10
15
7
null
head_ptrentry
13
insert_ptr13
insert_ptr = new node(entry, head_ptr);
The correct new node
can be completely
created in one step by
calling an appropriatenode constructor.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
-
8/13/2019 Linked List Action
13/45
Inserting a Node at the Front
10
15
7
null
head_ptrentry
13
insert_ptr13
insert_ptr = new node(entry, head_ptr);
Make the old head pointer
point to the new node.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
-
8/13/2019 Linked List Action
14/45
Inserting a Node at the Front
10
15
7
null
head_ptrentry
13
insert_ptr13
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
void list_head_insert(node*& head_ptr, const node::value_type& entry);
-
8/13/2019 Linked List Action
15/45
Inserting a Node at the Front
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
10
15
7
null
head_ptr
13
When the function returns, thelinked list has a new node at the
front.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
-
8/13/2019 Linked List Action
16/45
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
Inserting a Node at the Front
-
8/13/2019 Linked List Action
17/45
Inserting a Node at the Front
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}Does the function work
correctly for the empty
list ?
-
8/13/2019 Linked List Action
18/45
head_ptrentry
13 null
Inserting a Node at the Front
Does the function work
correctly for the empty
list ?
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
-
8/13/2019 Linked List Action
19/45
Inserting a Node at the Front
head_ptrentry
13 null
insert_ptr13
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}
null
-
8/13/2019 Linked List Action
20/45
-
8/13/2019 Linked List Action
21/45
Inserting a Node at the Front
head_ptr
13
null
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;
}When the function
returns, the linked list
has one node.
-
8/13/2019 Linked List Action
22/45
Caution!
Always make sure that
your linked list
functions work
correctly with an
empty list.
EMPTY LIST
-
8/13/2019 Linked List Action
23/45
Pseudocode for Inserting Nodes
Nodes are often inserted at places other than the
front of a linked list.
There is a general pseudocode that you can follow
for any insertion function. . .
-
8/13/2019 Linked List Action
24/45
Pseudocode for Inserting Nodes
Determine whether the new node will be the first node in
the linked list. If so, then there is only one step:
list_head_insert(head_ptr, entry);
-
8/13/2019 Linked List Action
25/45
Pseudocode for Inserting Nodes
Determine whether the new node will be the first node in
the linked list. If so, then there is only one step:
list_head_insert(head_ptr, entry);
-
8/13/2019 Linked List Action
26/45
Pseudocode for Inserting Nodes
Determine whether the new node will be the first node in
the linked list. If so, then there is only one step:
list_head_insert(head_ptr, entry);
A pointer
to the
head of
the list
-
8/13/2019 Linked List Action
27/45
Pseudocode for Inserting Nodes
Determine whether the new node will be the first node in
the linked list. If so, then there is only one step:
list_head_insert(head_ptr, entry);
-
8/13/2019 Linked List Action
28/45
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptrto point to thenode which is just beforethe new node's position.
-
8/13/2019 Linked List Action
29/45
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptrto point to thenode which is just beforethe new node's position.
In this example, the
new node will be
the second node
previous_ptr
-
8/13/2019 Linked List Action
30/45
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptrto point to thenode which is just before the new node's position
What is the name of
this orange pointer ?
Look at the pointer
which is in the node
*previous_ptr
previous_ptr
-
8/13/2019 Linked List Action
31/45
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptrto point to thenode which is just before the new node's position
This pointer is called
previous_ptr->link_field
(although this name may
be private to the node)
What is the name of
this orange pointer ?
previous_ptr
-
8/13/2019 Linked List Action
32/45
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptrto point to thenode which is just before the new node's position
previous_ptr->link_field
points to the head
of a small linked
list, with 10 and 7
previous_ptr
-
8/13/2019 Linked List Action
33/45
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptrto point to thenode which is just before the new node's position.
The new node must
be inserted at the
front of this small
linked list.
13
Write one C++ statement
which will do the insertion.
previous_ptr
-
8/13/2019 Linked List Action
34/45
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptrto point to thenode which is just before the new node's position.
13
What might cause this
statement to fail to compile?
previous_ptrlist_head_insert(previous_ptr->link_field, entry);
-
8/13/2019 Linked List Action
35/45
Pseudocode for Inserting Nodes
15
10
7
null
head_ptr
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptrto point to thenode which is just before the new node's position.
13
Use a node member function
to get the link field if
needed.
previous_ptrlist_head_insert(previous_ptr->link( ), entry);
-
8/13/2019 Linked List Action
36/45
Pseudocode for Inserting Nodes
Determine whether the new node will be the first node in
the linked list. If so, then there is only one step:
list_head_insert(head_ptr, entry);
Otherwise (if the new node will not be first):
Set a pointer namedprevious_ptrto point to the node
which is just before the new node's position.
Make the function call:list_head_insert(previous_ptr->link( ), entry);
-
8/13/2019 Linked List Action
37/45
Pseudocode for Inserting Nodes
The process of adding a new node in the middle
of a list can also be incorporated as a separate
function. This function is called list_insert in the
linked list toolkit of Section 5.2.
-
8/13/2019 Linked List Action
38/45
Pseudocode for Removing Nodes
Nodes often need to be removed from a linked list.
As with insertion, there is a technique for removing
a node from the front of a list, and a technique for
removing a node from elsewhere.
Well look at the pseudocode for removing a node
from the front of a linked list.
-
8/13/2019 Linked List Action
39/45
Removing the Head Node
10 15 7
nullhead_ptr
13
Start by setting up a temporary pointer named remove_ptrto
the head node.
remove_ptr
-
8/13/2019 Linked List Action
40/45
Removing the Head Node
10 15 7
null
head_ptr
13
Set up remove_ptr.
head_ptr = remove_ptr->link( );
remove_ptr
Draw the change that this
statement will make to the
linked list.
-
8/13/2019 Linked List Action
41/45
Removing the Head Node
10 15 7
nullhead_ptr
13
Set up remove_ptr.
head_ptr = remove_ptr->link( );
remove_ptr
-
8/13/2019 Linked List Action
42/45
Removing the Head Node
Set up remove_ptr.
head_ptr = remove_ptr->link( );
delete remove_ptr; // Return the node's memory to heap.
10 15 7
nullhead_ptr
13
remove_ptr
-
8/13/2019 Linked List Action
43/45
Removing the Head Node
Heres what the linked list looks like after the removal finishes.
10 15 7
nullhead_ptr
-
8/13/2019 Linked List Action
44/45
It is easy to insert a node at the front of a list.
The linked list toolkit also provides a function for
inserting a new node elsewhere It is easy to remove a node at the front of a list.
The linked list toolkit also provides a function for
removing a node elsewhere--you should read
about this function and the other functions of the
toolkit.
Summary
-
8/13/2019 Linked List Action
45/45
THE END
Presentation copyright 2010, Addison Wesley Longman,
For use withData Structures and Other Objects Using C++
by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club
Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
Students and instructors who useData Structures and Other Objects Using C++are welcome
to use this presentation however they see fit, so long as this copyright notice remains
intact.