Linked Lists in Action
Linked Lists in Action
Linked Lists in Action
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
Data Structures and Other Objects
For this presentation, nodes in a linked list are objects, as shown here.
15
class node { public: typedef double value_type; ... private value_type data_field; node *link_field; };
data_field
10
link_field data_field
link_field
data_field
null
link_field
15
class node { public: typedef int value_type; ... private value_type data_field; node *link_field; };
data_field
10
link_field data_field
link_field
data_field
null
link_field
15
class node { public: typedef int value_type; ... private value_type data_field; node *link_field; };
data_field
10
link_field data_field
link_field
data_field
null
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.
10
data_field
15
data_field
link_field
link_field head_ptr
data_field
null
link_field
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 null in the head pointer.
null
head_ptr
We want to add a new entry, 13, to the front of the linked list shown here.
15 10 13
entry head_ptr
7 null
insert_ptr
15 10 13
entry head_ptr
7 null
15 10 13
entry head_ptr
7 null
insert_ptr = new node; Place the data in the new node's data_field.
13
insert_ptr
15 10 13
entry head_ptr
7 null
insert_ptr = new node; Place the data in the new node's data_field. Connect the new node to the front of the list.
13
entry head_ptr
13
insert_ptr
15 10 7 null
insert_ptr = new node(entry, head_ptr); The correct new node can be completely created in one step by calling an appropriate node constructor.
13
entry head_ptr
13
insert_ptr
15 10 7 null
insert_ptr = new node(entry, head_ptr); insert_ptr Make the old head pointer point 13 to the new node.
15 10 13
entry head_ptr
7 null
insert_ptr
15 10 13
entry head_ptr
7 null
When the function returns, the linked list has a new node at the front.
head_ptr
10 7 null
13
entry
Does the function work correctly for the empty null list ?
head_ptr
13
entry
null
head_ptr
13 null
insert_ptr
13
entry head_ptr
13 null
insert_ptr
When the function returns, the linked list has one node.
13
head_ptr
null
Caution!
Always make sure that your linked list functions work correctly with an empty list.
EMPTY LIST
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. . .
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);
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);
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
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);
in e th dat en a e w to no put de Th
previous_ptr
10
15 7 null
head_ptr
previous_ptr
10
15 7 null
head_ptr
previous_ptr
10
15 7 null
head_ptr
previous_ptr
10
15 7 null
head_ptr
The new node must be inserted at the front of this small linked list.
previous_ptr
10
15 7 null
head_ptr
list_head_insert(previous_ptr->link_field, entry);
previous_ptr
10
15
7 null
head_ptr
list_head_insert(previous_ptr->link( ), entry);
previous_ptr
10
15
7 null
head_ptr
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 named previous_ptr to point to the node which is just before the new node's position. Make the function call: list_head_insert(previous_ptr->link( ), entry);
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.
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.
remove_ptr
13
head_ptr
10
15
7 null
remove_ptr
Draw the change that this statement will make to the linked list.
15 7 null
13
head_ptr
10
remove_ptr
13
head_ptr
10
15
7 null
remove_ptr
13
head_ptr
10
15
7 null
10
head_ptr
15
7 null
Summary
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.
Presentation copyright 2010, Addison Wesley Longman, For use with Data 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 (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