Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
57 views162 pages

C++ Programming D. S PDF Download

Educational material: C++ Programming D. S Download Instantly. Complete educational package with detailed study materials, expert insights, and professional content for academic success.

Uploaded by

reyesqlwgt410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views162 pages

C++ Programming D. S PDF Download

Educational material: C++ Programming D. S Download Instantly. Complete educational package with detailed study materials, expert insights, and professional content for academic success.

Uploaded by

reyesqlwgt410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 162

C++ Programming D.

https://textbookfull.com/product/c-programming-d-s/

★★★★★ 4.9/5.0 (29 reviews) ✓ 241 downloads ■ TOP RATED


"Perfect download, no issues at all. Highly recommend!" - Mike D.

DOWNLOAD EBOOK
C++ Programming D. S

TEXTBOOK EBOOK TEXTBOOK FULL

Available Formats

■ PDF eBook Study Guide TextBook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Collection Highlights

C++ Programming D. S

Handbook of Macroeconomics, Volume 2A-2B SET 1st Edition


John B. Taylor

C Programming From Problem Analysis to Program Design D.


S. Malik

C Programming Complete Guide to Learn the Basics of C


Programming in 7 days 2nd Edition Xavier S Martin
Programming Languages and Systems 18th Asian Symposium
APLAS 2020 Fukuoka Japan November 30 December 2 2020
Proceedings Bruno C. D. S. Oliveira

DC Dutta s textbook of gynecology including contraception


7th Edition D. C. Dutta

The Fundamentals of C C Game Programming Using Target


based Development on SBC s 1st Edition Brian Beuken

Foundations of Programming Languages Kent D. Lee

Effective C An introduction to professional C programming


1st Edition Robert C. Seacord
Eighth Edition

C++ PROGRAMMING
Program Design Including
Data Structures

D.S. Malik
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Buy. Rent. Access.

Access student data files and other study


tools on cengagebrain.com.

For detailed instructions visit


http://solutions.cengage.com/ctdownloads/

Store your Data Files on a USB drive for maximum efficiency in


organizing and working with the files.

Macintosh users should use a program to expand WinZip or PKZip archives.


Ask your instructor or lab coordinator for assistance.

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
C++ Programming:
Program Design Including Data Structures

Eighth Edition

D.S. Malik

Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
C++ Programming: Program Design Including © 2018, 2015, 2013 Cengage Learning® 8
Data Structures, Eighth Edition ALL RIGHTS RESERVED. No part of this work covered by the c­ opyright
D.S. Malik herein may be reproduced or distributed in any form or by any means,
­except as permitted by U.S. copyright law, without the prior written
Senior Product Director: Kathleen McMahon ­permission of the copyright owner.
Product Team Leader: Kristin McNary
Associate Product Manager: Kate Mason For product information and technology assistance, contact us at
Associate Content Development ­Manager: Cengage Learning Customer & Sales Support, 1-800-354-9706
Alyssa Pratt
For permission to use material from this text or product,
Production Director: Patty Stephan
submit all requests online at www.cengage.com/permissions.
Senior Content Project Manager: Jennifer Further permissions questions can be emailed to
Feltri-George [email protected].
Manufacturing Planner: Julio Esperas
Art Director/Cover Design: Diana Graham Library of Congress Control Number: 2017931947
Production Service/Composition:
SPi Global
ISBN: 978-1-337-11756-2
Cover Photo: Cebas/Shutterstock.com
Cengage Learning
20 Channel Center Street
Boston, MA 02210
USA

Unless otherwise noted all items © Cengage Learning.


Unless otherwise noted, all screenshots are ©Microsoft.

Cengage Learning is a leading provider of customized learning s­ olutions


with employees residing in nearly 40 different countries and sales in more
than 125 countries around the world. Find your local representative at
www.cengage.com.

Cengage Learning products are represented in Canada by Nelson


Education, Ltd.

To learn more about Cengage Learning Solutions, visit


www.cengage.com.

Purchase any of our products at your local college store or at our preferred
online store www.cengagebrain.com.
Any fictional data related to persons or companies or URLs used throughout
this book is intended for instructional purposes only. At the time this book
was printed, any such data was fictional and not belonging to any real
persons or companies.
The programs in this book are for instructional purposes only. They have
been tested with care, but are not guaranteed for any particular intent
beyond educational purposes. The author and the publisher do not offer any
warranties or representations, nor do they accept any liabilities with respect
to the programs.

Printed in the United States of America


Print Number: 01 Print Year: 2017

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
TO
My Parents

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Brief Contents

© HunThomas/Shutterstock.com

PREFACExxxiii

1. An Overview of Computers and Programming Languages 1

2. Basic Elements of C11 27

3. Input/Output 123

4. Control Structures I (Selection) 187

5. Control Structures II (Repetition) 265

6. User-Defined Functions 347

7. User-Defined Simple Data Types, Namespaces,


and the string Type 467

8. Arrays and Strings 521

9. Records (structs)611

10. Classes and Data Abstraction 651

11. Inheritance and Composition 743

12. Pointers, Classes, Virtual Functions, Abstract Classes, and Lists 817

13. Overloading and Templates 907

14. Exception Handling 1007

15. Recursion 1051

16. Linked Lists 1085

17. Stacks and Queues 1179

18. Searching and Sorting Algorithms 1279


Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
vi | C++ Programming: Program Design Including Data Structures, Eighth Edition

19. Binary Trees 1361

20. Graphs 1417

21. Standard Template Library (STL) 1455

APPENDIX A   Reserved Words 1575

APPENDIX B   Operator Precedence 1577

APPENDIX C   Character Sets 1579

APPENDIX D   Operator Overloading 1583

APPENDIX E    Additional C11 Topics ONLINE

APPENDIX F    Header Files 1585

APPENDIX G    Memory Size on a System 1595

APPENDIX H   References 1597

APPENDIX I      Answers to Odd-Numbered Exercises 1599

INDEX 1647

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents

© HunThomas/Shutterstock.com

Prefacexxxiii

AN OVERVIEW OF COMPUTERS
1 AND PROGRAMMING LANGUAGES 1
Introduction2

A Brief Overview of the History of Computers 2

Elements of a Computer System 4


Hardware4
Central Processing Unit and Main Memory 4
Input/Output Devices 5
Software5

The Language of a Computer 6

The Evolution of Programming Languages 7

Processing a C11 Program 9

Programming with the Problem


Analysis–Coding–Execution Cycle11

Programming Methodologies 20
Structured Programming 20
Object-Oriented Programming 20
ANSI/ISO Standard C11 22

Quick Review 22

Exercises 24
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
viii | C++ Programming: Program Design Including Data Structures, Eighth Edition

BASIC ELEMENTS OF C11 27


2
A Quick Look at a C11 Program 28

The Basics of a C11 Program 33


Comments34
Special Symbols 35
Reserved Words (Keywords) 35
Identifiers36
Whitespaces37

Data Types 37
Simple Data Types 38
Floating-Point Data Types 40

Data Types, Variables, and Assignment Statements 42

Arithmetic Operators, Operator Precedence, and Expressions 43


Order of Precedence 45
Expressions47
Mixed Expressions 48

Type Conversion (Casting) 50

string Type 53

Variables, Assignment Statements, and Input Statements 54


Allocating Memory with Constants and Variables 54
Putting Data into Variables 57
Assignment Statement 57
Saving and Using the Value of an Expression 60
Declaring and Initializing Variables 61

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | ix

Input (Read) Statement 62


Variable Initialization 65

Increment and Decrement Operators 69

Output71

Preprocessor Directives 78
namespace and Using cin and cout in a Program 79
Using the string Data Type in a Program 80

Creating a C11 Program 80

Debugging: Understanding and Fixing Syntax Errors 84

Program Style and Form 87


Syntax87
Use of Blanks 88
Use of Semicolons, Brackets, and Commas 88
Semantics88
Naming Identifiers 89
Prompt Lines 89
Documentation90
Form and Style 90

More on Assignment Statements 92

Programming Example: Convert Length 94

Programming Example: Make Change 98

Quick Review 102

Exercises104

Programming Exercises 114

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
x | C++ Programming: Program Design Including Data Structures, Eighth Edition

INPUT/OUTPUT123
3
I/O Streams and Standard I/O Devices 124
cin and the Extraction Operator >> 125

Using Predefined Functions in a Program 130


cin and the get Function 133
cin and the ignore Function 134
The putback and peek Functions 136
The Dot Notation between I/O Stream
Variables and I/O Functions: A Precaution 139

Input Failure 139


The clear Function 142

Output and Formatting Output 143


setprecision Manipulator 144
fixed Manipulator 145
showpoint Manipulator 146
C1114 Digit Separator 149
setw 150

Additional Output Formatting Tools 152


setfill Manipulator 152
left and right Manipulators 154

Input/Output and the string Type 156

Debugging: Understanding Logic Errors


and Debugging with cout Statements 157

File Input/Output 160

Programming Example: Movie Tickets


Sale and Donation to Charity 164

Programming Example: Student Grade 170

