DATA STRUCTURES A Programming Approach with C SECOND EDITION DHARMENDER SINGH KUSHWAHA Associate Professor Department of Computer Science and Engineering Motilal Nehru National Institute of Technology (MNNIT) Allahabad ARUN KUMAR MISRA Professor Department of Computer Science and Engineering Motilal Nehru National Institute of Technology (MNNIT) Allahabad Delhi-110092 2014
DATA STRUCTURES: A Programming Approach with C, Second Edition (with CD-ROM) Dharmender Singh Kushwaha and Arun Kumar Misra 2014 by PHI Learning Private Limited, Delhi. All rights reserved. No part of this book may be reproduced in any form, by mimeograph or any other means, without permission in writing from the publisher. The authors and the publisher make no warranty of any kind, expressed or implied, with regard to programs contained in this companion CD. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs. ISBN-978-81-203-5029-8 The export rights of this book are vested solely with the publisher. Second Printing (Second Edition) September, 2014 Published by Asoke K. Ghosh, PHI Learning Private Limited, Rimjhim House, 111, Patparganj Industrial Estate, Delhi-110092 and Printed by Baba Barkha Nath Printers, Bahadurgarh, Haryana-124507.
To My parents Flt. Lt. Balwant Singh (Retd.) and Mrs. Savitri Singh, wife Seema, daughter Nikita & son Karan To Dharmender Singh Kushwaha My wife, Mrs. Kalpana Misra Arun Kumar Misra
Contents Preface xv 1. A Quick Overview of C Fundamentals 1 38 Introduction 1 Programming Fundamentals 1 Variables 2 Constants 2 Data Types 2 Flow Control 3 Type Conversion 5 Declaring, Initializing and Assigning Variables 5 Variable Scope 5 Operators 6 Arithmetic Operators 7 Relational Operators 7 Logical Operators 7 Assignment Operators 8 Ternary Operators 8 Bitwise Operators 9 Special Operators 9 Input/Output Instructions 9 Control Statements 11 Decision Control Instructions 11 Functions in C 16 Modifying Function Arguments 17 The Scope and Lifetime of Variables in Functions 18 Auto Specifier 18 Static Specifier 19 Register Specifier 19 Extern Specifier 20 Recursion 21 Arrays 21 Pointers 22 v
vi Contents Structures 23 sizeof Function 26 Structure and sizeof Function Structure Padding 26 Union 27 The Storage Class Qualifiers 28 Standard Library Functions 28 Let us Rewind! 29 Quiz 30 2. Introduction to Data Structure 39 60 Introduction 39 Data Types and Data Structures 40 Linear and Nonlinear Data Structures 41 Theta (Q) Notation: Average Case Behaviour 46 Big O(O) Notation: Wrost Case Behaviour 47 Omega (W) Notation: Best Case Behaviour 47 Computation of Asymptotic Notations 48 Best-case and Average-case Complexity 52 When do Constants Matter 53 Data Structures Widely Used in Computer Sciences 53 Let us Rewind! 55 Quiz 55 Review Questions 59 3. Understanding Pointers in C 61 94 Introduction 61 Pointer/Pointee 61 Dereference 61 Pointers and Arrays 66 Pointers and Two-dimensional Array 67 Operation with Pointers 69 Void Pointer or Generic Pointer 76 Dynamic Allocation of Memory 77 Dangling References 78 Null Pointers 78 Using a Null Pointer 78 Bad Pointer 78 Far and Near Pointer 79 Tricks to Tame Pointers 79 Pointers and Function 79 Pointers and Structures 81 Precedence and Associativity in Pointer Declaration 83 Miscellaneous Programs 85 Let us Rewind! 87 Quiz 88 Review Questions 94
Contents vii 4. Recursion 95 119 Introduction 95 Function Calls in Recursion 96 Recursive Function Calls Definition 96 How Recursive Function Call Works? 97 How Recursion Works? 99 Use of External Stack in Recursion 103 Recursive Data Structures 105 Type of Recursions 105 Classical Problems and Their Solution Using Recursion 110 The Tower of Hanoi Problem 110 Writing Linked List Backwards 111 Let us Rewind! 114 Quiz 115 Review Questions 118 5. Arrays 120 158 Introduction 120 1-Dimensional Array 121 Memory Space Required 121 Initializing 1-Dimensional Array 121 Implementation of 1-Dimensional Array in Memory 122 Basic Operations on Array 122 Character Array 126 Multi-dimensional Array 129 2-Dimensional Array 129 Initializing a 2-Dimensional Array 130 Implementation of 2-dimensional Array 135 3-Dimensional Array 138 Operations on Matrix 140 Matrix Addition 140 Matrix Multiplication 141 Matrix Transpose 141 Array as Parameter to a Function 144 Arrays and Pointers 145 Difference between Pointer to an Array and Array of Pointers 147 Array of Pointer 148 Array and Structures 149 Array within Structure Complex Structures 149 Array of Structure 149 Let us Rewind! 152 Quiz 152 Review Questions 158
viii Contents 6. Linked List 159 246 Introduction 159 Linked Lists Structure 160 Linked List Abstract Data Type 161 Singly Linked List 162 Insertion of an Element in a Singly Linked List 165 Deleting an Element from a Singly Linked List 166 Inserting Nodes in a Singly Linked List 166 Inserting Node at the End of Linked List 168 Deleting Nodes from a Linked List 171 Doubly Linked List 176 Inserting a Node at the Front of Doubly Linked List 177 Inserting Node at the End of a Doubly Linked List 179 Deleting a Node from the Front of a List 180 Deleting a Node from the End of a Doubly Linked List 181 Circular Linked List 182 Creating Circular Linked List 183 Operations on Circular Linked List 184 Inserting a Node at the Front of the Circular Linked List 184 Inserting a Node at the End of Circular Linked List 188 Deletion of Node from the Start of the Circular Linked List 192 Deleting Node from the Rear of Circular Linked List 197 Circularly Double Linked List 198 Inserting Node at the Front of a Doubly Circular Linked List 199 Inserting a Node at the End of the Doubly Circular Linked List 201 Deleting a Node from the Start of a Doubly Circular Linked List 210 Deleting Last Node from the Doubly Circular Linked List 210 Application of Linked List 215 Addition of Two Polynomials 215 Deciding on the Choice of the Linked List 238 Doubly Linked vs Singly Linked List 238 Circularly Linked vs Linearly Linked 238 Linked List Variants 238 Linked Lists Using Array of Nodes 239 Unrolled Linked List 239 Ordered List 240 Multi-linked list 240 Additional Useful Programs (In the CD) 241 Let us Rewind! 242 Quiz 243 Review Questions 245 7. Sorting 247 287 Introduction 247 Bubble Sort 247 Selection Sort 250
Contents ix Insertion Sort 252 Comparisons of Insertion Sort with Other Sorting Algorithms 254 Shell Sort 255 Bucket Sort 258 Heap Sort 261 Merge Sort 268 Complexity Analysis of Merge Sort 270 Quick Sort 271 Radix Sort 276 Let us Rewind! 282 Quiz 283 Review Questions 287 8. Strings 288 313 Introduction 288 String Implementation 288 A String ADT 289 Reading and Writing Strings: String I/O: gets() and puts() 290 Manipulating C Strings Using string.h 290 String Palindrome 297 Other Important Operations on Strings 299 Let us Rewind! 309 Quiz 309 Review Questions 313 9. Stacks 314 351 Introduction 314 Uses of Stacks in Computing 317 The Stack Abstract Data Type 317 Relevance of Stack in Function Call and Return 318 Push and Pop 320 Applications of Stack 322 Computation of Expressions 326 Infix Notation: a + b 327 Postfix Notation: ab+ 327 Prefix Notation: +ab 327 Reverse Polish Notation 332 Converting an Expression from Infix Notation to RPN 335 Converting Infix Expression to Prefix 340 Prefix to Infix Conversion 345 Let us Rewind! 346 Quiz 347 Review Questions 350
Data Structures: A Programming Approach 30% OFF Publisher : PHI Learning ISBN : 9788120350298 Author : Dharmender Singh Kushwaha Type the URL : http://www.kopykitab.com/product/7674 Get this ebook