D S ata tructureS anD P D rogram eSign u c++ Sing LICENSE, DISCLAIMER OF LIABILITY, AND LIMITED WARRANTY By purchasing or using this book (the “Work”), you agree that this license grants permission to use the contents contained herein, but does not give you the right of ownership to any of the textual content in the book or ownership to any of the information or products contained in it. This license does not permit uploading of the Work onto the Internet or on a network (of any kind) without the written consent of the Publisher. Duplication or dissemination of any text, code, simulations, images, etc. contained herein is limited to and subject to licensing terms for the respective products, and permission must be obtained from the Publisher or the owner of the content, etc., in order to reproduce or network any portion of the textual material (in any media) that is contained in the Work. Mercury Learning and inforMation (“MLI” or “the Publisher”) and anyone involved in the creation, writing, production, accompanying algorithms, code, or computer pro- grams (“the software”), and any accompanying Web site or software of the Work, can- not and do not warrant the performance or results that might be obtained by using the contents of the Work. The author, developers, and the Publisher have used their best efforts to insure the accuracy and functionality of the textual material and/or pro- grams contained in this package; we, however, make no warranty of any kind, express or implied, regarding the performance of these contents or programs. The Work is sold “as is” without warranty (except for defective materials used in manufacturing the book or due to faulty workmanship). The author, developers, and the publisher of any accompanying content, and anyone involved in the composition, production, and manufacturing of this work will not be liable for damages of any kind arising out of the use of (or the inability to use) the algo- rithms, source code, computer programs, or textual material contained in this publica- tion. This includes, but is not limited to, loss of revenue or profit, or other incidental, physical, or consequential damages arising out of the use of this Work. The sole remedy in the event of a claim of any kind is expressly limited to replacement of the book and only at the discretion of the Publisher. The use of “implied warranty” and certain “exclusions” vary from state to state, and might not apply to the purchaser of this product. D S ata tructureS anD P D rogram eSign u c++ Sing A Self-Teaching Introduction Dheeraj Malhotra Neha Malhotra MERCURY LEARNING AND INFORMATION Dulles, Virginia Boston, Massachusetts New Delhi Copyright © 2019 by Mercury Learning and inforMation LLC. All rights reserved. This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means, media, electronic display or mechanical display, including, but not limited to, photocopy, recording, Internet postings, or scanning, without prior permission in writing from the publisher. Publisher: David Pallai Mercury Learning and inforMation 22841 Quicksilver Drive Dulles, VA 20166 [email protected] www.merclearning.com Dedicated to our (800) 232-0223 loving parents and beloved students. D. Malhotra and N. Malhotra. Data Structures and Program Design Using C++. ISBN: 978-1-68392-370-1 The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks, etc. is not an attempt to infringe on the property of others. Library of Congress Control Number: 2018913035 181920321 Printed on acid-free paper in the United States of America. Our titles are available for adoption, license, or bulk purchase by institutions, corporations, etc. For additional information, please contact the Customer Service Dept. at (800) 232-0223(toll free). Digital versions of our titles are available at: www.authorcloudware.com and other electronic vendors. The sole obligation of Mercury Learning and inforMation to the purchaser is to replace the book and/or disc, based on defective materials or faulty workmanship, but not based on the operation or functionality of the product. Dedicated to our loving parents and beloved students. CONTENTS Preface xv Acknowledgments xvii 1 Introduction to Data Structures 1 1.1 Introduction 1 1.2 Types of Data Structures 3 1.2.1 Linear and Non-linear Data Structures 3 1.2.2 Static and Dynamic Data Structures 3 1.2.3 Homogeneous and Non-homogeneous Data Structures 4 1.2.4 Primitive and Non-Primitive Data Structures 4 1.2.5 Arrays 5 1.2.6 Queues 6 1.2.7 Stacks 7 1.2.8 Linked List 8 1.2.9 Trees 9 1.2.10 Graphs 11 1.3 Operations on Data Structures 12 1.4 Algorithms 13 1.4.1 Developing an Algorithm 14 1.5 Approaches for Designing an Algorithm 14 1.6 Analyzing an Algorithm 15 1.6.1 Time-Space Trade-off 17 1.7 Abstract Data Types 17 1.8 Big O Notation 18 1.9 Summary 18 1.10 Exercises 20 1.11 Multiple Choice Questions 21 viii • Contents 2 Introduction to the C++ Language 25 2.1 Introduction 26 2.2 C++ and Its Characteristics 26 2.3 Features of Object-Oriented Programming 27 2.4 Character Set Used in C++ 34 2.5 C++ Tokens 34 2.6 Data Types in C++ 36 2.7 Structure of a C++ Program 37 2.7.1 Structure of a C++ Program without Classes 37 2.7.2 Structure of a C++ Program with Classes 37 2.8 Operators in C++ 39 2.9 Decision Control Statements in C++ 45 2.10 Looping Statements in C++ 55 2.11 Break and Continue Statements 61 2.12 Functions in C++ 64 2.12.1 Passing Arguments to Functions 66 2.13 Structures in C++ 68 2.14 Reference Variables in C++ 69 2.15 Pointers 70 2.16 Arrays and Pointers 70 2.17 Summary 72 2.18 Exercises 75 2.18.1 Theory Questions 75 2.18.2 Programming Questions 77 2.18.3 Multiple Choice Questions 78 3 Arrays 81 3.1 Introduction 82 3.2 Definition of an Array 82 3.3 Array Declaration 83 3.4 Array Initialization 84 3.5 Calculating the Address of Array Elements 85 3.6 Operations on Arrays 86 3.7 2-D Arrays/ Two-Dimensional Arrays 110 3.8 Declaration of Two-Dimensional Arrays 110 3.9 Operations on 2-D Arrays 113 3.10 Multidimensional Arrays/ N-Dimensional Arrays 118 3.11 Calculating the Address of 3-D Arrays 118 3.12 Arrays and Pointers 121 3.13 Array of Pointers 122 3.14 Arrays and their Applications 123 3.15 Sparse Matrices 124 Contents • ix 3.16 Types of Sparse Matrices 124 3.17 Representation of Sparse Matrices 125 3.18 Summary 127 3.19 Exercises 129 3.19.1 Theory Questions 129 3.19.2 Programming Questions 130 3.19.3 Multiple Choice Questions 131 4 Linked Lists 135 4.1 Introduction 135 4.2 Definition of a Linked List 136 4.3 Memory Allocation in a Linked List 138 4.4 Types of Linked Lists 139 4.4.1 Singly Linked List 139 4.4.2 Operations on a Singly Linked List 140 4.4.3 Circular Linked Lists 159 4.4.4 Operations on a Circular Linked List 160 4.4.5 Doubly Linked List 173 4.4.6 Operations on a Doubly Linked List 174 4.5 Header Linked Lists 192 4.6 Applications of Linked Lists 207 4.7 Polynomial Representation 207 4.8 Summary 207 4.9 Exercises 208 4.9.1 Theory Questions 208 4.9.2 Programming Questions 209 4.9.3 Multiple Choice Questions 209 5 Queues 213 5.1 Introduction 213 5.2 Definition of a Queue 214 5.3 Implementation of a Queue 214 5.3.1 Implementation of Queues Using Arrays 214 5.3.2 Implementation of Queues Using Linked Lists 215 5.3.2.1 Insertion in Linked Queues 215 5.3.2.2 Deletion in Linked Queues 217 5.4 Operations on Queues 222 5.4.1 Insertion 222 5.4.2 Deletion 223 5.5 Types of Queues 227 5.5.1 Circular Queue 227 5.5.1.1 Limitation of Linear Queues 228