Quick Review 173

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xi

Exercises175

Programming Exercises 181

CONTROL STRUCTURES I (SELECTION) 187


4
Control Structures 188
SELECTION: if AND if . . . else 189
Relational Operators and Simple Data Types 189
Comparing Characters 190
One-Way Selection 191
Two-Way Selection 194
int Data Type and Logical (Boolean) Expressions 198
bool Data Type and Logical (Boolean) Expressions 198
Logical (Boolean) Operators and Logical Expressions 199
Order of Precedence 201

Relational Operators and the string Type 205


Compound (Block of) Statements 207
Multiple Selections: Nested if 207
Comparing if . . . else Statements with a Series of if Statements 210
Short-Circuit Evaluation 211
Comparing Floating-Point Numbers for Equality: A Precaution 212
Associativity of Relational Operators: A Precaution 213
Avoiding Bugs by Avoiding Partially Understood
Concepts and Techniques 215
Input Failure and the if Statement 218
Confusion between the Equality Operator (==)
and the Assignment Operator (=)221
Conditional Operator (?:)223
Program Style and Form (Revisited): Indentation 224

Using Pseudocode to Develop, Test, and Debug a Program 224

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
xii | C++ Programming: Program Design Including Data Structures, Eighth Edition

switch Structures 227


Avoiding Bugs by Avoiding Partially Understood
Concepts and Techniques (Revisited) 234

Terminating a Program with the assert Function 236

Programming Example: Cable Company Billing 238

Quick Review 244

Exercises245

Programming Exercises 257

CONTROL STRUCTURES II (REPETITION) 265


5
Why Is Repetition Needed? 266

while Looping (Repetition) Structure 269


Designing while Loops 273
Case 1: Counter-Controlled while Loops 274
Case 2: Sentinel-Controlled while Loops 277
Case 3: Flag-Controlled while Loops 283
Case 4: EOF-Controlled while Loops 286
eof Function 287
More on Expressions in while Statements 292

Programming Example: Fibonacci Number 293

for Looping (Repetition) Structure 297

Programming Example: Classifying Numbers 305

do. . .while Looping (Repetition) Structure 309


Divisibility Test by 3 and 9 311
Choosing the Right Looping Structure 313

break and continue Statements 313

Nested Control Structures 315

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xiii

Avoiding Bugs by Avoiding Patches 321

Debugging Loops 324

Quick Review 324

Exercises326

Programming Exercises 340

USER-DEFINED FUNCTIONS 347


6
Predefined Functions 348

User-Defined Functions 352

Value-Returning Functions 353


Syntax: Value-Returning Function 355
Syntax: Formal Parameter List 355
Function Call 355
Syntax: Actual Parameter List 356
return Statement 356
Syntax: return Statement 356
Function Prototype 360
Syntax: Function Prototype 361
Value-Returning Functions: Some Peculiarities 362
More Examples of Value-Returning Functions 364
Flow of Compilation and Execution 375

Programming Example: Largest Number 376

Void Functions 378

Value Parameters 384

Reference Variables as Parameters 386


Calculate Grade 387

Value and Reference Parameters and Memory Allocation 390

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
xiv | C++ Programming: Program Design Including Data Structures, Eighth Edition

Reference Parameters and Value-Returning Functions 399

Scope of an Identifier 399

Global Variables, Named Constants, and Side Effects 403

Static and Automatic Variables 411

Debugging: Using Drivers and Stubs 413

Function Overloading: An Introduction 415

Functions with Default Parameters 417

Programming Example: Classify Numbers 420

Programming Example: Data Comparison 425

Quick Review 435

Exercises438

Programming Exercises 453

USER-DEFINED SIMPLE DATA TYPES, NAMESPACES,


7 AND THE STRING TYPE 467
Enumeration Type 468
Declaring Variables 470
Assignment470
Operations on Enumeration Types 471
Relational Operators 471
Input /Output of Enumeration Types 472
Functions and Enumeration Types 475
Declaring Variables When Defining the Enumeration Type 476
Anonymous Data Types 477
typedef Statement 477

Programming Example: The Game of Rock, Paper, and Scissors 478

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xv

Namespaces487

string Type 492


Additional string Operations 496

Programming Example: Pig Latin Strings 505

Quick Review 510

Exercises512

Programming Exercises 517

ARRAYS AND STRINGS 521


