DATA ABSTRACTION AND STRUCTURES USING C++ This page intentionally left blank DATA ABSTRACTION AND STRUCTURES USING C++ Mark R. Headington University of Wisconsin-La Crosse David D. Rdey University of Wisconsin-La Cmse JONES AND BARTLETT PUBLISHERS Sudbury, Massachusetts BOSTON TORONTO LONDON SINGAPORE World Headquarters Jones and Bartlett Publishers 40 Tall Pine Drive Sudbury, MA 01776 978-443-5000 [email protected] www.jbpub.com Jones and Bartlett Publishers Canada 2 100 Bloor St. West Suite 6-272 Toronto, ON M6S 5A5 CANADA Jones and Bartlett Publishers International Barb House, Barb Mews London W6 7PA UK Copyright 01997 by Jones and Hartlett Publishers, 01994 Iy D. C. Heath and Company.. All rights reserved. No part ofthis publication may lie rcyroduced or transmitted in my tbrm or by any means, electronic or mechanical, including photocopy, recording, or any infi)rmation stor- age or retrieval system, without permission in writing from the publisher. + + + + Trademark Acknowledgments: Turbo C and Dorland C are registered trademarks of Borland International, Inc. Cover: James Crable “Enipire State Plaza, Albany, NY.” Reproduced courtesy of’J.J Brookings Gallery, San Jose,California. Library of Congress Catalog Number: 92 -7 5664 ISBN 0-7637-029 5-1 Printed in the United States of America 03 02 01 00 99 10 9 To Anne, Brady, and Kari, who haven’t seen much of me in the last three years. LM. R. H. To my wife, Sandi, and children, Derek and Kasandra. D. D. R. Data Abstraction and Structures Using C+ + Program Disk Jones and Bartlett Publishers offers free to students and instructors a program disk with all the + +. complete programs found in Data Abstraction and Structures Using C The program disk is available through the Jones and Bartlett World Wide Web site on the Internet." Download Instructions 1. Connect to the Jones and Bartlett student diskette home page (http://www.jbpub.com/di sks/). 2. Choose Data Abstraction and Structures Using C + +. 3. Follow the instructions for downloading and saving the Data Abstruction clnd Structiires Using C+ + data disk. 4. If you need assistance downloading a Jones and Bartlett student diskette, please send e-mail to help@?jbpub.com. "Downloading the Data Abstruction and Stnictures Using C+ + program disk via the Jones and Bartlett home page requires access to the Internet and a World Wide Web browser such as Netscape Navigator or Microsoft Internet Explorer. Instructors at schools without Internet access may call 1-800-832-0034 and request a copy of the program disk. Jones and Bartlett gralits adopters of Dacu Abstrclction and Stnictures Using C + + the right to duplicate copies of the program disk or to store the files on any stand-alone computer or network. PREFACE Software design has evolved dramatically in the past decade. In the 1970s, structured programming changed the way that programmers viewed control structures. More recently, the concepts of data abstraction and object-oriented software development have affected the way that programmers view data structures. Introductory computer science courses must embrace these tech- niques if they are to provide an adequate foundation in contemporary sofixare development. About This Book Data Abstraction and Stmctzwes Using C++ is designed for a second course in computer science fundamentals, consistent with the AChI guidelines for the CS2 course (August 1985). The book is also compatible with a Cl02 course in ACMAEEE’s Computing Curricula 1991, covering elements from the follow- ing knowledge units: ALl through AL4, AL6, SE1 through SES, and PL3 through PL6. The text’s emphasis is on abstraction and its role in software development. The first chapter reviews (and extends) the principles of control abstraction that students learn in the first computer science course. Chapter 2 serves as a bridge between control abstraction and data abstraction, introducing separate compilation and the use of header files to separate specification from im- plementation. From Chapter 3 forward, the focus is on data abstraction- the separation of a data type’s abstract properties from the details of its implementation, Although this text is intended primaril?. for the second computer science course, it also can be used in a sophomore/junior-level data structures course (CS7) when supplemented with more advanced material on data structures. Prerequisite Knowledge This text assumes that the student has completed a first course in computer science that examined the tools and techniques of structured programming in a modern language-Pascal, iuodula-2, Ada, C, C++, or Scheme, for example. Prior knowledge of C++is not required. Ij’e have provided three appendices to aid in the transition from another language to C++,and it is in these appen- dices that many readers will want to begin. Appendix A (Introductory VI I Comparison of Pascal and C++)is a starting point for students with no background in C or C++. Appendix B (Introductory Comparison of C: and C++)serves as a brief introduction to C++for those already familiar with C. Appendix C (C++S tream I/O) coiripletes the introduction to C++and should be read after either Appendix A or Appendix B. The Use of C++ This book originated in response to the rapid adoption of C and C++in the commercial arena and in academic curricula. A number of educators have expressed concern about the use of these languages, faulting them for being too unconstrained and for encouraging a tricky, unreadable style of program- ming. But it is well known that one can write bad code in any language. This text’s approach is to present the language and program examples in a very dis- ciplined manner so that potentially confusing coding practices (such as abun- dant use of side effects) do not obscure the primary subject: data abstraction and data structures. The choice of C++over C was a very clear one. C++extends the C lan- guage to provide linguistic support for information hiding and abstract data types as well as for object-oriented programming (OOP). Regarding the lat- ter, we have placed the material on object-oriented design and OOP in Chapters 10 and 11. We believe that these techniques are better appreciated, even nahiral, after a thorough grounding in the design and implenientation of abstract data types. However, instructors wanting an earlier introduction to these topics will find tlexibiliq in the chapter organization. Distinguishing Features Separation of Data Structure Definitions from Their Implementations Students often confuse the abstract properties of a data structure with its implementation. hlost textbooks interweave implementation issues M ith dis- cussion of a data structure’s characteristics and applications. A common result is that upper-division students almost invariably associate linked lists with dynamic data and machine-level pointers. They are surprised to learn (or remember) that alternative data representations exist and that an arrajr of records using subscripts as “pointers” is in fact the preferred alternative for time-critical applications such as operating system software. In this test, Part I (Abstraction) includes a chapter introducing well-known data structures as abstract data types. Their characteristics and applications are discussed without reference to possible implementations so students will be less apt to confuse abstract properties with implementation. Part I1 (Tools for Irnplementation) develops techniques and algorithms that culminate in alter- native iinplernentations of the major data structures. Software Design Features To highlight the importance of good program design techniques, we have sprinkled many separate Designiiig uyith Wisdom, Using the Lnngrrnge, and Pzrrsuing Coi7-ect7iess sections throughout the text. ilidditionally, nearly ever). chapter has a special section entitled A Studjt in Desipi, an in-depth exposition of the development of a particular program, module, or abstract data qpe. Development of Tools and Techniques Every programmer needs a toolbox. This book includes numerous tools for the developing software engineer. Ariiong these tools are modules, abstract data types, module and class interface diagrams, assertions, recursion, table- driven code, primitive models for data, Backus-Naur Form, performance analysis, and many widely used data structures with associated algorithms. Assertions We use assertions in the form of program comments throughout the text. Functions are documented with preconditions and postconditions to define precisely the contract between the caller and the called routine. Loop invari- ants document the semantics of loops. In addition, class invariants are intro- duced as a method of defining the expected behavior of class objects. Appendix D summarizes the assertion notation used in the book. Problem Specification Form We use a six-part problem specification form to emphasize the importance of precise and complete specifications. This form consists of the title, problem description, input specifications, output specifications, error handling, and a sample program execution. We frequently present larger programming exam- ples and programming project assignments in this form. Terminology The computer scientist needs an extensive vocabulary. To build and reinforce this essential vocabulary, we accent important terms in boldface in the text and list them in a Key Terms section at the end of every chapter. Extensive Selection of Examples Most students learn by example. The text therefore includes many care- fully selected examples illustrating program design and C++code. Il’e have
Description: