Tải bản đầy đủ (.pdf) (768 trang)

Data abstraction and problem solving with c++

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (36.42 MB, 768 trang )


Preface C++ 3e.fm Page iii Monday, July 16, 2001 1:44 PM

PREFACE

Welcome to the third edition of Data Abstraction and Problem Solving with
C++: Walls and Mirrors. Since the publication of the second edition, we
all have gained experience with teaching data abstraction in an objectoriented way using C++. This edition reflects that experience and the
evolution that C++ has taken.
This book is based on the original Intermediate Problem Solving and
Data Structures: Walls and Mirrors by Paul Helman and Robert Veroff
(© 1986 by The Benjamin/Cummings Publishing Company, Inc.). This
work builds on their organizational framework and overall perspective
and includes technical and textual content, examples, figures, and exercises derived from the original work. Professors Helman and Veroff introduced two powerful analogies, walls and mirrors, that have made it
easier for us to teach—and to learn—computer science.
With its focus on data abstraction and other problem-solving tools,
this book is designed for a second course in computer science. In recognition of the dynamic nature of the discipline and the great diversity in
undergraduate computer science curricula, this book includes comprehensive coverage of enough topics to make it appropriate for other
courses as well. For example, you can use this book in courses such as
introductory data structures or advanced programming and problem
solving. The goal remains to give students a superior foundation in data
abstraction, object-oriented programming, and other modern problemsolving techniques.

TO THE STUDENT
Thousands of students before you have read and learned from Walls and
Mirrors . The walls and mirrors in the title represent two fundamental
problem-solving techniques that appear throughout the presentation. Data
abstraction isolates and hides the implementation details of a module
from the rest of the program, much as a wall can isolate and hide you from
your neighbor. Recursion is a repetitive technique that solves a problem by
solving smaller problems of exactly the same type, much as mirror images


grow smaller with each reflection.
This book was written with you in mind. As former college students,
and as educators who are constantly learning, we appreciate the importance of a clear presentation. Our goal is to make this book as understandable as possible. To help you learn and to review for exams, we have
iii


Preface C++ 3e.fm Page iv Monday, July 16, 2001 1:44 PM

iv

APPROACH

included such learning aids as margin notes, chapter summaries, self-test
exercises with answers, and a glossary. As a help during programming,
you will find C++ reference material in the appendixes and inside the
covers. You should review the list of this book’s features given later in this
preface in the section “Pedagogical Features.”
The presentation makes some basic assumptions about your knowledge of C++. Some of you may need to review this language or learn it for
the first time by consulting Appendix A of this book. You will need to
know about the selection statements if and switch; the iteration statements for, while, and do; functions and argument passing; arrays;
strings; structures; and files. This book covers C++ classes in Chapters 1, 3,
and 8 and does not assume that you already know this topic. We assume
no experience with recursive functions, which are included in Chapters 2
and 5.
All of the C++ source code that appears in this book is available for
your use. Later in this preface, the description of supplementary materials tells you how to obtain these files. Note, however, that your instructor may already have obtained them for you.

APPROACH
This edition of Walls and Mirrors uses C++ to enhance its emphasis on
data abstraction and data structures. The book carefully accounts for the

strengths and weaknesses of the C++ language and remains committed
to a pedagogical approach that makes the material accessible to students
at the introductory level.

Prerequisites
We assume that readers either know the fundamentals of C++ or know
another language and have an instructor who will help them make the
transition to C++ by using the provided appendix. The book formally
introduces C++ classes, and so does not assume prior knowledge of
them. Included are the basic concepts of object-oriented programming,
inheritance, virtual functions, and class templates, all in C++. Although
the book provides an introduction to these topics in connection with
the implementations of abstract data types (ADTs) as classes, the emphasis remains on the ADTs, not on C++. The material is presented in the
context of object-based programming, but it assumes that future courses
will cover object-oriented design and software engineering in detail, so
that the focus can remain on data abstraction. We do, however, introduce the Unified Modeling Language (UML) as a design tool.

