Data Structures in C Using The Standard
Data Structures in C Using The Standard
Timothy Budd
Oregon State University
^ ADDISON-WESLEY
Preface v
I FUNDAMENTAL TOOLS 1
1 Fundamentals 3
1.1 The Study of Data Structures 3
1.1.1 The STL 4
1.2 Language Fundamentals 4
1.2.1 Comments 5
1.2.2 Constants 5
1.2.3 Basic Data Types and Declaration Statements
1.2.4 Expressions and Assignment Statements 7
1.2.5 Input and Output 9
1.2.6 Pointer Values 9
1.2.7 Conditional Statements 10
1.2.8 Loops 11
1.2.9 Arrays 12
1.2.10 Structures 14
1.2.11 Functions 14
1.2.12 The Function main 16
1.2.13 Include Files 17
1.2.14 Binding Times 17
1.3 Chapter Summary 19
Further Reading 20
Study Questions & Exercises 20
2 Classes and Object-Oriented Programming 23
Chapter Overview 23
2.1 The Card Game WAR 24
2.2 The Class Card 24
2.3 The Class Deck 29
2.3.1 In-Line Function Definitions 34
CONTENTS
Further Reading 83
Study Questions Si Exercises 84
5 Increasing Confidence in Correctness 87
Chapter Overview 87
5.1 Program Proofs 88
5.1.1 Invariants 88
5.1.2 Analyzing Loops 90
5.1.3 Asserting the Outcome is Correct 93
5.1.4 Progress Toward an Objective 94
5.1.5 Manipulating Unnamed Quantities 95
5.1.6 Function Calls 96
5.1.7 Recursive Algorithms 9 7
5.2 Program Testing 99
5.3 Chapter Summary 101
Further Reading 101
Study Questions Si Exercises 101
THE STANDARD CONTAINERS 105
6 The Standard Library Container Classes 107
Chapter Overview 107
6.1 Container Classes 108
6.1.1 Vectors 108
6.1.2 Strings 110
6.1.3 Lists 110
6.1.4 Double-Ended Queues 110
6.1.5 Stacks and Queues 111
6.1.6 Sets 112
6.1.7 Priority Queues 112
6.1.8 Maps (Dictionaries) 113
6.2 Selecting a Container 113
6.3 Iterators 115
Further Reading 119
6.4 Chapter Summary 119
Study Questions Si Exercises 119
7 The string Data Type 121
Chapter Overview 121
XVI CONTENTS
14 Searching 327
Chapter Overview 327
14.1 Divide and Conquer 328
14.1.1 Binary Search 329
14.1.2 Application: Root Finding 331
14.2 Ordered Vectors 332
14.3 Balanced Binary Search Trees 336
14.4 Application: Tree Sort 346
14.5 Finding the Nth Largest 347
14.5.1 Application: Quick Sort 352
14.6 Chapter Summary 355
Further Reading 356
CONTENTS
19 Graphs 445
Chapter Overview 445
19.1 The Graph Data Abstraction 445
19.2 Adjacency Matrix Representation 447
19.2.1 Warshall's Algorithm 448
19.3 Edge List Representation 451
xxii CONTENTS
IV APPENDICES 497
D. 12 Friends 530
D. 13 Derived Classes: Inheritance 531
D. 14 Templates 531
D. 15 Polymorphism: Virtual Functions 531
Bibliography 533
Index 539