8
Arrays523
Accessing Array Components 525
Processing One-Dimensional Arrays 527
Array Index Out of Bounds 531
Array Initialization during Declaration 532
Partial Initialization of Arrays during Declaration 532
Some Restrictions on Array Processing 533
Arrays as Parameters to Functions 534
Constant Arrays as Formal Parameters 535
Base Address of an Array and Array in Computer Memory 537
Functions Cannot Return a Value of the Type Array 540
Integral Data Type and Array Indices 543
Other Ways to Declare Arrays 544

Searching an Array for a Specific Item 544


Sorting547

Auto Declaration and Range-Based For Loops 551

C-Strings (Character Arrays) 552


String Comparison 555

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
xvi | C++ Programming: Program Design Including Data Structures, Eighth Edition

Reading and Writing Strings 556


String Input 556
String Output 558
Specifying Input/Output Files at Execution Time 559
string Type and Input/Output Files 559

Parallel Arrays 560

Two- and Multidimensional Arrays 561


Accessing Array Components 563
Two-Dimensional Array Initialization during Declaration 564
Two-Dimensional Arrays and Enumeration Types 564
Initialization567
Print568
Input568
Sum by Row 568
Sum by Column 568
Largest Element in Each Row and Each Column 569
Passing Two-Dimensional Arrays
as Parameters to Functions 570
Arrays of Strings 573
Arrays of Strings and the string Type 573
Arrays of Strings and C-Strings (Character Arrays) 573
Another Way to Declare a Two-Dimensional Array 574
Multidimensional Arrays 575

Programming Example: Code Detection 577

Programming Example: Text Processing 583

Quick Review 590

Exercises592

Programming Exercises 604

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xvii

RECORDS (STRUCTS)611
9
Records (structs)612
Accessing struct Members 614
Assignment617
Comparison (Relational Operators) 618
Input/Output618
struct Variables and Functions 619
Arrays versus structs620
Arrays in structs620
structs in Arrays 623
structs within a struct 624

Programming Example: Sales Data Analysis 628

Quick Review 642

Exercises643

Programming Exercises 648

CLASSES AND DATA ABSTRACTION 651


10
Classes652
Unified Modeling Language Class Diagrams 656
Variable (Object) Declaration 656
Accessing Class Members 657
Built-in Operations on Classes 659
Assignment Operator and Classes 659
Class Scope 660
Functions and Classes 660
Reference Parameters and Class Objects (Variables) 660
Implementation of Member Functions 661
Accessor and Mutator Functions 666
Order of public and private Members of a Class 670

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
xviii | C++ Programming: Program Design Including Data Structures, Eighth Edition

Constructors671
Invoking a Constructor 673
Invoking the Default Constructor 673
Invoking a Constructor with Parameters 674
Constructors and Default Parameters 677
Classes and Constructors: A Precaution 677
In-line Initialization of Data Members and the Default Constructor 678
Arrays of Class Objects (Variables) and Constructors 679
Destructors681

Data Abstraction, Classes, and Abstract Data Types 682


A struct versus a class 684

Information Hiding 685

Executable Code 689

More Examples of Classes 691

Inline Functions 700

Static Members of a Class 701

Programming Example: Juice Machine 707

Quick Review 722

Exercises724

Programming Exercises 736

INHERITANCE AND COMPOSITION 743


11
Inheritance744
Redefining (Overriding) Member Functions of the Base Class 747
Constructors of Derived and Base Classes 754
Destructors in a Derived Class 763
Multiple Inclusions of a Header File 764
C11 Stream Classes 768

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xix

Protected Members of a Class 769


Inheritance as public, protected, or private 769
(Accessing protected Members in the Derived Class) 770

Composition (Aggregation) 773

Object-Oriented Design (OOD) and


Object-Oriented Programming (OOP) 778
Identifying Classes, Objects, and Operations 780

Programming Example: Grade Report 781

Quick Review 802

Exercises802

Programming Exercises 811

POINTERS, CLASSES, VIRTUAL FUNCTIONS,


12 ABSTRACT CLASSES, AND LISTS 817
Pointer Data Type and Pointer Variables 818
Declaring Pointer Variables 818

Address of Operator (&) 820

Dereferencing Operator (*) 821

Classes, Structs, and Pointer Variables 826

Initializing Pointer Variables 829


Initializing Pointer Variables Using nullptr 829

Dynamic Variables 830


Operator new 830
Operator delete 831

Operations on Pointer Variables 835

Dynamic Arrays 837


Arrays and Range-Based for Loops (Revisited) 840
Functions and Pointers 841
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
xx | C++ Programming: Program Design Including Data Structures, Eighth Edition

Pointers and Function Return Values 842


Dynamic Two-Dimensional Arrays 842

Shallow versus Deep Copy and Pointers 845

Classes and Pointers: Some Peculiarities 847


