Table Of ContentIntroduction to
Data Abstraction, Algorithms
and Data Structures
++
With C and the STL
Fall 2016
Alexis Maciel
Department of Computer Science
Clarkson University
Copyright (cid:13)c 2016 Alexis Maciel
ii
Contents
Preface ix
1 Abstraction 1
1.1 A Pay Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.5 Modularity and Abstraction . . . . . . . . . . . . . . . . . . . . . . . . 20
2 Data Abstraction 25
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2 Classes to Enforce Data Abstraction . . . . . . . . . . . . . . . . . . . 28
2.3 Classes to Support Object-Oriented Programming . . . . . . . . . . 31
2.4 Constant Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.5 Inline Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.6 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.7 Get and Set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.8 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.9 Compiling Large Programs . . . . . . . . . . . . . . . . . . . . . . . . 67
iii
iv CONTENTS
2.10 The make Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
3 Strings and Streams 75
3.1 C Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.2 C++ Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.3 I/O Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.4 String Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
4 Error Checking 99
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
4.2 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4.3 Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
5 Vectors 117
5.1 A Simple File Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.2 Vectors in the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5.3 Design and Implementation of the File Viewer . . . . . . . . . . . . 125
5.4 Vectors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
6 Generic Algorithms 143
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
6.2 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
6.3 Iterator Types and Categories . . . . . . . . . . . . . . . . . . . . . . . 154
6.4 Vectors and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
6.5 Algorithms in the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
6.6 Implementing Generic Algorithms . . . . . . . . . . . . . . . . . . . . 167
6.7 Initializer Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
6.8 Functions as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 174
CONTENTS v
6.9 Function Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
7 Linked Lists 185
7.1 A Simple Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
7.2 Vector Version of the Text Editor . . . . . . . . . . . . . . . . . . . . . 186
7.3 Vectors and Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 199
7.4 Linked Lists in the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
7.5 List Version of the Text Editor . . . . . . . . . . . . . . . . . . . . . . . 207
8 Maps 209
8.1 A Phone Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
8.2 Maps in the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
8.3 Design and Implementation of the Phone Book . . . . . . . . . . . . 220
9 Object-Oriented Design 231
9.1 The Software Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . 231
9.2 The Software Development Process . . . . . . . . . . . . . . . . . . . 233
9.3 Specification, Design and Implementation . . . . . . . . . . . . . . . 236
10 Dynamically Allocated Arrays 245
10.1 The Size of Ordinary Arrays . . . . . . . . . . . . . . . . . . . . . . . . 245
10.2 The Dynamic Allocation of Arrays . . . . . . . . . . . . . . . . . . . . 247
10.3 Programming with Dynamically Allocated Arrays . . . . . . . . . . 249
11 Implementation of Vectors 259
11.1 A Basic Class of Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . 259
11.2 Iterators, Insert and Erase . . . . . . . . . . . . . . . . . . . . . . . . . 265
11.3 Destroying and Copying Vectors . . . . . . . . . . . . . . . . . . . . . 269
11.4 Growing and Shrinking Vectors Efficiently . . . . . . . . . . . . . . . 275
vi CONTENTS
12 Implementation of Linked Lists 283
12.1 Nodes and Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
12.2 Some Basic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
12.3 Iterators, Insert and Erase . . . . . . . . . . . . . . . . . . . . . . . . . 293
12.4 Destroying and Copying Linked Lists . . . . . . . . . . . . . . . . . . 304
13 Analysis of Algorithms 307
13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
13.2 Measuring Exact Running Times . . . . . . . . . . . . . . . . . . . . . 309
13.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
13.4 Asymptotic Running Times . . . . . . . . . . . . . . . . . . . . . . . . 314
13.5 Some Common Running Times . . . . . . . . . . . . . . . . . . . . . . 318
13.6 Basic Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
13.7 Worst-Case and Average-Case Analysis . . . . . . . . . . . . . . . . . 329
13.8 The Binary Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . 332
14 Recursion 337
14.1 The Technique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
14.2 When to Use Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . 348
14.3 Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
15 Sorting 353
15.1 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
15.2 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
15.3 Mergesort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
15.4 Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Bibliography 379
CONTENTS vii
Index 381
viii CONTENTS
Preface
Thesenotesareforasecondcourseoncomputerprogrammingandsoftwarede-
velopment. Inafirstcourse,youlikelyfocusedonlearningthebasics: variables,
control statements, input and output, files, vectors (or arrays), and functions.
Theseconceptsarecriticallyimportantandtheyaresufficientforthecreationof
manyusefulprograms. Butmanyotherprograms,especiallylargeones,require
more powerful concepts and techniques.
The creation of large computer programs poses three basic challenges. The
overallgoalofthesenotesistoteachyouconceptsandtechniquesthatwillallow
you to meet these three challenges. Here’s an overview.
First, a large program always contains a large amount of details and all this
informationcan bedifficult tomanage. The solutionisto organizethe program
intoasetofsmallercomponents. Thismodularityisusuallyachievedthroughthe
techniqueofabstraction. Evenifyouarenotfamiliarwiththeseterms,youhave
already used abstraction in your programs: abstraction happens automatically
everytimeyoucreateafunction. Inthesenotes,youwilllearnhowtoapplythe
technique of abstraction to data.
Second,alargeprogramtypicallyholdsalargeamountofdatathatneedsto
beaccessedefficiently. Inresponse,computerscientistshaveinventedawideva-
rietyofdatastructures,whicharewaysoforganizingdatasoitcanbestoredand
ix
x PREFACE
accessedefficiently. Youarealreadyfamiliarwithonedatastructure: vectors(or
arrays). In these notes, you will learn about other basic data structures: linked
lists, sets and maps. You will learn how and when to use these data structures.
Youwillalsolearnthetechniquesinvolvedintheimplementationofvectorsand
linked lists.
Third, a large program often performs complex tasks that require nontrivial
techniques. Computer scientists have designed algorithms that perform a wide
variety of complex tasks efficiently. In these notes, you will learn efficient al-
gorithms for searching and sorting. You will also learn to design algorithms
using the technique of recursion and how to analyze the efficiency of simple
algorithms.
Most of the concepts and techniques you will learn will be introduced
through the creation of programs that are representative of real-life software.
Theseexampleswillshowthattheseconceptsandtechniquesaretoolsthathave
been developed to solve real problems.
Asyoulearnaboutdataabstraction,datastructuresandalgorithms,youwill
alsolearnaboutanumberofotherimportanttopicssuchastheoverallsoftware
development process, the importance of good documentation, object-oriented
design, classes, pointers, dynamic memory allocation, exceptions, testing, the
use of standard software components (as available in a standard library), and
the creation of generic software components using templates and iterators.
ThesenotesusetheprogramminglanguageC++anditsassociatedStandard
TemplateLibrary(STL).Eventhoughthemainfocusofthesenotesisnotonthe
C++ language itself, you will learn all the relevant features of C++ and the
STL. In particular, you will learn to implement data structures that are realistic
subsets of the data structures provided in the STL.
After reading these notes and working on the exercises, you will be able
to create fairly complex computer programs. But you will also be prepared to
Description:programming), CS344 Algorithms and Data Structures (more advanced than those covered in these notes) and CS241 Computer Organization. In fact, the.