a practical approach to data structures and algorithms

4
A Practical Approach to Data Structures and Algorithms SAN J AY PAHUJA Software Engineer & Trainer New Age Science Limited The Control Centre, 11 A Little Mount Sion Tunbridge Wells, Kent TNI 1YS, UK www.newagescience.co.uk e-mail: [email protected]

Upload: others

Post on 21-Apr-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Practical Approach to Data Structures and Algorithms

A Practical Approach to

Data Structures and

Algorithms

SAN J AY PAHUJA Software Engineer & Trainer

New Age Science Limited The Control Centre, 11 A Little Mount Sion

Tunbridge Wells, Kent TNI 1YS, UK www.newagescience.co.uk • e-mail: [email protected]

Page 2: A Practical Approach to Data Structures and Algorithms

Preface

Contents

Chapter 1: Introduction to Data Structures and Algorithms

1.1 Introduction to data representation 1 1.2 Review of data structures-Array, Pointer, Structure, Lists, Trees, and Graphs 2 1.3 What is an Algorithm? 4 1.4 Designing Algorithms 6 1.5 Analyzing Algorithms 9 1.6 Mathematical Notation and Functions 12 1.7 Asymptotic Notation (0,6,0) 16 1.8 Performance Measurement 17

Chapter 2: Built-in Data Structure

2.1 Abstract Data Type 18 2.2 Arrays 18 2.3 Functions 18

2.4 Structures and Unions 26 2.5 Pointers 27 2.6 Memory Management in ' C 29

Chapter 3: Unordered and Ordered Arrays

3.1 One Dimensional Arrays 30

3.2 Two Dimensional Arrays 41 3.3 Multidimensional Arrays 50 3.4 Ordered Arrays 55 3.5 Sparse Matrix 62

Page 3: A Practical Approach to Data Structures and Algorithms

viii / Contents

Chapter 4: Linear Data Structure—Linked List 4.1 Linear Linked List and Operations 66 4.2 Circular Linked Linear List and Operations 100 4.3 Doubly Linear Linked List and Operations 116 4.4 Applications of Linked List 137 4.4.1 Polynomial Manipulation 137

4.4.2 Multiple-Precision Arithmetic 161

4.5 Set Operations on Linked List 166

Chapter 5: Algorithms on Stack

5.1 Representation Using Array and Linked List 174

5.2 Push and Pop Operation 177 5.3 Representation of expressions: Infix, Postfix, Prefix 182 5.4 Evaluation of the Postfix expression 184 5.5 Transforming Infix Expression into Postfix Expression 189 5.6 Recursion 196

Chapter 6: Algorithms on Queue

6.1 Representation: Using Array and Linked List 208 6.2 Insertion and Deletion Operations 210 6.3 Circular Queue 216 6.4 Double Ended Queues (DEQues) 221 6.5 Priority Queues 226

6.6 Multiple Queues 231

Chapter 7: Non-Linear Data Structure: Trees

7.1 General Concept 234 7.2 Binary Tree 236

7.3 Sequential and Linked List Representation of Binary Tree 238 7.4 Binary Tree Traversal Algorithm: Recursive and Non-recursive 240

7.5 Threaded Binary Tree Traversal 255 7.6 General Tree and Its Conversion 261 7.7 Binary Search Tree (BST) 271 7.8 Height Balanced Trees: AVL 292

7.9 B-trees 310 7.10 Applications of Trees 315

Chapter 8: Non-Linear Data Structure: Graphs

8.1 Properties of Graphs 319 8.2 Representation of Graphs 321

Page 4: A Practical Approach to Data Structures and Algorithms

Contents I '

8.3 Traversal Algorithms-Depth First Search, Breadth First Search 323 8.4 Minimum Cost Spanning Tree 335 8.5 Biconnectivity 347 8.6 Strong Connectivity 352 8.7 Transitive Closure Algorithm 355 8.8 Shortest Path Algorithms 357

8.9 Applications of Graph 367

Chapter 9: Sorting Algorithms and Their Analysis

9.1 Internal and External Sorting 369 9.2 Sorting Problem 370

9.2.1 Bubble Sort 372 9.2.2 Selection Sort 376 9.2.3 Insertion Sort 382 9.2.4 Shell sort 385 9.2.5 Address Calculation Sort 390 9.2.6 Radix Sort 390

9.2.7 Merge Sort 395 9.2.8 Quick Sort 402 9.2.9 Heap Sort 409

Chapter 10: Searching Techniques

10.1 Sequential Search 433 10.2 Binary Search 433 10.3 Hashing 434

10.3.1 Hash Functions 436 10.3.2 Collison Resolution Techniques 438 10.4 Dynamic Memory Allocation 451

Chapter 11: File Structures

11.1 Definition and Concept 461

11.2 File Organization 462 11.3 Files in ' C 465

• Appendix: Conceptual Problem Solutions 473

• Index 553

• CD-Index 559