C++ Reserved Keywords C++ reserves and gives predefi ned meanings to the following keywords. You may not redefi ne key- words or use them for other purposes. Keywords that appear in color are new since C++11. alignas decitype namespace struct alignof default new switch and delete noexcept template and_eq do not this asm double not_eq thread_local auto dynamic_cast nullptr throw bitand else operator true bitor enum or try bool explicit or_eq typedef break export private typeid case extern protected typename catch false public union char fl oat register unsigned char16_t for reinterpret_cast using char32_t friend return virtual class goto short void compl if signed volatile const inline sizeof wchar_t const_cast int static while constexpr long static_assert xor continue mutable static_cast xor_eq Operator Meaning Associativity Usage * multiply left expr * expr / divide left expr / expr % modulo left expr % expr + add left expr + expr - subtract left expr - expr << bitwise shift left‡ left expr << expr > > bitwise shift right‡ left expr >> expr < less than left expr < expr <= less than or equal to left expr <= expr > greater than left expr > expr >= greater than or equal to left expr >= expr = = equal left expr == expr ! = not equal left expr != expr & bitwise AND left expr & expr ^ bitwise EXCLUSIVE OR left expr ^ expr | bitwise OR left expr | expr && logical AND left expr && expr | | logical OR left expr || expr ? : conditional left expr ? expr : expr = assign left lvalue = expr *= multiply and assign left lvalue *= expr /= divide and assign left lvalue /= expr %= modulo and assign left lvalue %= expr += add and assign left lvalue += expr -= subtract and assign left lvalue -= expr <<= shift left and assign left lvalue <<= expr >>= shift right and assign left lvalue >>= expr &= AND and assign left lvalue &= expr |= OR and assign left lvalue |= expr ^= EXCLUSIVE OR and assign left lvalue ^= expr , comma left expr, expr ‡ Typically overloaded for I/O Data Abstraction & Problem Solving with C++ WALLS AND MIRRORS SIXTH EDITION Frank M. Carrano University of Rhode Island Timothy Henry University of Rhode Island Vice President and Editorial Director, ECS: Marcia Cover Designer: Liz Harasymczuk J. Horton Permissions Supervisor: Michael Joyce Executive Editor: Tracy Johnson Permissions Administrator: Jenell Forschler Associate Editor: Carole Snyder Director, Image Asset Services: Annie Atherton Director of Marketing: Christy Lesko Manager, Visual Research: Karen Sanatar Marketing Manager: Yez Alayan Cover Art: Shutterstock/SVLuma Marketing Assistant: Jon Bryant Media Project Manager: Renata Butera Director of Production: Erin Gregg Full-Service Project Management: Rose Kernan Managing Editor: Jeff Holcomb Composition: Cenveo Publisher Services / Nesbitt Associate Managing Editor: Robert Engelhardt Printer/Binder: Edwards Brothers Manufacturing Buyer: Lisa McDowell Cover Printer: Lehigh-Phoenix Color/Hagerstown Art Director: Anthony Gemmellaro Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on the appropriate page within text. Copyright © 2013, 2007, 2005 Pearson Education, Inc., publishing as Addison-Wesley. All rights reserved. Printed 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, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to 201-236-3290. Many of the designations by manufacturers and sellers 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. Library of Congress Cataloging-in-Publication Data on fi le. 10 9 8 7 6 5 4 3 2 1 ISBN 10: 0-13-292372-6 ISBN 13: 978-0-13-292372-9 W e elcome to the sixth edition ofData Abstraction & Problem Solving with C++: Walls and Mirrors . Since m the publication of the fi rst edition, we all have gained experience with teaching data abstraction in an object- oriented way using C++. This edition refl ects that experience and the many comments and suggestions received o from faculty and students alike. c I am happy to introduce Dr. Timothy Henry, my co-author and colleague at the University of Rhode Island. l Together, we have given this book a much needed revision and a new look. However, our goal remains to give e W students a superior foundation in data abstraction, object-oriented programming, and other modern problem- solving techniques. All C++ code has been rewritten with a focus on safe and secure programming practices. It also adheres to the C++11 standard. We hope that you enjoy reading this book. Like many others before you, you can learn—or teach—data structures in an effective and sustainable way. Talk to Us Walls and Mirrors continues to evolve. Your comments, suggestions, and corrections will be greatly appreciated. Here are a few ways to reach us: • E-mail: [email protected] • Facebook: www.facebook.com/makingitreal • Twitter: twitter.com/Frank_M_Carrano • Blog: frank-m-carrano.com/makingitreal iiiiii s T t he topics that we cover in this book deal with the various ways of organizing data so that a given application n can access and manipulate data in an effi cient way. These topics are fundamental to your future study of compu- e ter science, as they provide you with the foundation of knowledge required to create complex and reliable soft- d ware. Whether you are interested in designing video games or software for robotic-controlled surgery, the study of data structures is vital to your success. Even if you do not study all of the topics in this book now, you are u likely to encounter them later. We hope that you will enjoy reading the book, and that it will serve as a useful t reference tool for your future courses. S The walls and mirrors in the title represent two fundamental problem-solving techniques that appear o throughout the presentation. Data abstraction isolates and hides the implementation details of a module from the t rest of the program, much as a wall can isolate and hide you from your neighbor. Recursion is a repetitive tech- nique that solves a problem by solving exactly the same but smaller problems, much as images in facing mirrors e grow smaller with each refl ection. t o Please be sure to browse the rest of this preface to see the features that will help you in your studies. To help you learn and to review for exams, we have included such learning aids as video tutorials (VideoNotes), check- N point questions with answers, margin notes, programming tips, chapter summaries, and a glossary. As a help during programming, you will fi nd C++ reference material in the appendices and inside the covers. You should A review the list of this book’s features given later in this preface in the section “Features to Enhance Learning.” 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 fi rst time by consulting the appendices of this book. This book covers C++ classes and other relevant aspects of the language in new C++ Interludes that occur throughout the book be- tween certain chapters. These interludes do not assume that you already know their topics. We assume no experi- ence 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 de- scription of supplementary materials tells you how to obtain these fi les, as well as the VideoNotes and other on- line documents. iv Organization s t T n his book’s organization, sequencing, and pace of topic coverage make learning and teaching easier by focusing your attention on one concept at a time, by providing fl exibility in the order in which you can cover topics, and by e clearly distinguishing between the specifi cation and implementation of abstract data types, or ADTs. To accomplish t n these goals, we have organized the material into 21 chapters. Most chapters focus on either the specifi cation and use of an ADT or its various implementations. You can choose to cover the specifi cation of an ADT followed by its imple- o mentations, or you can treat the specifi cation and use of several ADTs before you consider any implementation issues. C The book’s organization makes it easy for you to choose the topic order that you prefer. f Table of Contents at a Glance o The following list shows the overall composition of the book. A further chapter-by-chapter description appears e later. Note that gray highlighted sections are available online. l b Chapter 1 Data Abstraction: The Walls a C++ Interlude 1 C++ Classes Chapter 2 Recursion: The Mirrors T Chapter 3 Array-Based Implementations C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation f Chapter 4 Link-Based Implementations e Chapter 5 Recursion as a Problem-Solving Technique i Chapter 6 Stacks r C++ Interlude 3 Exceptions B Chapter 7 Stack Implementations Chapter 8 Lists Chapter 9 List Implementations Chapter 10 Algorithm Effi ciency Chapter 11 Sorting Algorithms and Their Effi ciency C++ Interlude 4 Class Relationships and Reuse Chapter 12 Sorted Lists and Their Implementations Chapter 13 Queues and Priority Queues Chapter 14 Queue Implementations C++ Interlude 5 Overloaded Operators and Friend Access Chapter 15 Trees Chapter 16 Tree Implementations C++ Interlude 6 Iterators Chapter 17 Heaps Chapter 18 Dictionaries and Their Implementations Chapter 19 Balanced Search Trees Chapter 20 Graphs Chapter 21 Processing Data in External Storage C++ Interlude 7 The Standard Template Library Appendix A Review of C++ Fundamentals Appendix B Important Themes in Programming Appendix C The Unifi ed Modeling Language Appendix D The Software Life Cycle Appendix E Mathematical Induction Appendix F Algorithm Verifi cation Appendix G Files Appendix H C++ Header Files and Standard Functions Appendix I C++ Documentation Systems Appendix J ASCII Character Codes Appendix K C++ for Java Programmers Appendix L C++ for Python Programmers Index Glossary Answers to Checkpoint Questions v n What’s New? o T his edition of Walls and Mirrors is a signifi cant revision of the previous edition, yet remains committed to a i t pedagogical approach that makes the material accessible to students at the introductory level. Although i everything looks new, you will fi nd the coverage that you enjoyed in previous editions is still here. At a glance, d the book has more—but shorter—chapters, a second color, and new C++ Interludes. Let’s examine the details. E Organization. The book begins with a shorter C hapter 1 , so that it can focus on the specifi cation of abstract data s types (ADTs). After general discussions of problem solving, good programming practices, and ADTs, we spec- i ify a simple ADT—the bag. We defi ne the bag’s operations within a C++ template interface in a non-threatening h way. We have moved some sections from the original fi rst chapter to the appendices. t By introducing the bag as the fi rst ADT we consider, we make the diffi cult topic of linked data more acces- o sible to students. Adding or removing the fi rst node in a chain of linked nodes is the easiest task, and these simple t manipulations are the ones we need to use for a linked implementation of the bag. The next ADT that we con- sider is the stack, a more useful data container that has the same simple chain in one of its defi nitions. Moreover, w many students are already familiar with stacks. Later, the treatment of lists looks at the more involved operations e of adding and removing a node that lies between existing nodes of a chain. N The rest of the coverage will be familiar to previous users of Walls and Mirrors but often you will fi nd ADTs presented in two chapters rather than one. These chapters separate the discussions of specifi cation and imple- mentation. We will describe each chapter later in this preface. To summarize, this organization • Replaces Chapter 1 with an introduction to ADTs and template interfaces using the ADT bag. • P rovides a more focused introduction to array-based implementations and link-based implementations using the ADT bag. • M akes the topic of linked data more accessible to students by discussing it progressively as we introduce the ADTs bag, stack, and list. • Places greater emphasis on data abstraction as a problem solving tool. • Enhances the separation between specifi cation and implementation of basic ADTs by placing them in successive chapters. • Specifi es ADTs informally at fi rst, then in UML, and ultimately in C++ template interfaces. • Demonstrates safe and secure programming practices in completely revised code that adheres to the C++11 standard. • Covers the ADT heap in its own chapter. • Reorganizes the coverage of the ADT dictionary (formerly called the ADT table) into two chapters. C++ Interludes. The introduction of C++ classes and other aspects of C++ that we need for our presentation and were previously covered in Chapters 3 , 4 , and 8 of the previous edition are now featured in C++ Interludes. Seven of these “mini-chapters” appear throughout the book to cover relevant C++ topics as we need them. Note that these interludes separate the details of C++ from the discussion of data structures. VideoNotes. Online tutorials are a Pearson feature that provides visual and audio support to the presentation given throughout the book. They offer students another way to recap and reinforce key concepts. VideoNotes allow for self-paced instruction with easy navigation, including the ability to select, play, rewind, fast-forward, and stop within each video. Unique VideoNote icons appear throughout this book whenever a video is available for a particular concept or problem. A detailed list of the 49 VideoNotes for this text and their associated loca- tions in the book can be found on page xxiii . VideoNotes are free with the purchase of a new textbook. To pur- chase access to VideoNotes, please go to www.pearsonhighered.com/carrano vi Other features. Walls and Mirrors now has many new features that enhance its usefulness to both readers and n instructors. This edition o • A dds a second color to enhance the effectiveness of the illustrations, distinguish pseudocode from C++ i t code, and provide visual interest. i • Includes Notes and Programming Tips to emphasize key material and offer programming advice. d • Distinguishes major pieces of code in a new Listing element. E • R eplaces the self-test exercises at the ends of chapters with Checkpoint Questions placed throughout the chapters. s • Numbers sections and subsections for easy reference. i h • Includes transition guides from Python to C++ and Java to C++. t o t w e N vii
Description: