Table Of ContentProgramming
C
Abstractions in ++
Eric S. Roberts
Stanford University
Spring Quarter 2012
Foreword
This text represents a major revision of the course reader that we’ve been using at
Stanford for the last several years. The primary goal of the revision was to bring the
approach more closely in line with the way C++ is used in industry, which will in
turn make it easier to export Stanford’s approach to teaching data structures to a
larger fraction of schools. Even though this quarter’s draft is reasonably complete,
the text remains somewhat rough. In particular, these chapters have not yet had the
benefit of the wonderful copyediting service that my wife Lauren Rusk has provided
for all my books.
This textbook has had an interesting evolutionary history that in some ways
mirrors the genesis of the C++ language itself. Just as Bjarne Stroustrup’s first
version of C++ was implemented on top of a C language base, this reader began its
life as my textbook Programming Abstractions in C (Addison-Wesley, 1998). In
2002-03, Julie Zelenski updated it for use with the C++ programming language,
which we began using in CS106 B and CS106 X during that year. Although the
revised text worked fairly well at the outset, CS106 B and CS106 X have evolved in
recent years so that their structure no longer tracks the organization of the book. In
2009, I embarked on a comprehensive process of rewriting the book so that students
in these courses can use it as both a tutorial and a reference. As always, that process
takes a considerable amount of time, and there are almost certainly some sections of
the book that need a substantial rewrite.
I want to thank my colleagues at Stanford over the last several years, starting
with Julie Zelenski for her extensive work on the initial C++ revision. My
colleagues Keith Schwarz, Jerry Cain, Stephen Cooper, and Mehran Sahami have
all made important contributions to the revision. I also need to express my thanks to
several generations of section leaders and so many students over the years, all of
whom have helped make it so exciting to teach this wonderful material. Finally, I
want to thank the students in CS106 B in winter quarter 2011-12 who put up with a
partially finished reader and contributed many error reports and suggestions.
I’ve always believed that programming is one of the most challenging and
exciting intellectual activities that humans have ever discovered. By staying close
to the machine, C++ gives you all the freedom you need to build applications that
take advantage of the full power of modern computing. I hope you all enjoy the
ride.
i
Contents
1 Overview of C++ 1
1.1 Your first C++ program 2
1.2 The history of C++ 3
1.3 The structure of a C++ program 6
1.4 Variables 14
1.5 Data types 19
1.6 Expressions 26
1.7 Statements 36
1.8 The Stanford C++ libraries 48
Summary 49
Review questions 50
Exercises 52
2 Functions and Libraries 57
2.1 The idea of a function 58
2.2 Libraries 61
2.3 Defining functions in C++ 63
2.4 The mechanics of function calls 67
2.5 Reference parameters 75
2.6 Interfaces and implementations 80
2.7 Principles of interface design 87
2.8 Designing a random number library 92
2.9 A brief tour of the graphics library 110
Summary 113
Review questions 114
Exercises 116
3 Strings 125
3.1 Using strings as abstract values 126
3.2 String operations 129
3.3 The <cctype> library 137
3.4 Modifying the contents of a string 138
3.5 The legacy of C-style strings 139
3.6 Writing string applications 140
3.7 The strlib.h library 146
Summary 147
Review questions 148
Exercises 149
ii
4 Streams 159
4.1 Using strings as abstract values 160
4.2 Formatted input 165
4.3 Data files 167
4.4 Class hierarchies 180
4.5 Exploring the Stanford libraries 187
Summary 189
Review questions 190
Exercises 191
5 Collections 197
5.1 The Vector class 199
5.2 The Stack class 213
5.3 The Queue class 219
5.4 The Map class 228
5.5 The Set class 234
5.6 Iterating over a collection 238
Summary 245
Review questions 247
Exercises 248
6 Designing Classes 263
6.1 Representing points 264
6.2 Operator overloading 273
6.3 Rational numbers 284
6.4 Designing a token scanner class 295
6.5 Encapsulating programs as classes 305
Summary 307
Review questions 308
Exercises 310
iii
7 Introduction to Recursion 319
7.1 A simple example of recursion 320
7.2 The factorial function 322
7.3 The Fibonacci function 329
7.4 Checking palindromes 336
7.5 The binary search algorithm 339
7.6 Mutual recursion 340
7.7 Thinking recursively 342
Summary 344
Review questions 346
Exercises 348
8 Recursive Strategies 353
8.1 The Towers of Hanoi 354
8.2 The subset-sum problem 365
8.3 Generating permutations 368
8.4 Graphical recursion 372
Summary 379
Review questions 379
Exercises 380
9 Backtracking Algorithms 393
9.1 Recursive backtracking in a maze 394
9.2 Backtracking and games 404
9.3 The minimax algorithm 413
Summary 419
Review questions 420
Exercises 421
10 Algorithmic Analysis 433
10.1 The sorting problem 434
10.2 Computational complexity 439
10.3 Recursion to the rescue 447
10.4 Standard complexity classes 453
10.5 The Quicksort algorithm 456
10.6 Mathematical induction 462
Summary 466
Review questions 467
Exercises 470
iv
11 Pointers and Arrays 477
11.1 The structure of memory 478
11.2 Pointers 488
11.3 Arrays 498
11.4 Using functions as data values 508
Summary 515
Review questions 517
Exercises 520
12 Dynamic Memory Management 527
12.1 Dynamic allocation and the heap 528
12.2 Defining a CharStack class 533
12.3 Heap-stack diagrams 544
12.4 Copying objects 550
Summary 556
Review questions 557
Exercises 559
13 Efficiency and Representation 563
13.1 The concept of an editor buffer 564
13.2 Defining the buffer abstraction 566
13.3 An array-based implementation 570
13.4 A stack-based implementation 578
13.5 A list-based implementation 583
Summary 601
Review questions 602
Exercises 604
14 Linear Structures 611
14.1 Templates 612
14.2 Defining stacks as a template class 615
14.3 Implementing stacks as linked lists 622
14.4 Implementing queues 626
14.5 Implementing vectors 640
Summary 648
Review questions 649
Exercises 650
v
15 Maps and Hashing 653
15.1 Implementing maps using arrays 654
15.2 Lookup tables 660
15.3 Hashing 663
Summary 675
Review questions 675
Exercises 677
16 Trees 681
16.1 Family trees 683
16.2 Binary search trees 685
16.3 Balanced trees 698
16.4 Implementing maps using BSTs 709
Summary 711
Review questions 712
Exercises 715
17 Expression Trees 725
17.1 Overview of the interpreter 726
17.2 The structure of expressions 731
17.3 Defining the expression hierarchy 736
17.4 Parsing an expression 750
Summary 756
Review questions 757
Exercises 759
18 Sets 765
18.1 Sets as a mathematical abstraction 766
18.2 Expanding the set interface 770
18.3 Implementation strategies for sets 778
18.4 Optimizing sets of small integers 780
Summary 786
Review questions 787
Exercises 790
vi
19 Graphs 793
19.1 The structure of a graph 794
19.2 Representation strategies 798
19.3 A low-level graph abstraction 802
19.4 Graph traversals 809
19.5 Defining a Graph class 815
19.6 Finding shortest paths 824
19.7 Implementing priority queues 828
Summary 831
Review questions 833
Exercises 835
vii
A Library Interfaces 847
A-1 cmpfn.h 848
A-2 console.h 849
A-3 direction.h 850
A-4 error.h 852
A-5 filelib.h 853
A-6 foreach.h 859
A-7 gevents.h 860
A-8 graph.h 874
A-9 graphics.h 879
A-10 grid.h 887
A-11 gtypes.h 890
A-12 gwindow.h 895
A-13 hashmap.h 904
A-14 hashset.h 907
A-15 lexicon.h 913
A-16 map.h 916
A-17 point.h 919
A-18 pqueue.h 921
A-19 queue.h 924
A-20 random.h 927
A-21 set.h 928
A-22 simpio.h 933
A-23 sound.h 934
A-24 stack.h 936
A-25 strlib.h 938
A-26 thread.h 941
A-27 tokenscanner.h 945
A-28 vector.h 951
Index 955
viii
Description:turn make it easier to export Stanford's approach to teaching data structures to a . for a second course in computer science and therefore assumes that you have prior experience with closely related languages such as C or Java.