ebook img

Data Structure and Algorithms Using C++: A Practical Implementation PDF

403 Pages·2021·8.723 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Data Structure and Algorithms Using C++: A Practical Implementation

Data Structure and Algorithms Using C++ Scrivener Publishing 100 Cummings Center, Suite 541J Beverly, MA 01915-6106 Publishers at Scrivener Martin Scrivener ([email protected]) Phillip Carmical ([email protected]) Data Structure and Algorithms Using C++ A Practical Implementation Edited by Sachi Nandan Mohanty ICFAI Foundation For Higher Education, Hyderabad, India and Pabitra Kumar Tripathy Kalam Institute of Technology, Berhampur, India This edition first published 2021 by John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, USA and Scrivener Publishing LLC, 100 Cummings Center, Suite 541J, Beverly, MA 01915, USA © 2021 Scrivener Publishing LLC For more information about Scrivener publications please visit www.scrivenerpublishing.com. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or other- wise, except as permitted by law. Advice on how to obtain permission to reuse material from this title is available at http://www.wiley.com/go/permissions. Wiley Global Headquarters 111 River Street, Hoboken, NJ 07030, USA For details of our global editorial offices, customer services, and more information about Wiley prod- ucts visit us at www.wiley.com. Limit of Liability/Disclaimer of Warranty While the publisher and authors have used their best efforts in preparing this work, they make no rep- resentations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation any implied warranties of merchant- ability or fitness for a particular purpose. No warranty may be created or extended by sales representa- tives, written sales materials, or promotional statements for this work. The fact that an organization, website, or product is referred to in this work as a citation and/or potential source of further informa- tion does not mean that the publisher and authors endorse the information or services the organiza- tion, website, or product may provide or recommendations it may make. This work is sold with the understanding that the publisher is not engaged in rendering professional services. The advice and strategies contained herein may not be suitable for your situation. You should consult with a specialist where appropriate. Neither the publisher nor authors shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. Further, readers should be aware that websites listed in this work may have changed or disappeared between when this work was written and when it is read. Library of Congress Cataloging-in-Publication Data ISBN 978-1-119-75054-3 Cover image: Pixabay.Com Cover design by Russell Richardson Set in size of 11pt and Minion Pro by Manila Typesetting Company, Makati, Philippines Printed in the USA 10 9 8 7 6 5 4 3 2 1 Contents Preface xi 1 Introduction to Data Structure 1 1.1 Definition and Use of Data Structure 1 1.2 Types of Data Structure 2 1.3 Algorithm 3 1.4 Complexity of an Algorithm 6 1.5 Efficiency of an Algorithm 7 1.6 Asymptotic Notations 8 1.7 How to Determine Complexities 9 1.8 Questions 13 2 Review of Concepts of ‘C++’ 15 2.1 Array 15 2.1.1 One-Dimensional Array 16 2.1.2 Multi-Dimensional Array 17 2.1.3 String Handling 20 2.2 Function 26 2.2.1 User Defined Functions 26 2.2.2 Construction of a Function 27 2.2.3 Actual Argument and Formal Argument 31 2.2.4 Call by Value and Call by Reference 32 2.2.5 Default Values for Parameters 34 2.2.6 Storage Class Specifiers 35 2.3 Pointer 37 2.3.1 Declaration of a Pointer 37 2.3.2 Initialization of a Pointer 37 2.3.3 Arithmetic With Pointer 38 2.3.4 Passing of a Pointer to Function 39 2.3.5 Returning of a Pointer by Function 40 2.3.6 C++ Null Pointer 41 v vi Contents 2.4 Structure 42 2.4.1 The typedef Keyword 46 2.5 Questions 47 3 Sparse Matrix 49 3.1 What is Sparse Matrix 49 3.2 Sparse Matrix Representations 49 3.3 Algorithm to Represent the Sparse Matrix 51 3.4 Programs Related to Sparse Matrix 52 3.5 Why to Use Sparse Matrix Instead of Simple Matrix? 56 3.6 Drawbacks of Sparse Matrix 57 3.7 Sparse Matrix and Machine Learning 57 3.8 Questions 58 4 Concepts of Class 59 4.1 Introduction to CLASS 59 4.2 Access Specifiers in C++ 60 4.3 Declaration of Class 60 4.4 Some Manipulator Used In C++ 62 4.5 Defining the Member Functions Outside of the Class 64 4.6 Array of Objects 64 4.7 Pointer to Object 66 4.8 Inline Member Function 67 4.9 Friend Function 69 4.9.1 Simple Friend Function 69 4.9.2 Friend With Inline Substitution 70 4.9.3 Granting Friendship to Another Class (Friend Class) 71 4.9.4 More Than One Class Having the Same Friend Function 73 4.10 Static Data Member and Member Functions 75 4.11 Constructor and Destructor 78 4.11.1 Constructor 78 4.11.1.1 Empty Constructor 79 4.11.1.2 Default Constructor 79 4.11.1.3 Parameterized Constructors 80 4.11.1.4 Copy Constructor 81 4.11.2 Destructor 83 4.12 Dynamic Memory Allocation 84 4.13 This Pointer 86 4.14 Class Within Class 87 4.15 Questions 89 Contents vii 5 Stack 91 5.1 STACK 91 5.2 Operations Performed With STACK 91 5.3 ALGORITHMS 93 5.4 Applications of STACK 96 5.5 Programming Implementations of STACK 106 5.6 Questions 126 6 Queue 129 6.1 Queue 129 6.2 Types of Queue 129 6.3 Linear Queue 129 6.4 Circular Queue 134 6.5 Double Ended Queue 138 6.6 Priority Queue 139 6.7 Programs 142 6.8 Questions 165 7 Linked List 167 7.1 Why Use Linked List? 167 7.2 Types of Link List 167 7.3 Single Link List 168 7.4 Programs Related to Single Linked List 177 7.4.1 /* Creation of a Linked List */ 177 7.4.2 /* Insert a Node Into a Simple Linked List at the Beginning */ 178 7.4.3 /* Insert a Node Into a Simple Linked List at the End of the List */ 180 7.4.4 /* Insert a Node Into a Simple Linked List When the Node Is Known */ 182 7.4.5 /* Insert a Node Into a Simple Linked List Information Is Known and Put After Some Specified Node */ 184 7.4.6 /* Deleting the First Node From a Simple Linked List */ 187 7.4.7 /* Deleting the Last Node From a Simple Linked List */ 189 7.4.8 /* Deleting a Node From a Simple Linked List When Node Number Is Known */ 191 7.4.9 Deleting a Node From a Simple Linked List When Information of a Node Is Given 193 viii Contents 7.4.10 /* SEARCH A NODE INTO A SIMPLE LINKED LIST WITH INFORMATION IS KNOWN*/ 197 7.4.11 /* Sorting a Linked List in Ascending Order */ 199 7.4.12 /* Reversing a Linked List */ 202 7.4.13 Program for Student Data Using Linked List 203 7.5 Double Link List 210 7.6 Programs on Double Linked List 216 7.6.1 /* Creation of Double Linked List */ 216 7.6.2 /* Inserting First Node in the Doubly Linked List */ 218 7.6.3 /*Inserting a Node in the Doubly Linked List When Node Number Is Known*/ 220 7.6.4 /*Inserting a Node in the Doubly Linked List When Information Is Known*/ 223 7.6.5 /* Delete First Node From a Double Linked List */ 226 7.6.6 /*Delete the Last Node From the Double Linked List*/ 229 7.7 Header Linked List 231 7.7.1 /* Inserting a Node Into a Header Linked List */ 233 7.8 Circular Linked List 235 7.9 Application of Linked List 239 7.9.1 Addition of Two Polynomial 239 7.9.2 /* Polynomial With Help of Linked List */ 240 7.9.3 Program for Linked Queue 241 7.9.4 Program for Linked Stack 243 7.10 Garbage Collection and Compaction 245 7.11 Questions 247 8 TREE 249 8.1 Tree Terminologies 249 8.2 Binary Tree 251 8.3 Representation of Binary Tree 253 8.3.1 Array Representation of a Tree 253 8.3.2 Linked List Representation of a Tree 254 8.4 Operations Performed With the Binary Tree 254 8.4.1 /*Creation of a Tree*/ 255 8.5 Traversing With Tree 256 8.5.1 /* Binary Tree Traversal */ 259 8.6 Conversion of a Tree From Inorder and Preorder 262 Contents ix 8.7 Types of Binary Tree 265 8.8 Expression Tree 265 8.9 Binary Search Tree 268 8.10 Height Balanced Tree (AVL Tree) 272 8.11 Threaded Binary Tree 277 8.12 Heap Tree 279 8.13 Huffman Tree 282 8.14 Decision Tree 286 8.15 B-Tree 287 8.16 B + Tree 292 8.17 General Tree 293 8.18 Red–Black Tree 293 8.19 Questions 294 9 Graph 295 9.1 Graph Terminologies 295 9.2 Representation of Graph 301 9.3 Traversal of Graph 305 9.3.1 Breadth First Search (BFS) 305 9.3.2 Depth First Search 311 9.4 Spanning Tree 315 9.4.1 Kruskal Algorithm 315 9.4.2 Prim’s Algorithm 318 9.5 Single Source Shortest Path 322 9.5.1 Bellman–Ford Algorithm 323 9.5.2 Dijkstra’s Algorithm 327 9.6 All Pair Shortest Path 335 9.7 Topological Sorting 345 9.8 Questions 347 10 Searching and Sorting 349 10.1 Linear Search 349 10.2 Binary Search 351 10.3 Bubble Sort 355 10.4 Selection Sort 359 10.5 Insertion Sort 361 10.6 Merge Sort 363 10.7 Quick Sort 366 10.8 Radix Sort 369 10.9 Heap Sort 372 10.10 Questions 389

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.