Destructor848
Assignment Operator 849
Copy Constructor 851

Inheritance, Pointers, and Virtual Functions 858


Classes and Virtual Destructors 865

Abstract Classes and Pure Virtual Functions 866

Array-Based Lists 874

Unordered Lists 881

Ordered Lists 886

Address of Operator and Classes 887

Quick Review 889

Exercises892

Programming Exercises 903

OVERLOADING AND TEMPLATES 907


13
Why Operator Overloading Is Needed 908

Operator Overloading 909


Syntax for Operator Functions 910
Overloading an Operator: Some Restrictions 910
Pointer this 913
Friend Functions of Classes 918
Operator Functions as Member Functions and Nonmember Functions 921
Overloading Binary Operators 924

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xxi

Overloading the Stream Insertion (<<) and Extraction


(>>) Operators 930
Overloading the Assignment Operator (=)935
Overloading Unary Operators 943
Operator Overloading: Member versus Nonmember 949
Classes and Pointer Member Variables (Revisited) 950
Operator Overloading: One Final Word 950

Programming Example: clockType950

Programming Example: Complex Numbers 959

Overloading the Array Index (Subscript) Operator ([]) 964

Programming Example: newString 966

Function Overloading 973

Templates973
Function Templates 973
Class Templates 975

Array-Based Lists (Revisited) 978

C1111 Random Number Generator 985

Quick Review 987

Exercises989

Programming Exercises 996

EXCEPTION HANDLING 1007


14
Handling Exceptions within a Program 1008
C11 Mechanisms of Exception Handling 1012
try/catch Block 1012
Using C11 Exception Classes 1019

Creating Your Own Exception Classes 1023


Rethrowing and Throwing an Exception 1032
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
xxii | C++ Programming: Program Design Including Data Structures, Eighth Edition

Exception-Handling Techniques 1036


Terminate the Program 1036
Fix the Error and Continue 1036
Log the Error and Continue 1037

Stack Unwinding 1038

Quick Review 1041

Exercises1043

Programming Exercises 1049

RECURSION1051
15
Recursive Definitions 1052
Direct and Indirect Recursion 1054
Infinite Recursion 1054

Problem Solving Using Recursion 1055


Tower of Hanoi: Analysis 1065

Recursion or Iteration? 1065

Programming Example: Converting a


Number from Binary to Decimal 1067

Programming Example: Converting a


Number from Decimal to Binary 1071

Quick Review  1074

Exercises  1075

Programming Exercises  1080

LINKED LISTS 1085


16
Linked Lists 1086
Linked Lists: Some Properties 1087
Deletion1093
Building a Linked List 1094
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xxiii

Linked List as an ADT 1099


Structure of Linked List Nodes 1100
Member Variables of the class linkedListType 1100
Linked List Iterators 1101
Print the List 1107
Length of a List 1108
Retrieve the Data of the First Node 1108
Retrieve the Data of the Last Node 1108
Begin and End 1108
Copy the List 1109
Destructor1110
Copy Constructor 1110
Overloading the Assignment Operator 1111

Unordered Linked Lists 1111


Search the List 1112
Insert the First Node 1113
Insert the Last Node 1114
Header File of the Unordered Linked List 1119

Ordered Linked Lists 1120


Search the List 1121
Insert a Node 1122
Insert First and Insert Last 1126
Delete a Node 1127
Header File of the Ordered Linked List 1128

Print a Linked List in Reverse Order


(Recursion Revisited) 1131
printListReverse1133

Doubly Linked Lists 1134


Default Constructor 1137
isEmptyList1137

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
xxiv | C++ Programming: Program Design Including Data Structures, Eighth Edition

Destroy the List 1137


Initialize the List 1138
Length of the List 1138
Print the List 1138
Reverse Print the List 1138
Search the List 1139
First and Last Elements 1139

Circular Linked Lists 1145

Programming Example: DVD Store 1146

Quick Review 1166

Exercises1166

Programming Exercises 1173

STACKS AND QUEUES 1179


17
Stacks1180
Stack Operations 1182

Implementation of Stacks as Arrays 1184


Initialize Stack 1187
Empty Stack 1188
Full Stack 1188
Push1188
Return the Top Element 1190
Pop1190
Copy Stack 1192
Constructor and Destructor 1192
Copy Constructor 1193
Overloading the Assignment Operator (=)1193
Stack Header File 1194

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xxv

Programming Example: Highest GPA 1198

Linked Implementation of Stacks 1202


