×
DREAM IT. BELIEVE IT. ACHIEVE IT.
--Your friends at LectureNotes
Close

Data Structure using C

by Jntu HeroesJntu Heroes
Type: NoteInstitute: Jawaharlal nehru technological university anantapur college of engineering Offline Downloads: 150Views: 4865Uploaded: 1 year ago

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Jntu Heroes
Jntu Heroes
Contents 1 Introduction 1.1 What this book is, and what it isn’t . . . 1.2 Assumed knowledge . . . . . . . . . . . . 1.2.1 Big Oh notation . . . . . . . . . . 1.2.2 Imperative programming language 1.2.3 Object oriented concepts . . . . . 1.3 Pseudocode . . . . . . . . . . . . . . . . . 1.4 Tips for working through the examples . . 1.5 Book outline . . . . . . . . . . . . . . . . 1.6 Testing . . . . . . . . . . . . . . . . . . . 1.7 Where can I get the code? . . . . . . . . . 1.8 Final messages . . . . . . . . . . . . . . . I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data Structures 2 Linked Lists 2.1 Singly Linked List . . . . . 2.1.1 Insertion . . . . . . . 2.1.2 Searching . . . . . . 2.1.3 Deletion . . . . . . . 2.1.4 Traversing the list . 2.1.5 Traversing the list in 2.2 Doubly Linked List . . . . . 2.2.1 Insertion . . . . . . . 2.2.2 Deletion . . . . . . . 2.2.3 Reverse Traversal . . 2.3 Summary . . . . . . . . . . 1 1 1 1 3 4 4 6 6 7 7 7 8 . . . . . . . . . . . 9 9 10 10 11 12 13 13 15 15 16 17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . in the binary search tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 20 21 22 24 24 25 26 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . reverse order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Binary Search Tree 3.1 Insertion . . . . . . . . . . . . . . . . . 3.2 Searching . . . . . . . . . . . . . . . . 3.3 Deletion . . . . . . . . . . . . . . . . . 3.4 Finding the parent of a given node . . 3.5 Attaining a reference to a node . . . . 3.6 Finding the smallest and largest values 3.7 Tree Traversals . . . . . . . . . . . . . 3.7.1 Preorder . . . . . . . . . . . . . I 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.8 3.7.2 Postorder . . 3.7.3 Inorder . . . 3.7.4 Breadth First Summary . . . . . . 4 Heap 4.1 Insertion . 4.2 Deletion . 4.3 Searching 4.4 Traversal 4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 29 30 31 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 33 37 38 41 42 5 Sets 5.1 Unordered . . . . 5.1.1 Insertion . 5.2 Ordered . . . . . 5.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 46 46 47 47 6 Queues 6.1 A standard queue . . 6.2 Priority Queue . . . 6.3 Double Ended Queue 6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 49 49 49 53 7 AVL Tree 7.1 Tree Rotations . 7.2 Tree Rebalancing 7.3 Insertion . . . . . 7.4 Deletion . . . . . 7.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 56 57 58 59 61 II . . . . . . . . . . . . . . . . . . . . . . . . . Algorithms 8 Sorting 8.1 Bubble Sort . 8.2 Merge Sort . 8.3 Quick Sort . . 8.4 Insertion Sort 8.5 Shell Sort . . 8.6 Radix Sort . 8.7 Summary . . 62 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 63 63 65 67 68 68 70 9 Numeric 72 9.1 Primality Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 9.2 Base conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 9.3 Attaining the greatest common denominator of two numbers . . 73 9.4 Computing the maximum value for a number of a specific base consisting of N digits . . . . . . . . . . . . . . . . . . . . . . . . . 74 9.5 Factorial of a number . . . . . . . . . . . . . . . . . . . . . . . . 74 9.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 II 2
10 Searching 76 10.1 Sequential Search . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 10.2 Probability Search . . . . . . . . . . . . . . . . . . . . . . . . . . 76 10.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 11 Strings 11.1 Reversing the order of words in a sentence . . . . . . . . . . . 11.2 Detecting a palindrome . . . . . . . . . . . . . . . . . . . . . 11.3 Counting the number of words in a string . . . . . . . . . . . 11.4 Determining the number of repeated words within a string . . 11.5 Determining the first matching character between two strings 11.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 79 80 81 83 84 85 A Algorithm Walkthrough 86 A.1 Iterative algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 86 A.2 Recursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 88 A.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 B Translation Walkthrough 91 B.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 C Recursive Vs. Iterative Solutions 93 C.1 Activation Records . . . . . . . . . . . . . . . . . . . . . . . . . . 94 C.2 Some problems are recursive in nature . . . . . . . . . . . . . . . 95 C.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 D Testing D.1 What constitutes a unit test? . D.2 When should I write my tests? D.3 How seriously should I view my D.4 The three A’s . . . . . . . . . . D.5 The structuring of tests . . . . D.6 Code Coverage . . . . . . . . . D.7 Summary . . . . . . . . . . . . E Symbol Definitions . . . . . . . . . . . . test suite? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 . 97 . 98 . 99 . 99 . 99 . 100 . 100 101 III 3
Chapter 1 Introduction 1.1 What this book is, and what it isn’t This book provides implementations of common and uncommon algorithms in pseudocode which is language independent and provides for easy porting to most imperative programming languages. It is not a definitive book on the theory of data structures and algorithms. For the most part this book presents implementations devised by the authors themselves based on the concepts by which the respective algorithms are based upon so it is more than possible that our implementations differ from those considered the norm. You should use this book alongside another on the same subject, but one that contains formal proofs of the algorithms in question. In this book we use the abstract big Oh notation to depict the run time complexity of algorithms so that the book appeals to a larger audience. 1.2 Assumed knowledge We have written this book with few assumptions of the reader, but some have been necessary in order to keep the book as concise and approachable as possible. We assume that the reader is familiar with the following: 1. Big Oh notation 2. An imperative programming language 3. Object oriented concepts 1.2.1 Big Oh notation For run time complexity analysis we use big Oh notation extensively so it is vital that you are familiar with the general concepts to determine which is the best algorithm for you in certain scenarios. We have chosen to use big Oh notation for a few reasons, the most important of which is that it provides an abstract measurement by which we can judge the performance of algorithms without using mathematical proofs. 1 4

Lecture Notes