Flexibility
The extensive coverage of this book should provide you with the material that you want for your course. You can select the topics you desire


Preface C++ 3e.fm Page v Monday, July 16, 2001 1:44 PM

PREFACE

v

and present them in an order that fits your course. The chapter dependency chart shows which chapters should be covered before a given
chapter can be taught.
In Part I, you can choose among topics according to your students’

background. Three of the chapters in this part provide an extensive
introduction to data abstraction and recursion. Both topics are important, and there are various opinions about which should be taught first.

CHAPTER DEPENDENCY CHART
This chart indicates the chapters that should be studied before you cover a particular chapter.

Chapter 1
Principles

Chapter 3
Data abstraction
Chapter 2
Recursion
Chapter 4
Linked lists
Chapter 5
More recursion

Chapter 6
Stacks

Chapter 7
Queues

Chapter 8
Advanced C++ topics

Chapter 9
Algorithm efficiency, sorting


Chapter 10
Trees

Chapter 13
Graphs

Chapter 11
Tables, priority queues

Chapter 12
Advanced tables
Chapter 14
Section on
external tables
Dependency by one section of chapter
Dependency that you can ignore

Section on
external sorting


Preface C++ 3e.fm Page vi Monday, July 16, 2001 1:44 PM

vi

APPROACH

Although in this book a chapter on recursion both precedes and follows
the chapter on data abstraction, you can simply rearrange this order.
Part II treats topics that you can also cover in a flexible order. For

example, you can cover all or parts of Chapter 8 on advanced C++ either
before or after you cover stacks (Chapter 6). You can cover algorithm efficiency and sorting (Chapter 9) any time after Chapter 5. You can introduce trees before queues or graphs before tables, or cover hashing,
balanced search trees, or priority queues any time after tables and in any
order. You also can cover external methods (Chapter 14) earlier in the
course. For example, you can cover external sorting after you cover
mergesort in Chapter 9.

Data Abstraction
The design and use of abstract data types permeate this book’s problemsolving approach. Several examples demonstrate how to design an ADT
as part of the overall design of a solution. All ADTs are first specified—in
both English and pseudocode—and then used in simple applications
before implementation issues are considered. The distinction between an
ADT and the data structure that implements it remains in the forefront
throughout the discussion. The book explains both encapsulation and
C++ classes early. Students see how C++ classes hide an implementation’s
data structure from the client of the ADT. Abstract data types such as
lists, stacks, queues, trees, tables, heaps, and priority queues form the
basis of our discussions.

Problem Solving
This book helps students learn to integrate problem-solving and programming abilities by emphasizing both the thought processes and the techniques that computer scientists use. Learning how a computer scientist
develops, analyzes, and implements a solution is just as important as
learning the mechanics of the algorithm; a cookbook approach to the
material is insufficient.
The presentation includes analytical techniques for the development
of solutions within the context of example problems. Abstraction, the
successive refinement of both algorithms and data structures, and recursion are used to design solutions to problems throughout the book.
C++ pointers and linked list processing are introduced early and used
in building data structures. The book also introduces at an elementary
level the order-of-magnitude analysis of algorithms. This approach allows

the consideration—first at an informal level, then more quantitatively—
of the advantages and disadvantages of array-based and pointer-based
data structures. An emphasis on the trade-offs among potential solutions
and implementations is a central problem-solving theme.
Finally, programming style, documentation including preconditions
and postconditions, debugging aids, and loop invariants are important


Preface C++ 3e.fm Page vii Monday, July 16, 2001 1:44 PM

PREFACE

vii

parts of the problem-solving methodology used to implement and verify
solutions. These topics are covered throughout the book.

