Download Chapter17

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary search tree wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Chapter 17: Linked Lists
Starting Out with C++
Early Objects
Eighth Edition
by Tony Gaddis, Judy Walters,
and Godfrey Muganda
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Topics
17.1
17.2
17.3
17.4
17.5
17.6
Introduction to the Linked List ADT
Linked List Operations
A Linked List Template
Recursive Linked List Operations
Variations of the Linked List
The STL list Container
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-2
17.1 Introduction to the Linked List ADT
• Linked list: a sequence of data structures (nodes) with
each node containing a pointer to its successor
• The last node in the list has its successor pointer set to
NULL
NULL
list
head
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-3
Linked List Terminology
• The node at the beginning is called the
head of the list
• The entire list is identified by the pointer
to the head node. This pointer is called
the list head.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-4
Linked Lists
• Nodes can be added or removed from
the linked list during execution
• Addition or removal of nodes can take
place at beginning, end, or middle of the
list
list
head
Add or delete node
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
NULL
17-5
Linked Lists vs. Arrays and Vectors
• Linked lists can grow and shrink as
needed, unlike arrays, which have a fixed
size
• Unlike vectors, insertion or removal of a
node in the middle of the list is very efficient
NULL
list
head
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-6
Node Organization
A node contains:
– data: one or more data fields – may be
organized as structure, object, etc.
– a pointer that can point to another node
pointer
data
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-7
Empty List
• A list with no nodes is called the empty
list
• In this case the list head is set to NULL
list
head
NULL
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-8
Creating an Empty List
• Define a pointer for the head of the list:
ListNode *head = NULL;
• Head pointer initialized to NULL to indicate
an empty list
head
NULL
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-9
C++ Implementation
Implementation of nodes requires a
structure containing a pointer to a structure
of the same type (a self-referential data
structure):
struct ListNode
{
int data;
ListNode *next;
};
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-10
C++ Implementation
Nodes can be equipped with constructors:
struct ListNode
{
int data;
ListNode *next;
ListNode(int d, ListNode* p=NULL)
{data = d; next = p;}
};
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-11
Building a List from a File of Numbers
ListNode *head = NULL;
int val;
while (inFile >> val)
{
// add new nodes at the head
head = new ListNode(val, head);
};
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-12
Traversing a Linked List
• List traversals visit each node in a linked list
to display contents, validate data, etc.
• Basic process of traversal:
set a pointer to the head pointer
while pointer is not NULL
process data
set pointer to the successor of the current node
end while
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-13
Traversing a Linked List
nodePtr
5
13
19
NULL
list
head
nodePtr points to the node containing 5, then the
node containing 13, then the node containing 19,
then points to NULL, and the list traversal stops
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-14
17.2 Linked List Operations
Basic operations:
•
•
•
•
•
add a node to the end of the list
insert a node within the list
traverse the linked list
Delete/remove a node from the list
delete/destroy the list
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-15
Creating a Node
ListNode *p;
int num = 23;
p = new ListNode(num);
p
23
NULL
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-16
Appending an Item
To add an item to the end of the list:
• If the list is empty, set head to a new node
containing the item
head = new ListNode(num);
• If the list is not empty, move a pointer p to the last
node, then add a new node containing the item
p->next = new ListNode(num);
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-17
Appending an Item
p
5
13
23
NULL
list
head
List originally has nodes
with 5 and 13.
p locates the last node,
then a node with a new
item, 23, is added
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-18
Destroying a Linked List
• Must remove all nodes used in the list
• To do this, use list traversal to visit each
node
• For each node,
– Unlink the node from the list
– Free the node’s memory
• Finally, set the list head to NULL
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-19
Inserting a Node
• Used to insert an item into a sorted list,
keeping the list sorted.
• Two possibilities:
– Insertion is at the head of the list (because
item at head is already greater than item
being inserted, or because list is empty
– Insertion is after an existing node in a nonempty list
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-20
Inserting a Node at Head of a List
• Test to see if
– head pointer is NULL, or
– node value pointed at by head is greater
than value to be inserted
• Must test in this order: unpredictable
results if second test is attempted on an
empty list
• Create new node, set its next pointer to
head, then point head to it
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-21
Inserting a Node in Body of a List
• Requires two pointers to traverse the list:
– pointer to locate the node with data value
greater than that of node to be inserted
– pointer to 'trail behind' one node, to point to
node before point of insertion
• New node is inserted between the nodes
pointed at by these pointers
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-22
Inserting a Node into a
Linked List
previousNode
5
13
19
list
head
nodePtr
NULL
num
17
Correct position located
Item to insert
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-23
Inserting a Node into a
Linked List
previousNode
5
list
head
13
19
nodePtr
NULL
17
New node created and inserted in order in the linked list
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-24
Removing an Element
• Used to remove a node from a linked list
• Requires two pointers: one to locate the
node to be deleted, one to point to the
node before the node to be deleted
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-25
Deleting a Node
Contents of node to
be deleted: 13
previousNode
5
nodePtr
13
19
NULL
list
head
Locating the node containing 13
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-26
Deleting a Node
previousNode
5
nodePtr
13
19
NULL
list
head
Adjusting pointer around the node to be deleted
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-27
Deleting a Node
previousNode
5
nodePtr
19
NULL
list
head
Linked list after deleting the node containing 13
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-28
17.3 A Linked List Template
• A linked list template can be written by
replacing the type of the data in the node
with a type parameter, say T.
• If defining the linked list as a class
template, then all member functions must
be function templates
• Implementation assumes use with data
types that support comparison: == and <=
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-29
17.4 Recursive Linked List Operations
• A non-empty linked list consists of a
head node followed by the rest of the
nodes
• The rest of the nodes form a linked list
that is called the tail of the original list
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-30
Recursive Linked List Operations
Many linked list operations can be
broken down into the smaller problems
of processing the head of the list and
then recursively operating on the tail of
the list
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-31
Recursive Linked List Operations
To find the length (number of elements) of
a list
– If the list is empty, the length is 0 (base
case)
– If the list is not empty, find the length of the
tail and then add 1 to obtain the length of
the original list
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-32
Recursive Linked List Operations
To find the length of a list:
int length(ListNode *myList)
{
if (myList == NULL) return 0;
else
return 1 + length(myList->next);
}
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-33
Recursive Linked List Operations
Using recursion to display a list:
void displayList(ListNode *myList)
{
if (myList != NULL)
{
cout << myList->data << " ";
displayList(myList->next);
}
}
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-34
Other Recursive Linked List Operations
• Insert and remove operations can be written to
use recursion
• General design considerations:
– Base case is often when the list is empty
– Recursive case often involves the use of the tail of the
list (i.e., the list without the head). Since the tail has
one fewer entry than the list that was passed in to this
call, the recursion eventually stops.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-35
17.5 Variations of the Linked List
Other linked list organizations:
– doubly-linked list: each node contains two
pointers: one to the next node in the list, one to
the previous node in the list
5
list
head
13
19
NULL
NULL
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-36
Variations of the Linked List
Other linked list organizations:
– circular linked list: the last node in the list
points back to the first node in the list, not to
NULL
5
13
19
list
head
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-37
17.6 The STL list Container
• Template for a doubly linked list
• Member functions for
– locating beginning, end of list: front,
back, end
– adding elements to the list: insert,
merge, push_back, push_front
– removing elements from the list: erase,
pop_back, pop_front, unique
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
17-38
Chapter 17: Linked Lists
Starting Out with C++
Early Objects
Eighth Edition
by Tony Gaddis, Judy Walters,
and Godfrey Muganda
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley