Data Structures and Other Objects Using C++ 4th Edition Edition Michael Main download
Data Structures and Other Objects Using C++ 4th Edition Edition Michael Main download
https://ebookmass.com/product/data-structures-and-other-objects-
using-c-4th-edition-edition-michael-main/
https://ebookmass.com/product/data-structures-and-algorithm-analysis-
in-c-4th-edition-ebook-pdf/
https://ebookmass.com/product/data-structures-and-algorithms-in-c-2nd-
edition/
https://ebookmass.com/product/starting-out-with-c-from-control-
structures-through-objects-8th-edition-ebook-pdf/
https://ebookmass.com/product/starting-out-with-c-from-control-
structures-through-objects-ninth-edition-global-edition-tony-gaddis/
Data structure using C Reema Thareja
https://ebookmass.com/product/data-structure-using-c-reema-thareja/
https://ebookmass.com/product/c-programming-program-design-including-
data-structures-8th-edition-d-s-malik/
https://ebookmass.com/product/data-parallel-c-programming-accelerated-
systems-using-c-and-sycl-2nd-edition-james-reinders/
https://ebookmass.com/product/statistics-informed-decisions-using-
data-4th-edition-ebook-pdf/
https://ebookmass.com/product/data-parallel-c-programming-accelerated-
systems-using-c-and-sycl-james-reinders/
Preface i
DATA
STRUCTURES
&
OTHER
OBJECTS
Using C++ 4TH
EDITION
MICHAEL MAIN
Department of Computer Science
University of Colorado at Boulder
WALTER SAVITCH
Department of Computer Science
and Engineering
University of California, San Diego
Addison-Wesley
Boston Columbus Indianapolis New York San Francisco Upper Saddle River
Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto
Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
ii Preface
Editor-in-Chief: Michael Hirsch Marketing Manager: Erin Davis
Editorial Assistant: Stephanie Sellinger Marketing Coordinator: Kathryn Ferranti
Managing Editor: Jeffrey Holcomb Art Director: Linda Knowles
Production Project Manager: Heather McNally Cover Designer: Elena Sidorova
Copy Editor: Sada Preisch Cover Artwork: © 2010 Stocksearch / Alamy
Proofreader: Genevieve d’Entremont Senior Manufacturing Buyer: Carol Melville
Access the latest information about all Pearson Addison-Wesley Computer Science titles from our World
Wide Web site: http://www.pearsonhighered.com/cs
The programs and applications presented in this book have been included for their instructional value. They
have been tested with care, but are not guaranteed for any particular purpose. The publisher does not offer any
warranties or representations, nor does it accept any liabilities with respect to the programs or applications.
Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook
appear on appropriate page within text.
UNIX® is a registered trademark in the United States and other countries, licensed exclusively through X-Open
Company, Ltd. Pez® is a registered trademark of Pez Candy, Inc.
Copyright © 2011, 2005, 2001, 1997 Pearson Education, Inc., publishing as Addison-Wesley, 501 Boylston
Street, Suite 900, Boston MA 02116. All rights reserved. Manufactured in the United States of America. This
publication is protected by Copyright, and permission should be obtained from the publisher prior to any
prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic,
mechanical, photocopying, recording, or likewise. To obtain permission(s) to use material from this work,
please submit a written request to Pearson Education, Inc., Permissions Department, , 501 Boylston Street,
Suite 900, Boston MA 02116.
Many of the designations by manufacturers and seller to distinguish their products are claimed as trademarks.
Where those designations appear in this book, and the publisher was aware of a trademark claim, the
designations have been printed in initial caps or all caps.
QA76.73.C153M25 2010
005.13’3—dc22
2009049328
CIP
1 2 3 4 5 6 7 8 9 10--CRS--14 13 12 11 10
Preface
menting these (or similar) classes. With this in mind, the primary changes that
you’ll find for this edition are:
• A new Section 2.6 that gives an early introduction to the Standard Tem-
plate Library using the pair class. We have been able to introduce students
to the STL here even before they have a full understanding of templates.
• An earlier introduction of the multiset class and STL iterators in Section
3.4. This is a good location for the material because the students have just
seen how to implement their first collection class (the bag), which is
based on the multiset.
• We continue to introduce the STL string class in Section 4.5, where it’s
appropriate for the students to implement their own string class with a
dynamic array.
• A new Section 5.6 that compares three similar STL classes: the vector, the
list, and the deque. At this point, the students have enough knowledge to
understand typical vector and list implementations.
• A first introduction to the STL algorithms appears in Section 6.3, and this
is now expanded on in Sections 11.2 (the heap algorithms) and 13.4
(expanded coverage of sorting and binary search in the STL).
• A new Section 8.4 provides typical implementation details for the STL
deque class using an interesting combination of dynamic arrays and point-
ers.
• A discussion of hash tables in the proposed TR1 expansions for the STL
is now given in Section 12.6.
Most chapters also include new programming projects, and you may also keep
an eye on our project web site, www.cs.colorado.edu/~main/dsoc.html, for new
projects as we develop them.
Step 1: Understand the data type abstractly. At this level, a student gains an
understanding of the data type and its operations at the level of concepts and
pictures. For example, a student can visualize a stack and its operations of push-
ing and popping elements. Simple applications are understood and can be car-
ried out by hand, such as using a stack to reverse the order of letters in a word.
Step 2: Write a specification of the data type as a C++ class. In this step,
the student sees and learns how to write a specification for a C++ class that can
Preface v
implement the data type. The specification includes prototypes for the construc-
tors, public member functions, and sometimes other public features (such as an
underlying constant that determines the maximum size of a stack). The prototype
of each member function is presented along with a precondition/postcondition
contract that completely specifies the behavior of the function. At this level, it’s
important for the students to realize that the specification is not tied to any par-
ticular choice of implementation techniques. In fact, this same specification may
be used several times for several different implementations of the same data type.
Step 3: Use the data type. With the specification in place, students can write
small applications or demonstration programs to show the data type in use.
These applications are based solely on the data type’s specification, as we still
have not tied down the implementation.
understood. With this in mind, some instructors may wish to cover Chapter 14
earlier, just before stacks and queues.
Another alternative is to identify students who already know the basics of
classes. These students can carry out an inheritance project (such as the ecosys-
tem of Section 14.2 or the game engine in Section 14.3) while the rest of the stu-
dents first learn about classes.
Iterators. Iterators are another important part of the proposed Standard Tem-
plate Library, allowing a programmer to easily step through the items in a con-
tainer object (such as the elements of a set or bag). Such iterators may be
internal (implemented with member functions of the container class) or external
(implemented by a separate class that is a friend of the container class). We
introduce internal iterators with one of the first container classes (a sequential
list in Section 3.2). An internal iterator is added to the bag class when it is
needed in Chapter 6. At that point, the more complex external iterators also are
discussed, and students should be aware of the advantages of an external itera-
tor. Throughout the text, iterators provide a good opportunity for programming
projects, such as implementing an external bag iterator (Chapter 6) or using a
stack to implement an internal iterator of a binary search tree (Chapter 10).
In a course that has time for advanced tree projects (Chapter 11), we analyze
the recursive tree algorithms, explaining the importance of keeping the trees
balanced—both to improve worst-case performance, and to avoid potential run-
time stack overflow.
Advanced Projects
The text offers good opportunities for optional projects that can be undertaken
by a more advanced class or by students with a stronger background in a large
class. Particular advanced projects include the following:
• A polynomial class using dynamic memory (Section 4.6).
• An introduction to Standard Library iterators, culminating in an imple-
mentation of an iterator for the student’s bag class (Sections 6.3 through
6.5).
• An iterator for the binary search tree (Programming Projects in Chapter
10).
• A priority queue, implemented with a linked list (Chapter 8 projects), or
implemented using a heap (Section 11.1).
• A set class, implemented with B-trees (Section 11.3). We have made a
particular effort on this project to provide information that is sufficient for
students to implement the class without need of another text. In our
courses, we have successfully directed advanced students to do this
project as independent work.
• An inheritance project, such as the ecosystem of Section 14.2.
• An inheritance project using an abstract base class such as the game base
class in Section 14.3 (which allows easy implementation of two-player
games such as Othello or Connect Four).
• A graph class and associated graph algorithms from Chapter 15. This is
another case where advanced students may do work on their own.
Preface ix
Typical course. Start with Chapters 1–10, skipping parts of Chapter 2 if the
students have a prior background in C++ classes. Most chapters can be covered
in a week, but you may want more time for Chapter 5 (linked lists), Chapter 6
(templates), Chapter 9 (recursion), or Chapter 10 (trees). Typically, we cover the
material in 13 weeks, including time for exams and extra time for linked lists
and trees. Remaining weeks can be spent on a tree project from Chapter 11, or
on binary search (Section 12.1) and sorting (Chapter 13).
extra weeks at the end of the term, so that students may spend more time on
searching, sorting, and the advanced topics (shaded on page xi.)
We also have taught the course with further acceleration by spending no lec-
ture time on stacks and queues (but assigning those chapters as reading).
Early recursion / early sorting. One to three weeks may be spent at the start
of class on recursive thinking. The first reading will then be Chapters 1 and 9,
perhaps supplemented by additional recursive projects.
If recursion is covered early, you may also proceed to cover binary search
(Section 12.1) and most of the sorting algorithms (Chapter 13) before introduc-
ing C++ classes.
Chapter Dependencies
At the start of the course, students should be comfortable writing functions and using
arrays in C++ or C. Those who have used only C should read Appendix F and pay
particular attention to the discussion of reference parameters in Section 2.4.
Chapter 1
Introduction
Section 14.3
Virtual Methods Section 11.4
Detailed Tree Analysis
The shaded boxes provide
good opportunities for
advanced work.
xii Preface
Acknowledgments
We started this book while Walter was visiting Michael at the Computer Science
Department of the University of Colorado in Boulder. The work was completed
after Walter moved back to the Department of Engineering and Computer Sci-
ence at the University of California, San Diego. We are grateful to these institu-
tions for providing facilities, wonderful students, and interaction with congenial
colleagues.
Our students have been particularly helpful—nearly 5000 of our students
worked through the material, making suggestions, showing us how they learned.
We thank the reviewers and instructors who used the material in their data struc-
tures courses and provided feedback: Zachary Bergen, Cathy Bishop, Martin
Burtscher, Gina Cherry, Courtney Comstock, Stephen Davies, Robert Frohardt,
John Gillett, Mike Hendricks, Ralph Hollingsworth, Yingdan Huang, Patrick
Lynn, Ron McCarty, Shivakant Mishra, Evi Nemeth, Rick Osborne, Rachelle
Reese, and Nicholas Tran. The book was also extensively reviewed by Wolfgang
W. Bein, Bill Hankley, Michael Milligan, Paul Nagin, Jeff Parker, Andrew L.
Wright, John R. Rose, and Evan Zweifel. We thank these colleagues for their
excellent critique and their encouragement.
Thank you to Lesley McDowell and Chris Schenk, who are pleasant and
enthusiastic every day in the computer science department at the University of
Colorado. Our thanks also go to the editors and staff at Addison-Wesley. Heather
McNally’s work has encouraged us and provided us with smooth interaction on
a daily basis and eased every step of the production. Karin Dejamaer and Jessica
Hector provided friendly encouragement in Boulder, and we offer our thanks to
them. We welcome and appreciate Michael Hirsch in the role of editor, where he
has shown amazing energy, enthusiasm, and encouragement. Finally, our origi-
nal editor, Susan Hartman, has provided continual support, encouragement, and
direction—the book wouldn’t be here without you!
In addition to the work and support from those who put the book together, we
thank those who offered us daily interest and encouragement. Our deepest thanks
go to Holly Arnold, Vanessa Crittenden, Meredith Boyles, Suzanne Church, Erika
Civils, Lynne Conklin, Andrzej Ehrenfeucht, Paul Eisenbrey, Skip Ellis, John
Kennedy, Rick Lowell, George Main, Mickey Main, Jesse Nuzzi, Ben Powell,
Marga Powell, Megan Powell, Grzegorz Rozenberg, Hannah, Timothy, and
Janet.
Contents
2.2 Constructors 45
The Throttle’s Constructor 46
What Happens If You Write a Class with No Constructors? 49
Programming Tip: Always Provide Constructors 49
Revising the Throttle’s Member Functions 49
Inline Member Functions 49
Programming Tip: When to Use an Inline Member Function 50
Self-Test Exercises for Section 2.2 51
2.3 Using a Namespace, Header File, and Implementation File 51
Creating a Namespace 51
The Header File 52
Describing the Value Semantics of a Class Within the Header File 56
Programming Tip: Document the Value Semantics 57
The Implementation File 57
Using the Items in a Namespace 59
Pitfall: Never Put a Using Statement Actually in a Header File 60
Self-Test Exercises for Section 2.3 62
2.4 Classes and Parameters 63
Programming Example: The Point Class 63
Default Arguments 65
Programming Tip: A Default Constructor Can Be Provided by Using Default
Arguments 66
Parameters 67
Pitfall: Using a Wrong Argument Type for a Reference Parameter 70
Clarifying the Const Keyword
Part 3: Const Reference Parameters 72
Programming Tip: Use const Consistently 73
When the Type of a Function’s Return Value Is a Class 73
Self-Test Exercises for Section 2.4 74
2.5 Operator Overloading 74
Overloading Binary Comparison Operators 75
Overloading Binary Arithmetic Operators 76
Overloading Output and Input Operators 77
Friend Functions 80
Programming Tip: When to Use a Friend Function 81
The Point Class—Putting Things Together 82
Summary of Operator Overloading 85
Self-Test Exercises for Section 2.5 85
2.6 The Standard Template Libary and the Pair Class 86
Chapter Summary 87
Solutions to Self-Test Exercises 88
Programming Projects 90
Contents xvii
ebookmasss.com