Data Structures With C Programming 2nd Edition Anil Kumar Yadav - The Full Ebook Version Is Ready For Instant Download
Data Structures With C Programming 2nd Edition Anil Kumar Yadav - The Full Ebook Version Is Ready For Instant Download
com
https://textbookfull.com/product/data-structures-with-c-
programming-2nd-edition-anil-kumar-yadav/
OR CLICK HERE
DOWLOAD EBOOK
https://textbookfull.com/product/c-programming-program-design-
including-data-structures-d-s-malik/
textbookfull.com
https://textbookfull.com/product/effective-robotics-programming-with-
ros-anil-mahtani/
textbookfull.com
https://textbookfull.com/product/interpreting-lisp-programming-and-
data-structures-2nd-edition-gary-d-knott-auth/
textbookfull.com
https://textbookfull.com/product/problem-solving-in-data-structures-
algorithms-using-c-programming-interview-guide-first-edition-hemant-
jain/
textbookfull.com
Problem Solving in Data Structures Algorithms Using C
Programming Interview Guide 1st Edition Hemant Jain
https://textbookfull.com/product/problem-solving-in-data-structures-
algorithms-using-c-programming-interview-guide-1st-edition-hemant-
jain/
textbookfull.com
https://textbookfull.com/product/mathematical-programming-with-data-
perturbations-ii-2nd-edition-fiacco/
textbookfull.com
https://textbookfull.com/product/data-structures-using-c-2nd-edition-
reema-thareja-oxford-publication-2014-reema-thareja/
textbookfull.com
https://textbookfull.com/product/interpreting-lisp-programming-and-
data-structures-gary-d-knott/
textbookfull.com
ARCLER
P r e s s
www.arclerpress.com
Data Structures with C Programming
Dr. Anil Kumar Yadav and Vinod Kumar Yadav
Arcler Press
2010 Winston Park Drive,
2nd Floor
Oakville, ON L6H 5R7
Canada
www.arclerpress.com
Tel: 001-289-291-7705
001-905-616-2116
Fax: 001-289-291-7601
Email: [email protected]
This book contains information obtained from highly regarded resources. Reprinted material
sources are indicated and copyright remains with the original owners. Copyright for images and
other graphics remains with the original owners as indicated. A Wide variety of references are
listed. Reasonable efforts have been made to publish reliable data. Authors or Editors or Publish-
ers are not responsible for the accuracy of the information in the published chapters or conse-
quences of their use. The publisher assumes no responsibility for any damage or grievance to the
persons or property arising out of the use of any materials, instructions, methods or thoughts in
the book. The authors or editors and the publisher have attempted to trace the copyright holders
of all material reproduced in this publication and apologize to copyright holders if permission has
not been obtained. If any copyright holder has not been acknowledged, please write to us so we
may rectify.
Notice: Registered trademark of products or corporate names are used only for explanation and
identification without intent of infringement.
Arcler Press publishes wide variety of books and eBooks. For more information about
Arcler Press and its products, visit our website at www.arclerpress.com
ABOUT THE AUTHORS
Dr. Anil Kumar Yadav has done his B.Tech (CSE), M.Tech (I.T)
and PhD (Computer Science and Engineering) in the area of Machine
Learning specialization in Reinforcement Learning and Artificial
Intelligence. He has filed patent on topic Machine Learning, “Method
for Reinforcement Learning,”. He has been teaching data structures and
Artificial intelligence over the last 13 years at under graduate and post
graduate levels. With over a decade of teaching experience in the
engineering institutions, he has exemplified his work at various
designations. His research interests include data structures, deep
learning , machine learning algorithms specially reinforcement
learning, computational intelligence, computer vision and image
processing, brain computer/machine interface, intelligent informatics, soft-
computing in modeling and control, cognitive science. He has presented
papers at national and international conferences and published several
papers in international journals and also attended several workshops. He is
also a reviewer of some of the international journal and conferences.
Vinod Kumar Yadav has done his B.Tech (I.T), M.Tech (CSE). His area
of interest is Data mining, Artificial Intelligence and Machine Learning. He
has been teaching data structures over the last 7 years at under graduate and
post graduate levels. He is also GATE Qualified in Information Technology
in 2007. He has published many papers at national, international conferences
and international Journals. He also attended several national workshops.
TABLE OF CONTENTS
List of Figures.................................................................................................xi
Preface.................................................................................................... ....xvii
Chapter 2 Array........................................................................................................ 35
2.1. Introduction....................................................................................... 36
2.2. Application of Array........................................................................... 36
2.3. Definition of Array............................................................................. 37
2.4. Representation of Array...................................................................... 37
2.5. Ordered List....................................................................................... 56
2.6. Sparse Matrices.................................................................................. 57
2.7. Garbage Collection............................................................................ 60
Chapter 3 Recursion................................................................................................. 63
3.1. Introduction....................................................................................... 64
3.2. Recursion.......................................................................................... 64
3.3. Tower of Hanoi ................................................................................. 72
3.4. Backtracking ..................................................................................... 74
Chapter 4 Stack........................................................................................................ 77
4.1. Introduction....................................................................................... 78
4.2. Definition of Stack............................................................................. 78
4.3. Operations on Stack.......................................................................... 80
4.4. Disadvantages of Stack...................................................................... 94
4.5. Applications of Stack......................................................................... 94
4.6. Expressions (Polish Notation)............................................................. 95
4.7. Evaluation of Postfix Expression......................................................... 98
4.8. Decimal To Binary Conversion........................................................... 99
4.9. Reversing The String......................................................................... 102
viii
Chapter 8 Graph..................................................................................................... 213
8.1. Introduction..................................................................................... 214
8.2. Definition of Graph......................................................................... 214
8.3. Representation of Graphs................................................................. 221
8.4. Graph Traversal................................................................................ 224
8.5. Spanning Tree.................................................................................. 229
8.6. Shortest Path Problem...................................................................... 240
8.7. Application Of Graph...................................................................... 243
Index...................................................................................................... 309
ix
LIST OF FIGURES
xi
Figure 2.4 Output of Program 2.3.
Figure 2.5 Array positions.
Figure 2.6 3 x 4 array two-dimensional.
Figure 2.7 Row major store in an array.
Figure 2.8 Column major store in an array.
Figure 2.9 Output of Program 2.4.
Figure 2.10 Output of Program 2.5
Figure 2.11 Output of Searching.
Figure 2.12 Output of Program Insertion.
Figure 2.13 Output of Program Deletion.
Figure 2.14 Output of Program Deletion.
Figure 2.15 Polynomial representation.
Figure 2.16 Sparse matrix.
Figure 2.17 Output of Sparse Matrix or not.
Figure 3.1 Output of a Factorial number Using Iteration Method.
Figure 3.2 Output of a Factorial number Using Recursion Method.
Figure 3.3 Output of Fibonacci Series.
Figure 3.4 Output for Check Prime number.
Figure 3.5 Initial Setup of Tower of Hanoi
Figure 3.6 After applying first Step position of Tower
Figure 3.7 After applying second step position of tower
Figure 3.8 After applying third step position of tower.
Figure 3.9 After applying fourth step position of tower.
Figure 3.10 Tree Example.
Figure 4.1 Stack Containing Items.
Figure 4.2 Stack using a 1-dimensional array.
Figure 4.3 Stack empty condition
Figure 4.4 Stack full condition.
Figure 4.5 Performing Push Operation.
Figure 4.6 Performing Pop Operation.
Figure 4.7 Stack Operation.
xii
Figure 4.8 Output Example 4.1.
Figure 4.9 Output of stack operation using linked list.
Figure 4.10 Output of Decimal to Binary
Figure 4.11 Output Reverse a String.
Figure 5.1 Queue structure.
Figure 5.2 Array Implementation of Queue.
Figure 5.3 (a) Queue in Memory.
Figure 5.3 (b) Queue after deleting the first element.
Figure 5.3 (c) Queue after inserting an element.
Figure 5.4 Output of Queue Operations.
Figure 5.5 Output of Queue operation using Linked List.
Figure 5.6 Linear Queue.
Figure 5.7 Circular Queue.
Figure 5.8 Output of Circular Queue Operations
Figure 5.9 D-Queue.
Figure 5.10 (a) Input-restricted D-queue.
Figure 5.10 (b) Output-restricted D-queue.
Figure 5.11 Output of D-Queue Operations.
Figure 5.12 Output of Priority Queue.
Figure 6.1 Structure of a Node
Figure 6.2 Representation of link list.
Figure 6.3 Output of Linked List.
Figure 6.4 A singly-linked list containing three integer values.
Figure 6.5 Output of Single linked list operations.
Figure 6.6 Output of Circular Linked List Operations.
Figure 6.7 Doubly Linked List.
Figure 6.8 Output of Doubly linked list Operations.
Figure 6.9 Structure of a polynomial node.
Figure 6.10 List structure of polynomial.
Figure 6.11 Linked representation of a polynomial two variable.
Figure 7.1 Tree.
Figure 7.2 a, b and c represent a forest.
xiii
Figure 7.3 An example of Tree.
Figure 7.4 Binary tree.
Figure 7.5 Strictly Binary Tree
Figure 7.6 Almost Complete.
Figure 7.7 Complete Binary Tree.
Figure 7.8 Extended Binary Tree.
Figure 7.9 (a) Left -skewed binary tree (b) Right – skewed binary tree.
Figure 7.10 General Tree.
Figure 7.11 Expression Tree.
Figure 7.12 Sequential representation of a binary tree.
Figure 7.13 Structure of a node binary tree.
Figure 7.14 Linked List Representation.
Figure 7.15 Binary Tree (a) and (b).
Figure 7.16 Binary Tree (a) and (b).
Figure 7.17 Binary Tree (a) and (b).
Figure 7.18 Finally Binary Tree.
Figure 7.19 Computing pointer in a binary tree.
Figure 7.20 A threaded binary tree.
Figure 7.21 Binary Search Tree.
Figure 7.22 Binary Search Tree.
Figure 7.23 Before deletion.
Figure 7.24 After deletion.
Figure 7.25 Before deletion.
Figure 7.26 After deletion of a node from the tree.
Figure 7.27 Before deletion.
Figure 7.28 After the deletion of a node from the tree.
Figure 7.29 Red-Black Tree.
Figure 7.30 AVL tree.
Figure 7.31 Not an AVL tree.
Figure 7.32 Classification of Rotation.
Figure 7.33 Balanced AVL search tree.
Figure 7.34 AVL search tree after performing LL rotation.
xiv
Figure 7.35 Balanced AVL search tree.
Figure 7.36 AVL search tree after performing RR rotation
Figure 7.37 Balanced AVL search tree.
Figure 7.38 AVL search tree after performing LR rotation.
Figure 7.39 Balanced AVL search tree.
Figure 7.40 AVL search tree after performing RL rotation.
Figure 7.41 AVL tree Exercise of 7.4
Figure 7.42 Weight Balance Tree.
Figure 8.1 Simple Graph.
Figure 8.2 A Directed Graph.
Figure 8.3 An Undirected Graph.
Figure 8.4 A Complete Graph.
Figure 8.5 A Sub Graph.
Figure 8.6 A connected graph.
Figure 8.7 A Multigraph.
Figure 8.8 A Graph with four Vertices.
Figure 8.9 A directed graph with four Vertices.
Figure 8.10 A Null Graph.
Figure 8.11 An Isomorphic Graph.
Figure 8.12 A Homeomorphic Graph.
Figure 8.13 An Undirected Graph.
Figure 8.14 A Directed Graph.
Figure 8.15 Linked list representation of Figure 8.14.
Figure 8.16 An Undirected Graph.
Figure 8.17 Linked list of Figure 8.16.
Figure 8.18 Graph with six vertices.
Figure 8.19 An undirected graph.
Figure 8.20 An undirected graph.
Figure 8.21 An undirected graph.
Figure 8.22(a) Undirected Graph.
Figure 8.22(b) Spanning Tree.
Figure 8.23 Undirected Graph.
xv
Figure 8.24 Minimum spanning tree Graph of 8.23.
Figure 8.25 Undirected Graph G.
Figure 8.26 A Minimum Spanning tree of Figure 8.25.
Figure 8.27 Undirected Graph G.
Figure 8.28 Undirected Graph G.
Figure 8.29 A Minimum Spanning tree of Figure 8.27
Figure 8.30 Unweighted graph
Figure 8.31 A Graph.
Figure 9.1 Output of Bubble Sort.
Figure 9.2 Output of Insertion Sort.
Figure 9.3 Output of Selection Sort.
Figure 9.4 Output of Merge Sort element.
Figure 9.5 Output of merge sort for two unsorted list.
Figure 9.6 Quick Sort.
Figure 9.7 Output of Quick Sort element.
Figure 9.8 Output of Heap Sort.
Figure 9.9 Output of Radix sort elements.
Figure 10.1 Output for Binary search.
Figure 10.2 A small phone directory book as a hash table.
Figure 10.3 Chaining.
Figure 10.4 Rehashing.
xvi
PREFACE
The significant role of Data Structures is famous for Computer Science and
Engineering. This book is about the structure, actions and the principle of a
different data type that help improve the ability to write an efficient algorithm,
program and Analysis algorithm and program complexity. In this book, we
present the fundamental concepts, operations, and algorithms for different types
of data structures. This makes an understanding of this subject more lucid and
makes it more interesting. This book, intended for the first course in the data
structure at the junior or senior undergraduate level.
This book prepared according to the syllabus of various Universities and
conceived as a textbook for the course of Bachelor of Engineering or Technology
of different branches of computer science. The contents of the book have been
thoroughly organized and spread over ten chapters. Each chapter begins with
the basics and describes with syntax, diagrams, and examples. Each concept
of data structure has implemented in the C programming language. The book
not only covers the scope of the subject but also explains the philosophy of the
subject.
We trust the textbook would meet the requirements of both the teachers and
the students. We would very much appreciate receiving any suggestions for
improvement in the book from teachers, students, and other readers.
We wish to express our deep thanks to all those who helped in making this book
a reality.
We express our gratitude to our publisher and the team of publications that have
taken great pains in publishing the book with speed and accuracy.
1
CHAPTER
DATA STRUCTURES
CONTENTS
1.1. Introduction......................................................................................... 2
1.2. Data Structure Basic Terminology........................................................ 2
1.3. Data Structure...................................................................................... 3
1.4. Introduction To Algorithm.................................................................... 8
1.5. Basic Concept of Function................................................................. 13
1.6. Basic Concept of Pointers.................................................................. 19
1.7. Introduction To Structure.................................................................... 22
1.8. Dynamic Memory Allocation In Data Structure................................. 28
2 Data Structures with C Programming
1.1. INTRODUCTION
In the Computer Programming or Software development, Data Structures is
one of the most valuable roles for computer engineers. Use of appropriate
data structures enables a computer system to perform its task more efficiently,
by influencing the ability of computers to store and retrieve data from any
location in its memory. A data structure is a method of how storing data on
a computer so that it can be used efficiently. In Computer Programming we
are using different types of data to perform, store, access and sent data and
information. Computers cannot do without data, so organizing that data is
very important. If that data is not organized effectively, it is very difficult to
perform any task on that data. If it is organized effectively then any operation
can be performed easily on that data.
The time and space are factors which determine the efficiencies of the
program. The time required for execution of the program cannot be computed
in terms of seconds because of the following factors.
1. The hardware of the machine.
2. The amount of time required by each machine instruction.
3. The amount of time required by the compilers to execute the
instruction.
4. The instruction set.
18. These sayings put together, show the apostle's views most clearly, so as
to admit of no mistake on the mind of any individual. He says that all things
that pertain to life and godliness were given unto them through the
knowledge of God and our Saviour Jesus Christ. And if the question is
asked, how were they to obtain the knowledge of God? (for there is a great
difference between believing in God and knowing him: knowledge implies
more than faith. And notice, that all things that pertain to life and godliness
were given through the knowledge of God;) the answer is given, through
faith they were to obtain this knowledge; and, having power by faith to
obtain the knowledge of God, they could with it obtain all other things
which pertain to life and godliness.
20. From this we may extend as far as any circumstances may require
whether on earth or in heaven, and we will find it the testimony of all
inspired men, or heavenly messengers, that all things that pertain to life and
godliness are the effects of faith and nothing else; all learning, wisdom and
prudence fail, and every thing else as a means of salvation but faith. This is
the reason that the fishermen of Galilee could teach the world—because
they sought by faith and by faith obtained. And this is the reason that Paul
counted all things but filth and dross—what he formerly called his gain he
called his loss; yea, and he counted all things but loss for the excellency of
the knowledge of Christ Jesus the Lord. Phil 3:7,8,9 & 10. Because, to
obtain the faith by which he could enjoy the knowledge of Christ Jesus the
Lord, he had to suffer the loss of all things: this is the reason that the
Former Day Saints knew more, and understood more of heaven, and of
heavenly things than all others beside, because this information is the effect
of faith—to be obtained by no other means. And this is the reason, that men,
as soon as they lose their faith, run into strifes, contentions, darkness, and
difficulties; for the knowledge which tends to life disappears with faith, but
returns when faith returns; for when faith comes, it brings its train of
attendants with it—apostles, prophets, evangelists, pastors, teachers, gifts,
wisdom, knowledge, miracles, healings, tongues, interpretation of tongues,
&c. All these appear when faith appears on the earth, and disappear when it
disappears from the earth. For these are the effects of faith, and always
have, and always will, attend it. For where faith is, there will the knowledge
of God be also, with all things which pertain thereto revelations, visions,
and dreams, as well as every other necessary thing, in order that the
possessors of faith may be perfected and obtain salvation; for God must
change, otherwise faith will prevail with him. And he who possesses it will,
through it, obtain all necessary knowledge and wisdom, until he shall know
God, and the Lord Jesus Christ, whom he has sent: whom to know is eternal
life: Amen.
Transcriber's Note
This version of the Lectures on Faith is meant to match the relevant portion
of the 1835 edition of the Doctrine and Covenants. Scans are available, for
example, at http://www.josephsmithpapers.org/paper-summary/doctrine-
and-covenants-1835/13
*** END OF THE PROJECT GUTENBERG EBOOK THE LECTURES ON
FAITH ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com