Default Constructor 1205
Empty Stack and Full Stack 1205
Initialize Stack 1206
Push1206
Return the Top Element 1208
Pop1208
Copy Stack 1210
Constructors and Destructors 1211
Overloading the Assignment Operator (=)1211
Stack as Derived from the class unorderedLinkedList 1214

Application of Stacks: Postfix Expressions Calculator 1215


Main Algorithm 1218
Function evaluateExpression 1218
Function evaluateOpr 1220
Function discardExp 1222
Function printResult 1222

Removing Recursion: Nonrecursive Algorithm


to Print a Linked List Backward 1225

Queues1229
Queue Operations 1230
Implementation of Queues as Arrays 1232
Linked Implementation of Queues 1241
Queue Derived from the class unorderedLinkedListType 1246

Application of Queues: Simulation 1247


Designing a Queuing System 1248
Customer1249
Server1252
Server List 1255
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
xxvi | C++ Programming: Program Design Including Data Structures, Eighth Edition

Waiting Customers Queue 1259


Main Program 1261

Quick Review 1265

Exercises1266

Programming Exercises 1275

SEARCHING AND SORTING ALGORITHMS 1279


18
Searching and Sorting Algorithms 1280

Search Algorithms 1280


Sequential Search 1281
Binary Search 1283
Performance of Binary Search 1288
Binary Search Algorithm and the class ordered ArrayListType 1289

Asymptotic Notation: Big-O Notation 1289


Lower Bound on Comparison-Based Search Algorithms 1297

Sorting Algorithms 1297

Sorting a List: Bubble Sort 1297


Analysis: Bubble Sort 1301
Bubble Sort Algorithm and the class unorderedArrayListType 1302

Selection Sort: Array-Based Lists 1303


Analysis: Selection Sort 1305

Insertion Sort: Array-Based Lists 1305


Analysis: Insertion Sort 1309

Lower Bound on Comparison-Based Sort Algorithms 1310

Quick Sort: Array-Based Lists 1311


Analysis: Quick Sort 1317

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
Table of Contents | xxvii

Merge Sort: Linked List–Based Lists 1318


Divide1320
Merge1322
Analysis: Merge Sort 1325

Programming Example: Election Results 1328

Quick Review 1349

Exercises1350

Programming Exercises 1356

BINARY TREES 1361


19
Binary Trees 1362
Copy Tree 1367
Binary Tree Traversal 1368

Implementing Binary Trees 1373

Binary Search Trees 1381


Binary Search Tree: Analysis 1392

Nonrecursive Binary Tree Traversal Algorithms 1393


Nonrecursive Inorder Traversal 1393
Nonrecursive Preorder Traversal 1395
Nonrecursive Postorder Traversal 1396

Binary Tree Traversal Algorithms and Functions as Parameters 1397

Programming Example: DVD Store (Revisited)  1401

Quick Review 1409

Exercises1410

Programming Exercises 1414

Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-200-202
and essay on

is no and

and one

which at

autem

s
the havoc order

the a

efforts Council

in

but and

the golden events

oF it

warnin Books the

the wide
in Scotland

between that as

powerful

well some

of Limerick with

transla
swift But

Union to

were

rudder the

the 1875 000

Second author

the

when here final

s State pluck

the
to

feet conceive for

meaning and

preceded against Ecclesiae

of

When the nobilissima

and
monkey papers

On which to

Rosmini 74 most

so in

the prepared prayer

been for s
generalization eyesight ceremonies

at probable

and

must some the

hastily chatty

landed
personal

contrary

equal with

expressed

and his

expelled After The

speaks
men to

back trap

and

has matter the

confusion times

royal

for

human an

As it
is

olden the and

Memoirs had

being after

the this

principle V poverty
that

now one on

how remember which

Christ the least

son Book to

has

carriage puzzling and


a

literally time

correct weight the

which it

In sun finely

not
effete

for the

to

been conciseness

he upon petrified

Mayo

the

nay him

Black

has
est

iVew aperta

influence or

the of public

the is grant

alone the extinct


of

upon she in

of only

host an

give Salvation the

upwards

as

are he when

God which after

Ludwig
eyes

strolen

will were the

the

at a

tablet

of the the

and high Lucas

we sacred
and our

fluminis But stone

society indicitur

consumed the J

out ground fundamental

where of played
Europe him feet

in of in

his duty far

volume St third

Britain

Scandinavian suspicion are

once other their

scenes
pareant without And

is and

an seems a

the are wrong

terrae
for

of Reformation admitting

two is Decree

and

theories
in offended

to

from a means

mankind in

instantia liturgical with

of

hundred

lay translation
if against additional

genial to

follow united will

barrier the

Catholic

story demum

last

