Advanced Topics in C Core Concepts in Data Structures mmm Noel Kalicharan Apress-
Contents J About the Author About the Technical Reviewer Preface xiii xv xvii Chapter 1: Sorting, Searching, and Merging 1 1.1 Sorting an Array: Selection Sort 1 1.1.1 Analysis of Selection Sort 5 1.2 Sorting an Array: Insertion Sort 5 1.2.1 Analysis of Insertion Sort 11 1.3 Inserting an Element in Place 11 1.4 Sorting an Array of Strings 12 1.5 Sorting Parallel Arrays 14 1.6 Binary Search 15 1.7 Searching an Array of Strings 17 1.8 Example: Word Frequency Count 18 1.9 Merging Ordered Lists 22 1.9.1 Implementing the Merge 23 Chapter 2: Structures 27 2.1 Defining Structures 27 2.2 How to Declare a Structure 28 2.2.1 typedef 30 2.3 Working with an Array of Structures 32 2.4 Searching an Array of Structures 33 vii
2.5 Sorting an Array of Structures 33 2.6 How to Read, Search, and Sort a Structure 34 2.7 Nested Structures 38 2.8 Working with Fractions 39 2.8.1 Manipulating Fractions 39 2.9 A Voting Problem 41 2.10 Passing Structures to Functions 47 Chapter 3: Pointers 51 3.1 Defining Pointers 51 3.2 Passing Pointers as Arguments 53 3.3 More on Passing an Array as an Argument 55 3.4 Character Pointers 57 3.5 Pointer Arithmetic 58 3.6 Pointers to Structures 60 3.7 Pointers to Functions 62 3.8 Void Pointers 65 Chapter 4: Linked Lists 69 4.1 Defining Linked Lists 69 4.2 Basic Operations on a Linked List 71 4.2.1 Counting the Nodes in a Linked List 71 4.2.2 Searching a Linked List 72 4.2.3 Finding the Last Node in a Linked List 73 4.3 Dynamic Storage Allocation: malloc, calloc, sizeof, free 73 4.3.1 malloc 74 4.3.2 calloc. 75 4.3.3 sizeof 75 4.3.4 free 76 4.4 Building a Linked List: Adding New Item at the Tail 77 4.5 Insertion into a Linked List 80 viii
4.6 Building a Linked List: Adding a New Item at the Head 82 4.7 Deletion from a Linked List 83 4.8 Building a Sorted Linked List 84 4.9 Example: Palindrome 88 4.10 Saving a Linked List 91 4.11 Arrays vs. Linked Lists 91 4.12 Storing a Linked List Using Arrays 92 4.13 Merging Two Sorted Linked Lists 93 4.14 Circular and Two-Way Linked Lists 97 4.14.1 Circular Lists 97 4.14.2 Two-Way (Doubly Linked) Lists 100 Chapter 5: Stacks and Queues 103 5.1 Abstract Data Types 103 5.2 Stacks 103 5.2.1 Implementing a Stack Using an Array 104 5.2.2 Implementing a Stack Using a Linked List 108 5.3 Creating a Stack Header File 111 5.4 A General Stack Type 112 5.4.1 Example: Convert from Decimal to Binary 116 5.5 Converting Infix to Postfix 117 5.5.1 Evaluating a Postfix Expression 121 5.6 Queues 122 5.6.1 Implementing a Queue Using an Array 123 5.6.2 Implementing a Queue Using a Linked List 127 Chapter 6: Recursion 133 6.1 Recursive Definition 133 6.2 Writing Recursive Functions in C 134 6.3 Converting a Decimal Number to Binary Using Recursion 137 6.4 Printing a Linked List in Reverse Order 139 ix
6.5 Towers of Hanoi 140 6.6 Writing Power Functions 142 6.7 Merge Sort 143 6.8 static Variables.147 6.8.1 Internal Static 147 6.8.2 External Static 147 6.9 Counting Organisms 148 6.10 Finding a Path Through a Maze 152 6.10.1 Writing the Program 154 Chapter 7: Random Numbers, Games, and Simulation 159 7.1 Random Numbers 159 7.2 Random and Pseudorandom Numbers 160 7.3 Generating Random Numbers by Computer 160 7.4 A Guessing Game 163 7.5 Drills in Addition 164 7.6 Nim 166 7.7 Non-uniform Distributions 169 7.7.1 Collecting Bottle Caps 170 7.8 Simulation of Real-Life Problems 172 7.9 Simulating a Queue 173 7.9.1 Programming the Simulation 175 7.10 Estimating Numerical Values Using Random Numbers 177 7.10.1 Estimating Js 177 7.10.2 Estimating 7i 178 Chapter 8: Working with Files 183 8.1 Reading Data from a File 183 8.1.1 fscanf 184 8.1.2 Finding the Average of Some Numbers in a File 185 x
8.2 Sending Output to a File 186 8.2.1 fprintf 186 8.3 Text and Binary Files 187 8.4 Internal vs. External File Name 188 8.5fopen andfclose 189 8.6 getcand putc 191 8.7 feof and terror 192 8.8fgetsandfputs 193 8.8.1 Example: Comparing Two Files 193 8.9 Input/Output for Binary File 195 8.9.1 fread and fwrite : 195 8.10 Random Access Files 198 8.10.1 rewind and fseek 198 8.11 Indexed Files 202 8.12 Updating a Random Access File 208 Chapter 9: Introduction to Binary Trees 213 9.1 Trees 213 9.2 Binary Trees 215 9.3 Traversing a Binary Tree 216 9.4 Representing a Binary Tree 219 9.5 Building a Binary Tree 220 9.6 Binary Search Trees 224 9.7 Building a Binary Search Tree 227 9.7.1 Example: Word Frequency Count 228 9.8 An Array as a Binary Tree Representation 231 9.9 Some Useful Binary Tree Functions 234 9.10 Binary Search Tree Deletion 235 xi
Chapter 10: Advanced Sorting 241 10.1 Heapsort 241 10.1.1 Converting a Binary Tree into a Max-Heap 242 10.1.2 The Sorting Process 243 10.2 Building a Heap Using siftup 247 10.3 Analysis of Heapsort 250 10.4 Heaps and Priority Queues 251 10.5 Sorting a List of Items with Quicksort 251 10.5.1 AnotherWay to Partition 254 10.5.2 Nonrecursive Quicksort 257 10.5.3 Finding the k* Smallest Number 258 10.6 Shell (Diminishing Increment) Sort 259 Chapter 11: Hashing 265 11.1 Hashing Fundamentals 265 11.1.1 The Search and Insert Problem 265 11.2 Solving the Search and Insert Problem by Hashing 266 11.2.1 The Hash Function 269 11.2.2 Deleting an Item from a Hash Table 270 11.3 Resolving Collisions 271 11.3.1 Linear Probing 271 11.3.2 Quadratic Probing 273 11.3.3 Chaining 274 11.3.4 Linear Probing with Double Hashing 280 11.4 Example: Word Frequency Count 281 Index 287 xii