Applications
Classic application areas arise in the context of the major topics of this
book. For example, the binary search, quicksort, and mergesort algorithms provide important applications of recursion and introduce orderof-magnitude analysis. Such topics as balanced search trees, hashing, and
file indexing continue the discussion of searching. Searching and sorting
are considered again in the context of external files.
Algorithms for recognizing and evaluating algebraic expressions are
first introduced in the context of recursion and are considered again
later as an application of stacks. Other applications include, for example, the Eight Queens problem as an example of backtracking, eventdriven simulation as an application of queues, and graph searching and
traversals as other important applications of stacks and queues.

NEW AND REVISED MATERIAL
This edition retains the underlying approach and philosophy of the
second edition. We present data abstraction and programming both as

general concepts and in the context of C++. No sentence, example, margin
note, or figure escaped scrutiny during the preparation of this edition.
Many changes, additions, and deletions to the text, figures, and margin
notes were made to update and clarify the presentation. All C++ programs
were revised to reflect recent changes to the language.
Although we revised all chapters and appendixes, here is a list of the
major changes to the book.


The specifications of all ADT operations now use UML notation
to clarify the purpose and data type of their parameters.



Chapter 1 enhances its coverage of object-oriented design and
introduces the Unified Modeling Language (UML). We have
revised our naming conventions for C++ identifiers to be consistent with common practice. This change will help students who
either know Java or will learn Java later.



Chapter 3 briefly introduces inheritance after its presentation of
C++ classes. Also covered are the C++ namespace and exceptions. Although the ADTs in this chapter return boolean flags to
indicate error conditions, subsequent chapters use exceptions.



Chapter 4 includes a new section about the C++ Standard Template Library (STL). Class templates, containers, and iterators are
introduced. More extensive coverage of these topics is deferred
until Chapter 8. Chapter 4 also considers the STL class list.



Preface C++ 3e.fm Page viii Monday, July 16, 2001 1:44 PM

viii

OVERVIEW

Other classes in the STL are presented throughout the book, and
you can either skip or postpone covering them, as desired.


Chapter 6 includes the STL class stack.



Chapter 7 includes the STL class queue.



Chapter 8 provides a deeper discussion of inheritance and class
templates than was given earlier. Also covered are C++ friends
and iterators.



Appendix A is an updated review of C++ that now includes coverage of exceptions. Appendix C is an updated list of C++ headers. Appendix E is new and contains the STL classes list, stack,
and queue.

OVERVIEW

The pedagogical features and organization of this book were carefully
designed to facilitate learning and to allow instructors to tailor the material easily to a particular course.

Pedagogical Features
This book contains the following features that help students not only
during their first reading of the material, but also during subsequent
review:


Chapter outlines and previews.



Boxes listing key concepts in the material.



Margin notes.



Chapter summaries.



Cautionary warnings about common errors and misconceptions.



Self-test exercises with answers.




Chapter exercises and programming problems. The most challenging exercises are labeled with asterisks. Answers to the exercises appear in the Instructor’s Resource Manual.



Specifications for all major ADTs in both English and pseudocode,
as well as in UML notation.



C++ class definitions for all major ADTs.



Examples that illustrate the role of classes and ADTs in the
problem-solving process.



Appendixes, including a review of C++.



Glossary of terms.


Preface C++ 3e.fm Page ix Monday, July 16, 2001 1:44 PM


PREFACE

Organization
The chapters in this book are organized into two parts. In most cases,
Chapters 1 through 11 will form the core of a one-semester course.
Chapters 1 or 2 might be review material for your students. The coverage
given to Chapters 11 through 14 will depend on the role the course
plays in your curriculum. More detailed suggestions for using this book
with different courses appear in the Instructor’s Resource Manual.
Part I: Problem-Solving Techniques. The first chapter in Part I emphasizes the major issues in programming and software engineering. A new
introduction to the Unified Modeling Language (UML) is given here. The
next chapter discusses recursion for those students who have had little
exposure to this important topic. The ability to think recursively is one
of the most useful skills that a computer scientist can possess and is
often of great value in helping one to better understand the nature of a
problem. Recursion is discussed extensively in this chapter and again in
Chapter 5 and is used throughout the book. Included examples range
from simple recursive definitions to recursive algorithms for language
recognition, searching, and sorting.
Chapter 3 covers data abstraction and abstract data types (ADTs) in
detail. After a discussion of the specification and use of an ADT, the
chapter presents C++ classes and uses them to implement ADTs. This
chapter includes a brief introduction to inheritance, the C++ namespace,
and exceptions. Chapter 4 presents additional implementation tools in
its discussion of C++ pointer variables and linked lists. This chapter also
introduces class templates, the C++ Standard Template Library (STL),
containers, and iterators.
You can choose among the topics in Part I according to the background of your students and cover these topics in several orders.
Part II: Problem Solving with Abstract Data Types. Part II continues to
explore data abstraction as a problem-solving technique. Basic abstract