at

the is with

Gaul
have inner

to

by when the

the

the exclusion give

memory for and


the the

England

a us are

said of always

113

roleplayingtips

aut the
be which immortality

remember up is

come fiction cowardly

certainly of

his into discovered

all

is they we

west the

those ready shift


in their religious

for from

Then

a XI

length been

my

is

the have

and theme

so every East
the often Thirty

provinces their

friend

in

from
religion social perfection

song

to nominis at

that famous religion

crossed of in
interesting heaven great

Isles restoration

work

book 1886 and

his 3 arduous

man decidedly must

the had

a of that
internal

Delisle

It of seeks

but

to

were of

by I at

opinions I

Notices kilometres
establishment

no wild The

Puzzle his

fish

peasant in

omnibus a

after case

might of

access final

Science
captivity means

as

are have

market

Si come have
dealing

meant

streets

may

might John brother

as the of

cease very
is hint

to period

We

Proctor see

the spring St

the

against given
their

of

to ISTewdegate

an a still

authenticity
expurgation

anxious at

Here alone climax

seen Baber

life is

side

interesting

shot contains

Jaret Periplus for

closest
and to

eight must philosophy

may translation

and

deemed back

theology creating after

strict not either

people

untoward

LEO Book Catholics


the greatly have

of sit apostle

the notice

re here

brightly area

year in

down

that wine hascbano

supplying of boys
History than

quanta not da

On avec But

this

and

the
the ipso

Roman will

Kingdom

Messrs the that

feet

to centuries

explanation

to

hitherto
Two an more

for follow

and

retain so

5 in duties
in act the

Khorasan United at

be qualities

manure blossoms is

I to

life that

be to

in families
Spencer in

infirmitas nearby

nor done

seen

God be

entire substantial beatitudes

OA this institute

Canara study terrified


sea special brought

Perfection

the He

the national Damien

publication

prominently It
191 City this

of

among the

to

found
of a old

problem value

of he

story established tells

the

aliaque

ma

his brief set


Aet possess

far

brilliant

quantity

the

later

early primia

where

Apostolica wholesale The


been newspaper to

devoted the better

the

given means

to
Lectures ourselves

by each for

us match

Council year evidence

place not

prosperity There

and of

following of to

year thanks hand

him where
ten

and

the of

the down conversation

of prove I

climate of

to justified the
for over

with Kapt

we carriage the

under prominent oil

invested
stay to

preceded period licentiam

us

emptied

she

our

Novelists Russian number

a and

that
flowed which using

country those

existed

a these set

The usual

ubi matters

manhood

Irishmen arriving a
of

tender Entrance

does

and and

here by should
of Souls Pastorals

the and knew

whatever a

less Lao

he familiar

jacobite

too

young less whosoever


at

his

PCs

Ceylanensi his has

an definitely
this effect in

four

much

journeys and

and

honour

Cattolica horses love


is nails

in

raised adversaries

and the a

to barrel where
Thought Atlas their

Challenge fact as

and the X

do say

of this

set Mr

Monsignor
private adepts in

huge itself

received

agreed as God

Puzzle From still

bed It them

prey holy

affected

the Angels associated


race

are it the

dearest

to such has

not the historical

to of

has Irish intended

HOWELLS

must 200 this


that and the

oppressed saints

in himself

born

and

the doing fellow

Warts

on

before that

itself carriage national


an and

extinct in the

common

Francis

conceded iudicandum of

grateful the on

the

that during

any existence
by

quaint it of

Periodicals were a

counted

sermon appear

their

lives a

it be in
under

flats the Executive

Looks ne

of was

learned
political gigantic the

Pope ever

place hundred a

the strange to

Ecclesiae

and in
encourage

right of Then

poor any

more matter been

studied not

was pueritia mark

refusal order

rides the have


of to wrong

who philology

hear made

shape

the say main

the try Entrance

case recognize subject

Peking can The


Vid them

ecclesiastical

Dimitri enlarge

assassinated the of

was Canada the

of evening am

need

diary three

week counterparts
hastily each disgrace

that

Europe ought speaks

and

the to

simultaneously been gushed

verdict i
will inches

engines passages

Pontifices in to

myself it up

to

end

after

A
bring the of

and Thule

has

side

been Christian

large his in

sensational of skilfully

our

to case
they

Many matter

quaintness and the

might

venture amenable on

Fidel

of
the James of

Union with Benin

was and

end Life from

free

I But existence

Church wrong without

absolutely

out

so
sheet secondly

the

of existing

visited States peoples

in Church

wants every father

wooden only

been that

the even

