Beginning Data Structures Using C
4.5/5
()
About this ebook
A beginner of the Data structures, who has some basic knowledge of C, could find this book interesting and simple. Every program has a proper step by step explanation of each line of code. It contains the practical implementation of stacks, queues, linked lists, trees, graphs, searching and sorting techniques. Also, recursion has been explained in an easy manner with the numerous examples. However if you find any mistake, or want to give some suggestions for the improvement of this book, then the same may be sent to the publishers, so that the mistakes may be rectified and the suggestions may be incorporated.
Topics, which are covered in this book, are:
1. INTRODUCTION TO DATA STRUCTURES
1.1 ARRAYS
1.2 STACKS
1.3 QUEUES
1.4 LINKED LISTS
1.5 TREES
1.6 GRAPHS
1.7 DATA STRUCTURE OPERATIONS
2. STACKS
2.1 POLISH NOTATION
2.2 TRANSFORMING AN INFIX EXPRESSION INTO A POSTFIX EXPRESSION
2.3 EVALUATION OF A POSTFIX EXPRESSION
3. QUEUES
3.1 CIRCULAR QUEUE
3.2 PRIORITY QUEUES
3.3 DEQUES
3.4 INPUT RESTRICTED DEQUE
3.5 OUTPUT RESTRICTED DEQUE
4. RECURSION
4.1 BACKTRACKING
4.2 FACTORIAL OF A NUMBER
4.3 MULTIPLYING TWO NUMBERS USING RECURSION
4.4 GREATEST COMMON DIVISOR
4.5 FIBONACCI SERIES
4.6 BINARY SEARCH USING RECURSION
4.7 TOWERS OF HANOI
4.8 8 QUEENS PROBLEM
4.9 GENERATING PERMUTATIONS
4.10 TO FIND OUT THE DETERMINANT OF A MATRIX
4.11 INVERSE OF A MATRIX
4.12 A RECURSIVE PROBLEM
5. LINKED LISTS
5.1 LINEAR LINKED LIST
5.2 CIRCULAR LINKED LIST
5.3 DOUBLY LINKED LIST
6. STACKS AND QUEUES USING LINKED LISTS
6.1 STACKS USING LINKED-LIST
6.2 QUEUE USING LINKED-LIST
6.3 PRIORITY QUEUE USING LINKED-LIST
7. TREES
7.1 BINARY TREES
7.2 COMPLETE BINARY TREES
7.3 DEPTH (OR HEIGHT) OF A TREE
7.4 BINARY SEARCH TREES
7.5 TRAVERSING IN TREES WITHOUT USING RECURSION
7.6 HEIGHT BALANCED TREES; AVL TREES
7.7 THREADED BINARY TREES; INORDER THREADING
8. GRAPHS
8.1 SIMPLE GRAPH
8.2 DIGRAPH (DIRECTED GRAPH)
8.3 SIMPLE DIRECTED GRAPH
8.4 WEIGHTED GRAPH
8.5 PATH
8.6 CYCLE
8.7 CONNECTED GRAPH
8.8 COMPLETE GRAPH
8.9 INCIDENCE AND DEGREE
8.10 NULL GRAPH
8.11 ADJACENCY MATRIX
8.12 PATH MATRIX
8.13 WARSHALL'S ALGORITHM
8.14 SHORTEST PATH ALGORITHM
8.15 GRAPH COLORING
8.16 HAMILTONIAN CYCLES
8.17 ADJACENCY LIST
8.18 GRAPH TRAVERSAL
8.19 MINIMUM COST SPANNING TREES
8.20 TOPOLOGICAL SORT
9. SEARCHING
9.1 SEQUENTIAL SEARCH
9.2 BINARY SEARCH
10. SORTING
10.1 BUBBLE SORT
10.2 SELECTION SORT
10.3 INSERTION SORT
10.4 SHELL SORT
10.5 MERGING OF TWO SORTED ARRAYS
10.6 MERGE SORT
10.7 MERGE SORT USING RECURSION
10.8 QUICKSORT
10.9 RADIX SORT
10.10 HEAP SORT
10.11 BINARY TREE SORT
10.12 ADDRESS CALCULATION SORT
Related to Beginning Data Structures Using C
Related ebooks
Advanced C Concepts and Programming: First Edition Rating: 3 out of 5 stars3/5Data Structures I Essentials Rating: 0 out of 5 stars0 ratingsData Structures and Algorithm Analysis in C++, Third Edition Rating: 5 out of 5 stars5/5Computer Programming In C Language Rating: 4 out of 5 stars4/5Introduction to Algorithms Rating: 0 out of 5 stars0 ratingsC++ Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsData Structures in C / C ++: Exercises and Solved Problems Rating: 0 out of 5 stars0 ratingsProgramming Problems: A Primer for The Technical Interview Rating: 4 out of 5 stars4/5Visualizing Data Structures Rating: 0 out of 5 stars0 ratingsData Structures and Algorithm Analysis in Java, Third Edition Rating: 4 out of 5 stars4/5C Programming For Dummies Rating: 0 out of 5 stars0 ratingsCoding In C Decoded: Decoded, #1 Rating: 0 out of 5 stars0 ratingsC++17 STL Cookbook Rating: 3 out of 5 stars3/5C Programming: C Programming Language for beginners, teaching you how to learn to code in C fast! Rating: 0 out of 5 stars0 ratingsA Quick Reference to Data Structures and Computer Algorithms: An Insight on the Beauty of Blockchain Rating: 0 out of 5 stars0 ratingsData Structures and Algorithms Implementation through C: Let’s Learn and Apply Rating: 0 out of 5 stars0 ratingsAdvance Core Python Programming: Begin your Journey to Master the World of Python (English Edition) Rating: 4 out of 5 stars4/5Learning Functional Data Structures and Algorithms Rating: 0 out of 5 stars0 ratingsData Structures & Algorithms Interview Questions You'll Most Likely Be Asked Rating: 1 out of 5 stars1/5Structures and C Rating: 4 out of 5 stars4/5C & Data Structures Rating: 0 out of 5 stars0 ratingsGet Programming: Learn to code with Python Rating: 0 out of 5 stars0 ratingsMastering Data Structures: Core Concepts and Principles Rating: 0 out of 5 stars0 ratingsThe Ultimate Python Programming Guide For Beginner To Intermediate Rating: 4 out of 5 stars4/5Mastering C: A Comprehensive Guide to Proficiency in The C Programming Language Rating: 0 out of 5 stars0 ratingsIntroduction to C Programming, a Practical Approach Rating: 0 out of 5 stars0 ratingsComputer Programming Using C Rating: 0 out of 5 stars0 ratingsC language Programming: Simple, Short, and Straightforward Way of Learning C Programming Rating: 3 out of 5 stars3/5Programming Concepts in C++ Rating: 0 out of 5 stars0 ratingsProgramming Problems: Advanced Algorithms Rating: 4 out of 5 stars4/5
Trending on #Booktok
It Ends with Us: A Novel Rating: 4 out of 5 stars4/5Pride and Prejudice Rating: 4 out of 5 stars4/5If We Were Villains: A Novel Rating: 4 out of 5 stars4/5The Summer I Turned Pretty Rating: 4 out of 5 stars4/5Once Upon a Broken Heart Rating: 4 out of 5 stars4/5Better Than the Movies Rating: 4 out of 5 stars4/5Crime and Punishment Rating: 4 out of 5 stars4/5Beauty and the Beast Rating: 4 out of 5 stars4/5Rich Dad Poor Dad Rating: 4 out of 5 stars4/5The Little Prince: New Translation Version Rating: 5 out of 5 stars5/5Finnegans Wake Rating: 4 out of 5 stars4/5Milk and Honey: 10th Anniversary Collector's Edition Rating: 4 out of 5 stars4/5
Related categories
Reviews for Beginning Data Structures Using C
7 ratings1 review
- Rating: 4 out of 5 stars4/5
Oct 14, 2019
don't bad for introducing to data structures. Only one thing I did not like is a lot of duplicate code to make the programs worked(if we have a section about trees all operations will include the code block to add/insert/delete leaves and etc)
Book preview
Beginning Data Structures Using C - Yogish Sachdeva
BEGINNING DATA STRUCTURES
USING
C
By:
YOGISH SACHDEVA
Published By: Yogish Sachdeva
Copyright 2003 by Yogish Sachdeva
Smashwords Edition
This ebook is licensed for your personal enjoyment only. This ebook may not be re-sold or given away to other people. If you would like to share this book with another person, please purchase an additional copy for each recipient. If you’re reading this book and did not purchase it, or it was not purchased for your use only, then please return to Smashwords.com and purchase your own copy. Thank you for respecting the hard work of this author.
PREFACE
Friends, when I was doing MCA from Panjab University, Chandigarh, I had to search books on Data Structures through C for making preparation for examinations. Though this search led me to the selection of some good books on this subject, yet I could not find any book, which might have dealt with the Programming in such a manner, so that the students like me could do the same with ease. Thus it gave me an idea of writing a book on this subject, so that a learner of this subject may find the programming interesting. It is with this object in view, that an effort has been made to give proper explanations of the programs. I hope you will find this book very helpful in your studies. However if you find any mistake, or want to give some suggestions for the improvement of this book, then the same may be sent to the publishers, so that the mistakes may be rectified and the suggestions may be incorporated in the next edition. I consider it my duty to convey my hearty thanks to the various authors, whose books have made me competent enough to write this book. In the end, I would like to thank the publishers, who have taken keen interest and rendered me their full cooperation in the publication of this book.
YOGISH SACHDEVA
Table of Contents
INTRODUCTION TO DATA STRUCTURES
1.1 ARRAYS
1.2 STACKS
1.3 QUEUES
1.4 LINKED LISTS
1.5 TREES
1.6 GRAPHS
1.7 DATA STRUCTURE OPERATIONS
STACKS
2.1 POLISH NOTATION
2.2 TRANSFORMING AN INFIX EXPRESSION INTO A POSTFIX EXPRESSION
2.3 EVALUATION OF A POSTFIX EXPRESSION
QUEUES
3.1 CIRCULAR QUEUE
3.2 PRIORITY QUEUES
3.3 DEQUES
3.4 INPUT RESTRICTED DEQUE
3.5 OUTPUT RESTRICTED DEQUE
RECURSION
4.1 BACKTRACKING
4.2 FACTORIAL OF A NUMBER
4.3 MULTIPLYING NUMBERS USING RECURSION
4.4 GREATEST COMMON DIVISOR
4.5 FIBONACCI SERIES
4.6 BINARY SEARCH USING RECURSION
4.7 TOWERS OF HANOI
4.8 8 QUEENS PROBLEM
4.9 GENERATING PERMUTATIONS
4.10 TO FIND OUT THE DETERMINANT OF A MATRIX
4.11 INVERSE OF A MATRIX
4.12 A RECURSIVE PROBLEM
LINKED LISTS
5.1 LINEAR LINKED LIST
5.2 CIRCULAR LINKED LIST
5.3 DOUBLY LINKED LIST
STACKS AND QUEUES USING LINKED LISTS
6.1 STACKS USING LINKED-LIST
6.2 QUEUE USING LINKED-LIST
6.3 PRIORITY QUEUE USING LINKED-LIST
TREES
7.1 BINARY TREES
7.2 COMPLETE BINARY TREES
7.3 DEPTH (OR HEIGHT) OF A TREE
7.4 BINARY SEARCH TREES
7.5 TRAVERSING IN TREES WITHOUT USING RECURSION
7.6 HEIGHT BALANCED TREES; AVL TREES
7.7 THREADED BINARY TREES; INORDER THREADING
GRAPHS
8.1 SIMPLE GRAPH
8.2 DIGRAPH (DIRECTED GRAPH)
8.3 SIMPLE DIRECTED GRAPH
8.4 WEIGHTED GRAPH
8.5 PATH
8.6 CYCLE
8.7 CONNECTED GRAPH
8.8 COMPLETE GRAPH
8.9 INCIDENCE AND DEGREE
8.10 NULL GRAPH
8.11 ADJACENCY MATRIX
8.12 PATH MATRIX
8.13 WARSHALL’S ALGORITHM
8.14 SHORTEST PATH ALGORITHM
8.15 GRAPH COLORING
8.16 HAMILTONIAN CYCLES
8.17 ADJACENCY LIST
8.18 GRAPH TRAVERSAL
8.19 MINIMUM COST SPANNING TREES
8.20 TOPOLOGICAL SORT
SEARCHING
9.1 SEQUENTIAL SEARCH
9.2 BINARY SEARCH
SORTING
10.1 BUBBLE SORT
10.2 SELECTION SORT
10.3 INSERTION SORT
10.4 SHELL SORT
10.5 MERGING OF TWO SORTED ARRAYS
10.6 MERGE SORT
10.7 MERGE SORT USING RECURSION
10.8 QUICKSORT
10.9 RADIX SORT
10.10 HEAP SORT
10.11 BINARY TREE SORT
10.12 ADDRESS CALCULATION SORT
Introduction to Data Structures
A data type is an instance of data that may be native to the language or defined by the user. And a composition of data types, which forms some kind of systematic organization, is called Data Structure.
Data structure = Organized data + Allowed operations
Simple Data structure can be used as building blocks of complex data structure. Array is a simple type of Data structure by using which we can build more complex data structure. Data structure can be classified according to the following four types.
1. Linear & Non-linear: The data stored in a sequence is called Linear, while the other is called Non-linear e.g. Array is Linear & Tree is Non-linear.
2. Homogenous & Non-homogenous: The data structure, which contains single type of data, is known as Homogenous whereas others are Non-homogenous. For example, Array is an ordered set of homogenous elements stored in a contiguous memory location. And Record (structure) is a Non-Homogenous.
3. Static & Dynamic: This means the allocation of memory, either Static or Dynamic.
4. Direct access & Sequential access: The ability of directly referring to an item without having access to any other is called Direct access. Sequential access is searching through a number of items before finding a specific item i.e. an item, which is not directly accessible. For example, arrays provide a direct access to any element within the array by means of an index (direct access). But in linked lists, we must traverse through the list to locate a specific node (sequential access).
Some of the data structures, which will be discussed in detail later in the book, are given below.
Arrays
This is a simplest type of linear data structure (one dimensional array), by means of which a set of finite number say n of similar data elements can be referenced by a set of n consecutive numbers 1, 2, 3, …….. n. For example, consider the following list of elements:
5, 6, 8, 4, 3, 1, 2, 56, 67, 78
This list of 10 elements can be stored in an array of size 10 as follows:
Note: Arrays will not be discussed hereafter in this book. It will be presumed that the reader is familiar with arrays.
Stacks
Stack is a linear data structure of dynamic height, all components of which are of same data type (Homogenous components). A stack is also called a Last-In-First-Out (LIFO) system, in which insertion and deletion of any component can take place only at the top of the stack, where ‘top’ is an end of the stack. That is, at any given time, the only item, which can be accessed to, is on the top of the stack (sequential access). A common example of a stack is a dish or a coin stacker. Dishes are pushed
onto the top and popped
off the top.
Queues
Queue is also a linear data structure of dynamic height, which contains single type of data. Thus it is a homogenous data structure, all components of which are of same data type. A queue is, also, called a First-In-First-Out (FIFO) system, in which insertion of a component can take place only at one end, called rear
end of the queue and deletion of a component can take place only at other end, called front
end of the queue (Sequential access). The word queue
is thus like the queue at a counter for service, in which customers are dealt with in the order in which they arrive.
Linked Lists
Linked list is again a linear data structure of dynamic length. All components within the list are of same data type stored in nodes. Thus it is also a homogenous data structure. To locate a specific node in the list, we must sequentially traverse through the list (Sequential access). In a linked list, each item is allocated a space in memory as it is added to the list. A link is kept between each item with the next item in the list. Each node of the list has following two elements:
a) The item being stored in the list and
b) A pointer to the next item in the list
The last node in the list contains a NULL pointer to indicate that it is the end or tail of the list. As items are added to a list, memory for a node is dynamically allocated. Thus the number of items that may be added to a list is limited only by the amount of memory available.
Note: Above shown example of linked list is a Linear Linked List. There are two more variations of linked lists: Doubly Linked List and Circular Linked List. These lists will be discussed later in the book.
Trees
A tree is a non-empty finite set of vertices & edges, which satisfy a certain criterion. A vertex is a simple entity (node) that can have a name and contains some other associated information. An edge is a connection between two vertices.
In a tree, there is one specially designated vertex called root and the remaining vertices are partitioned into a collection of sub-sets, each of which is also a tree. The nodes emerging from a particular node are called children of that node and the node itself is named as the parent of all those nodes. A node may or may not have children. The line from parent to a child is called an edge.
It is a non-linear data structure of dynamic height, in which each node contains single type of data (Homogenous Components). To locate a specific node in the tree, we have to traverse through a number of nodes in the tree (Sequential access).
Graphs
A graph is, also, a non-empty finite set (V, E) of vertices & edges, where V shows the set of vertices and E is a set of edges connecting the vertices. Each element e in set E is associated with an unordered pair (Vi, Vj), where Vi and Vj are elements of set V, which are connected through ‘e’ (edge). A tree is a graph, but a graph may not necessarily be a tree.
EXAMPLE:
Data Structure Operations
The data visible in the data structures is processed by means of some specified operations. These operations are called data structure operations. The following operations play a major role in the processing of data:
Traversing: To visit each and every node of data structure exactly once is called traversing.
Insertion: To insert (add) a new record into the data structure.
Deletion: To delete (remove) a record from the data structure.
Searching: To find the location of a specific record in the data structure.
The following two operations are, also, a part of data structure operations, but these two are used in some special situations.
Sorting: Sorting means the arranging of the records of a data structure in some logical manner. (e.g. to arrange a list of alphabetical numbers in a dictionary order or arranging a list of numerical numbers in an increasing or decreasing manner).
Merging: Merging means the combining of the records of two sorted files into a third sorted file.
STACKS
A Stack is a linear list of elements in which an element may be inserted or deleted only at one end, called the top of the stack. This means that the element, which has been lastly inserted onto the stack, will be the first to come out. In view of this a stack is also called a Last-In-First-Out(LIFO) system, in which insertions and deletions can take place only at one end, called the top.
Special terminology is used for two basic operations associated with the stacks:
Push
is the term used to insert an element onto a stack.
Pop
is the term used to delete an element from a stack.
The following figure illustrates a stack, which can accommodate a maximum of 10 elements:
Sometimes it is required to access the top element of the stack without removing it from the stack, so a special term ‘Peek’ is used for performing this operation.
~~ Program Begins ~~
/* Implementing a stack using an array */
#include
#define MAX 100
typedef enum Boolean { False, True }Boolean;
typedef struct Stack
{
int Top; /* Variable 'Top' is used to denote the top position of stack */
int S[MAX]; /* Array of integer elements */
}Stack;
void Push(Stack *, int), Display(Stack *);
int Pop(Stack *), Peek(Stack *);
Boolean IsEmpty(Stack *), IsFull(Stack *);
void main()
{
int x, Item;
Stack St;
St.Top = -1; /* Initializing top */
while(1) /* Infinite loop */
{
printf(Enter choice \n
);
printf(1. Push\n
);
printf(2. Pop\n
);
printf(3. Peek\n
);
printf(4. Display\n
);
printf(5. Exit\n
);
scanf(%d
, &x);
switch(x)
{
case 1:
if(IsFull(&St))
{
printf(Overflow\n
);
break;
}
printf(Input the element\n
);
scanf(%d
, &Item);
Push(&St, Item);
break;
/* Invoke Push function and break the switch statement */
case 2:
if(IsEmpty(&St))
{
printf(Underflow\n
);
break;
}
Item=Pop(&St);
printf(Popped element: %d\n
, Item);
break;
/* Invoke Pop function and break the switch statement */
case 3:
if(IsEmpty(&St))
{
printf(Stack is empty\n
);
break;
}
Item=Peek(&St);
printf(Top element: %d\n
, Item);
break;
/* Invoke Peep function and break the switch statement */
case 4:
if(IsEmpty(&St))
{
printf(Stack is empty\n
);
break;
}
Display(&St);
break; /* Display the Stack */
case 5:
exit(); /* Exit the program */
} /* end switch */
} /* end while */
} /* end main */
Boolean IsFull(Stack *St)
{
if(St->Top==MAX-1)
return True; /* Stack cannot store more than 'MAX' elements */
else
return False;
} /* end IsFull */
Boolean IsEmpty(Stack *St)
{
if(St->Top == -1)
return True; /* No element in Stack */
else
return False;
} /* end IsFull */
void Push(Stack *St, int Item)
{
St->S[++St->Top]=Item; /* Push the element in array 's' */
} /* end Push */
int Pop(Stack *St)
{
int Item;
Item=St->S[St->Top--]; /* Pop the top element of array 's' */
return Item;
} /* end Pop */
int Peek(Stack * St)
{
int Item;
Item=St->S[St->Top]; /* Assign top element of the stack to Item */
return Item;
}
void Display(Stack *St)
{
int I;
for(I=St->Top;I>=0;I--)
printf( %d\n
, St->S[I]);
} /* end Display */
/*
After executing:
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
1
Input the element
1
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
1
Input the element
2
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
1
Input the element
3
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
3
Top element: 3
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
4
3
2
1
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
2
Popped element: 3
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
2
Popped element: 2
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
2
Popped element: 1
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
2
Underflow
Enter choice
1. Push
2. Pop
3. Peek
4. Display
5. Exit
5……………………………………(Terminated)
*/
~~ Program Ends ~~
Steps of the program:
In this program, a structure ‘Stack’ has been declared, which contains a variable ‘Top’, used to denote the top position of the stack, and an array ‘S’ of integer elements to capture the elements of the stack.
typedef struct Stack
{
int Top;
int S[MAX];
}Stack;
In addition to this, an enumerated data type ‘Boolean’ has been declared, which can take value False or True.
typedef enum Boolean { False, True }Boolean;
When an enumerated data type is declared, its members are automatically assigned integer values 0, 1, 2, 3… in succession, beginning with 0 for the first member.
So internally members ‘False’ and ‘True’ of enumerated data type ‘Boolean’ are assigned values 0 and 1 respectively.
void main()
This function invokes different functions, which are used to perform desired operations entered by user.
1) Here variable ‘St’ of type Stack has been declared, which contains a variable ‘Top’ and an array ‘S’ of integer elements.
Here ‘Top’ will point to the topmost element in array ‘S’.
2) Option 1 is used to push an element onto the stack. But before pushing an element, it is to be verified that stack should not be full i.e. it must have some space to grab an element. So ‘IsFull’ function is invoked, which returns either True or False, depending upon whether stack is full or not.
3) Option 2 is used to pop an element from the stack. But before popping an element, it is necessary to verify that stack should not be empty i.e. it should have some element, which