data types such as the stack, queue, binary tree, binary search tree, table,
heap, and priority queue are first specified and then implemented as
classes. The ADTs are used in examples and their implementations are
compared.
Chapter 8 extends the coverage of C++ classes by further developing
inheritance, class templates, and iterators. The chapter then introduces
virtual functions and friends. Chapter 9 formalizes the earlier discussions
of an algorithm’s efficiency by introducing order-of-magnitude analysis
and Big O notation. The chapter examines the efficiency of several
searching and sorting algorithms, including the recursive mergesort and
quicksort.
Part II also includes advanced topics—such as balanced search trees
(2-3, 2-3-4, red-black, and AVL trees) and hashing—that are examined as
table implementations. These implementations are analyzed to determine the table operations that each supports best.

ix


Preface C++ 3e.fm Page x Monday, July 16, 2001 1:44 PM

x

TALK TO US

Finally, data storage in external direct access files is considered.
Mergesort is modified to sort such data, and external hashing and B-tree
indexes are used to search it. These searching algorithms are generalizations of the internal hashing schemes and 2-3 trees already developed.

SUPPLEMENTARY MATERIALS
The following supplementary materials are available online to assist

instructors and students.


Source code. All of the C++ classes, functions, and programs that
appear in the book are available to readers.



Errata. We have tried not to make mistakes, but mistakes are
inevitable. A list of detected errors is available and updated as
necessary. You are invited to contribute your finds.



The source code and errata list are available at the URL
www.aw.com/cssupport



Instructor’s Resource Manual. Solutions to the exercises at the end of
the chapters as well as teaching notes and suggestions are available only to instructors through your Addison-Wesley sales representative.

TALK TO US
Walls and Mirrors continues to evolve. Your comments, suggestions,
and corrections will be greatly appreciated. You can contact us directly
by e-mail at


or



or through the publisher:
Computer Science Editorial Office
Addison-Wesley
75 Arlington Street
Boston, MA 02116


Preface C++ 3e.fm Page xi Monday, July 16, 2001 1:44 PM

PREFACE

ACKNOWLEDGMENTS
The suggestions from outstanding reviewers contributed greatly to this
book’s present form. In alphabetical order, they are
Vicki H. Allan — Utah State University
Don Bailey — Carleton University
Sebastian Elbaum — University of Nebraska, Lincoln
Matthew Evett — Eastern Michigan University
Susan Gauch — University of Kansas
Martin Granier — Western Washington University
Judy Hankins — Middle Tennessee State University
Sr. Joan Harnett — Manhattan College
Tom Irby — University of North Texas
Edwin J. Kay — Lehigh University
David Naffin — Fullerton College
Paul Nagin — Hofstra University
Bina Ramamurthy — SUNY at Buffalo
Dwight Tunistra
Karen VanHouten — University of Idaho

