Sequential Program Structures
Total Page:16
File Type:pdf, Size:1020Kb
Jim Welsh, John Elder and David Bustard Sequential Program Structures C. A. R. HOARE SERIES EDITOR SEQUENTIAL PROGRAM STRUCTURES St. Olaf College JUL 3 1984 Science Library Prentice-Hall International Series in Computer Science C. A. R. Hoare, Series Editor Published backhouse, r. c.. Syntax of Programming Languages: Theory and Practice de barker, J. w„ Mathematical Theory of Program Correctness bjorner, D, and jones, c„ Formal Specification and Software Development clark, k. L. and McCabe, f. g„ micro-PROLOG: Programming in Logic dromey, r. G., How to Solve it by Computer duncan, f.. Microprocessor Programming and Software Development goldschlager, l. and lister, a., Computer Science: A Modern Introduction henderson, p.. Functional Programming: Application and Implementation inmos ltd., The Occam Programming Manual jackson, m. a., System Development jones, c, b., Software Development: A Rigorous Approach Joseph, m., prasad, v. r., and natarajan, n„ A Multiprocessor Operating System MacCALLUM, I. Pascal for the Apple Reynolds, J. c„ The Craft of Programming tennent, r. d., Principles of Programming Languages welsh, j., and elder, j.. Introduction to Pascal, 2nd Edition welsh, J., elder, j. and bustard, d., Sequential Program Structures welsh, j., and McKEag, m.. Structured System Programming SEQUENTIAL PROGRAM STRUCTURES JIM WELSH University of Queensland, Australia JOHN ELDER Queen's University of Belfast and DAVID BUSTARD Queen's University of Belfast St. Olaf College JUL 3 1984 Science Library Prentice/Hall International ENGLEWOOD CLIFFS, NEW JERSEY LONDON NEW DELHI RIO DE JANEIRO SINGAPORE SYDNEY TOKYO TORONTO WELLINGTON Library of Congress Cataloging in Publication Data Welsh, Jim, 1943- Sequential program structures. Bibliography: p. Includes index. 1. Modular programming. 2. PASCAL (Computer program language) I. Elder, John, 1949- II. Bustard, Dave, 1949- III. Title. QA76.6.W4639 1984 001.64’2 83-19070 ISBN 0-13-806837-2 ISBN 0-13-806828-3 (pbk.) British Library Cataloguing in Publication Data Welsh, Jim Sequential program structures 1. PASCAL (Computer program language) I. Title. II. Elder, John, 1949- . III. Bustard, Dave 001.64’24 QA76.73.P2 ISBN 0-13-806837-2 ISBN 0-13-806828-3 (pbk.) © 1984 by Prentice-Hall International, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior permission of Prentice-Hall International, Inc. For permission within the United States contact Prentice-Hall Inc., Englewood Cliffs, New Jersey 07632. ISBN 0-13-A0l=,Aefl-3 {PAPER} ISBN 0-13-ADbA37-E {CASE} PRENTICE-HALL INTERNATIONAL INC., London PRENTICE-HALL OF AUSTRALIA PTY., LTD., Sydney PRENTICE-HALL CANADA, INC., Toronto PRENTICE-HALL OF INDIA PRIVATE LIMITED, New Delhi PRENTICE-HALL OF JAPAN, INC., Tokyo PRENTICE-HALL OF SOUTHEAST ASIA PTE., LTD., Singapore PRENTICE-HALL INC., Englewood Cliffs, New Jersey PRENTICE-HALL DO BRASIL LTDA., Rio de Janeiro WHITEHALL BOOKS LIMITED, Wellington, New Zealand Printed in the United States of America 10 987654321 Contents Preface xi 1. Goals in programming 1 Correctness 1 Flexibility 2 Portability 2 Adaptability 3 Utility 4 Clarity 5 Efficiency 5 Summary 6 2. Stepwise refinement 8 Specification, design and coding 8 Abstraction 9 Stepwise refinement 10 A first example 10 A second example 11 The general case 13 A further example 14 Procedures 18 Program revision 22 Language reinforcement 23 Summary 26 Exercises 26 v CONTENTS The abstractions in Pascal 28 The concept of type 29 Scalar types 30 The type Boolean 30 Enumerations 31 Ordered types 32 The types Integer and Char 33 The type Real 34 Subranges 34 Structured types 35 Records 35 Variant records 37 Arrays 38 Files 40 Sets 41 A case study 42 Summary 48 Exercises 49 Modular programming 50 Program-defined abstractions 50 A list of occurrences module 56 A wordlist module 58 A module for word input 60 Module refinement of programs 62 A module library 64 Modular testing 65 Summary 67 Exercises 68 Modular programming in Pascal plus 69 Envelopes 69 Multiple instances of envelopes 72 Parameterizing envelopes 76 Retrieval parameters 76 Instance parameters 79 Instances as parameters 80 Retrieving envelopes as modules 80 The concordance program in Pascal plus 81 Summary 90 Exercises 90 ITS Abstract data types 92 A first example: integerset 92 Using integerset 95 Implementing integerset 97 Generalizing the integerset abstract type 108 Dealing with errors 110 Structure traversing operators 113 A frequency histogram 117 Summary 121 Exercises 121 Stacks and queues 124 Sequences and their subclassification 124 Defining a stack 125 A simple use of stacks 128 Contiguous representation of stacks 129 Chained representation of stacks 132 Blocked representation of stacks 136 Storage organization and recursive block-structured languages 139 Defining a queue 145 Using queues 147 Contiguous representation of queues 149 Chained representation of queues 153 Blocked representation of queues 156 Defining a deque 158 Representation of deques 158 Case study : an encoded message system 160 Summary 168 Exercises 170 Lists 170 Defining a list abstraction 171 The concordance program revisited 176 Representation of lists 179 A contiguous representation 179 A chained list representation 183 Case study : a simple text editor 189 Sorting lists 198 Implementing the sort operator 200 Quicksort 202 Sorting a chained list 206 Maintaining sorted lists 209 An ordered list abstraction 211 Summary 214 Exercises 215 CONTENTS viii 9. Packages : an alternative abstraction mechanism 216 Packages 217 Hierarchic abstractions 223 The concordance program (yet again!) 224 Summary 228 Exercises 229 10. Trees 232 Tree representation of ordered lists 237 A general binary tree abstraction 244 Case study : an animal guessing program 254 Contiguous tree representations 263 Summary 264 Exercises 265 11. Graphs 267 Directed graphs 267 A directed graph abstraction 269 Graph manipulation 272 Graph input 272 Path analysis 275 Topological sorting 282 A revised graph abstraction 289 Contiguous representation of graphs 292 Chained representation of graphs 306 Node reference security 311 Summary 315 Exercises 317 12. Sparse data structures 318 A sparse array abstraction 322 Representation of sparse structures 325 Sequential representation 327 Hash table representation 329 An interactive concordance program 338 Multi-dimensional sparse arrays 342 Summary 345 Exercises 346 Appendix 1 360 1. Blocks 360 2. Procedure and function declarations 361 3. Envelope definitions 363 4. Instance declarations 364 5. Instances as parameters 366 6. Envelope interfaces 367 7. Simple statements 368 8. Activations, instantiations and inner statements 369 CONTENTS IX Appendix 2 370 Transcribing envelope moduls 370 Transcription of envelopes and instance declarations 374 Dealing with envelope parameters 376 Summary 378 Bibliography 379 Index 381 ' - Preface The topics covered in this book are the structures, both in program and in data, that arise in substantial computer programs of a sequential nature—those which imply no concurrency in their execution. In practical terms, it provides an introduction to the classification and organisation of data structures, to their encapsulation as abstract data types, and to modular (sequential) programming in general. The book begins by reviewing the objectives that are relevant to the construction of all computer programs. The conceptual tools and strategies that we must use to achieve these objectives are then considered. Subse¬ quent chapters analyse the strengths and limitations of simple programming languages, such as Pascal, in supporting these conceptual strategies. From the limitations identified, the need for modular programming is established, together with a notation for expressing the modular program structures concerned. In this way Chapters 1 to 5 introduce a methodology for the design, implementation and testing of programs with a well-defined modular structure, and a notation for expressing that structure within the program text. Chapter 6 then introduces the concept of abstract data types, using the concept of a program module to maintain a clear separation of the definition and use of an abstract type from the means chosen for its implementation. Within this general model, subsequent chapters introduce stacks, queues, lists, trees, graphs and tables as abstract data types. For each type a specifica¬ tion is developed, an external representation is defined, its use in practical programming context is illustrated, and major methods for its internal representation are described. The representations described are those suit¬ able for use in primary stage only, but a final chapter outlines how these representations can be saved in, and retrieved from, secondary storage when necessary. XI xii PREFACE Throughout the book stepwise refinement is used as the means of presenting the design and implementation of programs and program mod¬ ules, but in most major cases detailed listings of the final modules that result are also given. For modules that implement basic abstract data types in particular, thesp listings provide a valuable reference library of implementa¬ tion techniques, or indeed of directly usable code, for many programming applications. The exercises given at the end of each chapter enhance this aspect, by suggesting modifications and applications of the listed modules as well as original design and implementation