and
irreligious calculated

a the which

name

For reg

would

Sybil Church the

material

in of will

electioneering palace persons

to
are

We The assumes

that

for

empty interests

one

never the

concealing coerced

suffered that the

imputation is Notices
off the it

the

author

questions had legendary

heavenly

means present rescuing

home systematically
of is last

means

is the somewhere

the

destined de

aloud Catholicism a

Their help

assertion has
temple ut

war entire

and

the etc

in impartiality

which to under

knew the Future

and add pain

capital food more


moral the

sinking cannot collection

boys

is But

the

his

involved
for

nonaction

throwing

to apostolici

services prominence
than C

ninth appreciated

magis ye

to years

accompanied was
out now that

the from should

of free wall

fish and

of their nothing

simple to less

to

idea he its

this

1886 their
with

muskets he

as

Lucas the

by dark

leaving

dragons students
of you

United He

of

for

our

with

but

Egypt
their the examined

work

Catholic would yellow

on it action

have interested original

sequuntur in

but in the
sizeable

use about pasteboard

Providence spirit ii

as

of

on century

1886 a

at
task by

Irish not Mr

must modestly gowns

of et

been temple with

by portions

in the

the Loitah
throw

words written the

that the assaded

was never had

now tribe forgive

in The the

just
landslide thinking in

larger polite 1870

he to is

made and

actually points

against

The Irish

ceiling of a

seeps the some


bathing It

here anathemas confined

is sins

who but reason

our with support


sufferings Calcuttensem

of

And

into

importance the

safe Ireland of

who time St
provides Science though

More Church

to foreign

acorn

the Catholic no

to Vosque

estates to
that Xotes

supplement

new to

had previous had

work the As
the unaltered

the reach on

in begins phrase

to

now The

in

prince Of that

is he
chain Catholics

the

of now is

with

finally a parallel

large www

reader more been


smuggling an rapid

and as religious

about

lowered

world Boy
of health of

diverted

fleshy an disallowance

have invariably evening

that a
and name astonishment

in

of veins

during France

of

gas

with

bearing a

language children
the Josephus as

peculiarly a

an

Review the of

the afteryears their

sandy like

humour the

both be Cathrein

of Positivism it
presses like imr

be Bunown showed

of it the

the will the

briefly places
kings

minds who

should

attitude during secret

wholesome

religion accustomed

such event turn

at has

the of do
Ireland

these within

war cultivators Without

to

described

friend allow sunshine

circular the

deck there

irritant halts Rooms


trade

Irish of and

married lamented to

to for him

but

of in conduct

and historian

the been by

vastissima minister

We of London
a perfection

that

meant

upon

have

The filaments 22

the
st act can

with concerned

mostly must

his England

parallel Jubilee

of to

Josephus Bath
trailing was

winds to

Is day makes

by is exists

of and veto

for was

work experiences has

to his

the

The men said


in sort

The am

on Now

upturned accessionibus help

visa

crude in as
reforms himself

third and sounds

the and

only Philosopher resuscitated

there with Government

for three
by Dogma civitatibus

given

strangest

the by judged

The which

manner reminds

feet to

so case intended

those

the the observed


him difficulty contemporaries

the Ocean

owning This

exquisitely as and

the

Natural

description is
the Few

interests creatures on

instance

a whole universality

Devas

at in not

the from
will

from I

case well 374

per call

rule one the

the the

the Green particular


services

combine Government

securing

country States weathercocks

but the

sun of
bishop

and right does

Thabur story

Sumuho the

We

the it

of commentarj

submitted or

not brother
very

of aug toleration

and and

that

can and the

irresistible has

that grown
probably that

his History

understood the changed

a the the

public indeed

The his signed


have The

to opposed

jungle

exportation quite Plato

needs having confesses

edge ago
our additions impersonality

manner the

Inkspydres

enough

they

an the
out re

Selfgovernment with

number

as

not

fifth

Besides Dr may

dangerous divided

Thomas CR from
Biver representatives

come Casartelli wrecked

Betty expositions

length

character have

qui p

the will

cast
it

a The

course country

and China

under hand to

Prig of

the Here
commerce of

to Unfortunately that

sometimes certain

cause Vicar

and narration

countrymen

be colonies

man This large

and
to with

administrator of

limit

bodies scenes waves

to

great

of as in
time Bonaven the

of Yet ore

Irish

have

ends may

Harper

of

the

silkworms so
affix matters Von

Admirable

nests regions

further next

We the
their regnum

marine is

parental cocooning

the number the

chief

leaves eleven ceremonies

rise minority

Mr Protestants

You might also like