Kathie Yerion — Gonzaga University
We especially thank the people who produced this book. Our editors
at Addison-Wesley, Susan Hartman Sullivan and Katherine Harutunian,
provided invaluable guidance and assistance. This book would not have
been printed on time without our project manager, Daniel Rausch from
Argosy Publishing. Thank you, Daniel, for keeping us on schedule.
Many thanks to our copy editor, Rebecca Pepper, for smoothing out
our rough edges. Also, Pat Mahtani, Michael Hirsch, Gina Hagen, Jarrod
Gibbons, Michelle Renda, and Joe Vetere contributed their expertise and
care throughout the production of this book.
Many other wonderful people have contributed in various ways.
They are Doug McCreadie, Michael Hayden, Sarah Hayden, Andrew
Hayden, Albert Prichard, Ted Emmott, Maybeth Conway, Lorraine
Berube, Marge White, James Kowalski, Gerard Baudet, Joan Peckham, Ed
Lamagna, Victor Fay-Wolfe, Bala Ravikumar, Lisa DiPippo, Jean-Yves

xi


Preface C++ 3e.fm Page xii Monday, July 16, 2001 1:44 PM

xii

ACKNOWLEDGMENTS

Hervé, Hal Records, Wally Wood, Elaine Lavallee, Ken Sousa, Sally
Lawrence, Lianne Dunn, Gail Armstrong, Tom Manning, Jim Labonte,
Jim Abreu, and Bill Harding.
Numerous other people provided input for the previous editions of
Walls and Mirrors at various stages of its development. All of their comments were useful and greatly appreciated. In alphabetical order, they are

Karl Abrahamson, Stephen Alberg, Ronald Alferez, Vicki Allan, Jihad
Almahayni, James Ames, Claude W. Anderson, Andrew Azzinaro, Tony
Baiching, Don Bailey, N. Dwight Barnette, Jack Beidler, Wolfgang W.
Bein, Sto Bell, David Berard, John Black, Richard Botting, Wolfin Brumley, Philip Carrigan, Stephen Clamage, Michael Clancy, David Clayton,
Michael Cleron, Chris Constantino, Shaun Cooper, Charles Denault,
Vincent J. DiPippo, Suzanne Dorney, Colleen Dunn, Carl Eckberg, Karla
Steinbrugge Fant, Jean Foltz, Susan Gauch, Marguerite Hafen , Randy
Hale, George Hamer, Judy Hankins, Lisa Hellerstein, Mary Lou Hines,
Jack Hodges, Stephanie Horoschak, Lily Hou, John Hubbard, Kris Jensen,
Thomas Judson, Laura Kenney, Roger King, Ladislav Kohout, Jim
LaBonte, Jean Lake, Janusz Laski, Cathie LeBlanc, Urban LeJeune, John
M. Linebarger, Ken Lord, Paul Luker, Manisha Mande, Pierre-Arnoul de
Marneffe, John Marsaglia, Jane Wallace Mayo, Mark McCormick, Dan
McCracken, Vivian McDougal, Shirley McGuire, Sue Medeiros, Jim
Miller, Guy Mills, Rameen Mohammadi, Cleve Moler, Narayan Murthy,
Paul Nagin, Rayno Niemi, John O’Donnell, Andrew Oldroyd, Larry
Olsen, Raymond L. Paden, Roy Pargas, Brenda C. Parker, Thaddeus F.
Pawlicki, Keith Pierce, Lucasz Pruski, George B. Purdy, David Radford,
Steve Ratering, Stuart Regis, J. D. Robertson, Robert A. Rossi, John Rowe,
Michael E. Rupp, Sharon Salveter, Charles Saxon, Chandra Sekharan,
Linda Shapiro, Yujian Sheng, Mary Shields, Ronnie Smith, Carl Spicola,
Richard Snodgrass, Neil Snyder, Chris Spannabel, Paul Spirakis, Clinton
Staley, Matt Stallman, Mark Stehlick, Benjamin T. Schomp, Harriet
Taylor, David Teague, David Tetreault, John Turner, Susan Wallace, James
E. Warren, Jerry Weltman, Nancy Wiegand, Howard Williams, Brad
Wilson, James Wirth, Salih Yurttas, and Alan Zaring.
Thank you all.
F. M. C.
J. J. P.

















×