Chapt 05
Chapt 05
Chapt 05
CHAPTER 5
Data Structures and Other Objects
Declarations for Linked Lists
link_field 7 data_field
head_ptr
null
link_field
Declarations for Linked Lists
null
head_ptr
Inserting a Node at the Front
10
7
13
null
entry
head_ptr
Inserting a Node at the Front
15
10
7
13
null
entry
head_ptr
Inserting a Node at the Front
15
10
7
13
null
entry
head_ptr
Inserting a Node at the Front
10
7
13
null
entry
head_ptr
Inserting a Node at the Front
7
13
null
entry
head_ptr
Inserting a Node at the Front
10
7
13
null
entry
head_ptr
Inserting a Node at the Front
15
10
7
13
null
entry
head_ptr
Inserting a Node at the Front
15
When the function returns, the
linked list has a new node at the 10
front. 7
null
head_ptr
Inserting a Node at the Front
insert_ptr
13 null 13
entry null
head_ptr
Inserting a Node at the Front
insert_ptr
13 13
entry null
head_ptr
Inserting a Node at the Front
13
head_ptr null
Caution!
EMPTY LIST
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. . .
Pseudocode for Inserting Nodes
A pointer
to the
head of
the list
Pseudocode for Inserting Nodes
Th
in e d
th a t
en a
ew t o p
no ut
de
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
7
null
head_ptr
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
7
What is the name of
null
this orange pointer ? head_ptr
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
7
What is the name of
null
this orange pointer ? head_ptr
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
previous_ptr->link_field
10
points to the head previous_ptr
of a small linked
list, with 10 and 7 15
7
null
head_ptr
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
13
7
Write one C++ statement
null
which will do the insertion. head_ptr
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
13
10
list_head_insert(previous_ptr->link_field, entry); previous_ptr
15
7
What might cause this
statement to fail to compile? null
head_ptr
Pseudocode for Inserting Nodes
Otherwise (if the new node will not be first):
Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
13
10
list_head_insert(previous_ptr->link( ), entry); previous_ptr
15
7
Use a node member function
to get the link field if needed. null
head_ptr
Pseudocode for Inserting Nodes
remove_ptr
13 10 15 7
null
head_ptr
Removing the Head Node
Set up remove_ptr.
head_ptr = remove_ptr->link( );
13 10 15 7
null
head_ptr
Removing the Head Node
Set up remove_ptr.
head_ptr = remove_ptr->link( );
remove_ptr
13 10 15 7
null
head_ptr
Removing the Head Node
Set up remove_ptr.
head_ptr = remove_ptr->link( );
delete remove_ptr; // Return the node's memory to heap.
remove_ptr
13 10 15 7
null
head_ptr
Removing the Head Node
Here’s what the linked list looks like after the removal finishes.
10 15 7
null
head_ptr
Summary
Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright
Corel 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 use Data Structures and Other Objects Using C++ are welcome
to use this presentation however they see fit, so long as this copyright notice remains
intact.
THE END