DS Notes For 2IA
DS Notes For 2IA
LINKED LIST
Linked List is a linear data structure, in which elements are not stored at a contiguous location,
rather they are linked using pointers. Linked List forms a series of connected nodes, where each
node stores the data and the address of the next node.
A linked list is a linear data structure that stores a collection of data elements dynamically.
Nodes represent those data elements, and links or pointers connect each node.
Each node consists of two fields, the information stored in a linked list and a pointer that
stores the address of its next node.
The last node contains null in its second field because it will point to no node.
A linked list can grow and shrink its size, as per the requirement.
It does not waste memory space.
struct node
{
int data:
struct node*next;
};
node
data *next
Self Referential structures are those structures that have one or more pointers which point to
the same type of structure, as their member.
struct node
{
int data:
struct node*next;
};
node
data *next
struct node
{
int data;
struct node*next;
};
void insertfront()
{
temp=(struct node *) malloc(sizeof(struct node));
printf("\n Enter the data");
scanf("%d", &temp->data);
temp->next =NULL;
if(first==NULL)
first=temp;
else
{
temp->next=first;
first=temp;
}
}
void insertend()
{
temp=(struct node *) malloc(sizeof(struct node));
printf("\n Enter the data");
scanf("%d", &temp->data);
temp->next =NULL;
if(first==NULL)
first=temp;
else
{
q=first;
while(q->next!=NULL)
q=q->next;
q->next=temp;
}
}
void deletefront()
{
if(first==NULL)
printf("Thelistisempty. \n");
else
{
q=first;
first=first->next;
printf("\n Element Deleted is%d",q->data);
free(q);
}
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]
void deleteend()
{
if(first==NULL)
printf("Thelistisempty. \n");
else
{
q=first;
while(q->next->next!=NULL)
{
p=q;
q=q->next;
}
p->next=NULL;
printf("\n Element Deleted is %d",q->data);
free(q);
}
}
void display()
{
if(first==NULL)
printf("\n Listisempty.\n");
else
for(q=first; q!=NULL;q= q->next)
printf("%d ->", q->data);
}
void main()
{
int choice;
while(1)
{
printf("-----SINGLYLINKEDLIST MENU \n");
printf("1:INSERT AT TFRONT\n");
printf("2:INSERT AT ENDDISPLAY\n");
printf("3:DELETE FROM FRONT\n");
printf("4:DELETE FROM END\n");
printf("5.DISPLAY\n");
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]
printf("6:EXIT\n");
switch(choice)
{
case 1: insertfront();
break;
case 2: insertend();
break;
case 3:deletefront();
break;
case 4:deleteend(); ;
break;
case 5:display();
break;
case 6: exit(0);
default:printf("InvalidChoice\n");
}// switch end
}//while end
}
First
10 20 30 NULL
void create()
{
for(i=10; i<=30;i=i+10) //create nodes with 10,20,30
{
temp=(struct node *) malloc(sizeof(struct node));
temp->data =i;
temp->next =NULL;
if(first==NULL)
first=temp;
else
{
temp->next=first;
first=temp;
}
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
if(first==NULL)
first=temp;
else
{
temp->next=first;
first=temp;
}
}
void display()
{
if(first==NULL)
printf("\n Listisempty.\n");
else
for(q=first; q!=NULL;q= q->next)
printf("%d ->", q->data);
}
void main()
{
int choice;
while(1)
{
printf("-----LINKED STACK \n");
printf("1:PUSH\n");
printf("2:POP\n");
printf("3.DISPLAY\n");
printf("4:EXIT\n");
switch(choice)
{
case 1: insertfront();
break;
case 2: deletefront();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("InvalidChoice\n");
}// switch end
}//while end
}
void concatenate()
{
struct node *first 1;
struct node *first 2;
if(first1!=NULL) && (first2!=NULL)
first1->next=first2;
struct node
{
int data:
struct node*next;
struct node * prev;
};
node
data
*prev *next
#include<stdio.h>
#include<stdlib.h>
struct node
{
Int data;
struct node *next;
struct node *prev;
};
void nodecreate()
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data");
scanf("%d", &temp->data);
temp->next=NULL;
temp->prev=NULL;
}
void insertfront()
{
nodecreate();
if(first==NULL)
first=temp;
else
{
temp->next=first;
first->prev=temp;
first=temp;
}
}
void insertend()
{
nodecreate();
if(first==NULL)
first=temp;
else
{
q=first;
while(q->next!=NULL)
q=q->next;
temp->prev=q;
q->next=temp;
}
}
void deletefront()
{
if(first==NULL)
printf("The DLL list is empty. \n");
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]
else
{
q=first;
printf("Deleted element is %d \n\n",q->data);
first=first->next;
first->prev=NULL;
free(q);
}
}
void deleteend()
{
if(first==NULL)
printf("Thelistisempty. \n");
else
{
q=first;
while(q->next!=NULL)
{
p=q;
q=q->next;
}
p->next=NULL;
printf("\n Student deleted is %d",q->data);
free(q);
}
}
void display()
{
int count=0;
if(first==NULL)
printf(" DLL List is empty. \n");
else
{
for(q=first;q!=NULL;q=q->next)
{
printf("%d \t <=======>\n",q->data);
count++;
}
}
printf("\nTotal Number of nodes DLL List are: %d\n",count);
}
void main( )
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]
{
int choice,n,i;
while(1)
{
printf("\n-----DOUBLY LINKED LIST MENU \n");
printf("1:CREATE DLL\n");
printf("2:DISPLAY DLL\n");
printf("DOUBLE ENDED QUEUE DEMOSTRATION \n");
printf("3:INSERT AT END OF DLL\n");
printf("4:DELETE FROM END OF DLL\n");
printf("5:INSERT AT FRONT OF DLL\n");
printf("6:DELETE FROM FRONT OF DLL\n");
printf("7:EXIT\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter number of Employees:\n ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Employee %d details\n",i);
insertend();
}
break;
case 2: display();
break;
case 3: insertend();
break;
case 4: deleteend();
break;
case 5: insertfront();
break;
case 6: deletefront();
break;
case 7: exit(0);
default:printf("Invalid Choice\n");
}
}
}
void conacatenate()
{
struct node *first 1;
struct node *first 2;
struct node
{
int coef;
int exp;
struct node * next;
};
This data structure is a specialized method to organize and store data in the computer to be
used more effectively. It consists of a central node, structural nodes, and sub-nodes, which are
connected via edges. We can also say that tree data structure has roots, branches, and leaves
connected with one another.
Root-
The first node from where the tree originates is called as a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data structure.
Example-
2,Edge
The connecting link between any two nodes is called as an edge.
In a tree with n number of nodes, there are exactly (n-1) number of edges.
Example-
3. Parent-
The node which has a branch from it to any other node is called as a parent node.
In other words, the node which has one or more children is called as a parent node.
In a tree, a parent node can have any number of child nodes.
Example-
Here,
Node A is the parent of nodes B and C
Node B is the parent of nodes D, E and F
Node C is the parent of nodes G and H
Node E is the parent of nodes I and J
Node G is the parent of node K
4. Child-
Example-
Here,
Nodes B and C are the children of node A
Nodes D, E and F are the children of node B
Nodes G and H are the children of node C
Nodes I and J are the children of node E
Node K is the child of node G
5. Siblings-
Example-
1.
2.
6. Degree-
Example-
Here,
Degree of node A = 2
Degree of node B = 3
Degree of node C = 2
Degree of node D = 0
Degree of node E = 2
Degree of node F = 0
Degree of node G = 1
Degree of node H = 0
Degree of node I = 0
Degree of node J = 0
Degree of node K = 0
7. Internal Node-
The node which has at least one child is called as an internal node.
Internal nodes are also called as non-terminal nodes.
Every non-leaf node is an internal node.
Example-
8. Leaf Node-
The node which does not have any child is called as a leaf node.
Leaf nodes are also called as external nodes or terminal nodes.
Example-
9. Level-
Example-
10. Height-
Total number of edges that lies on the longest path from any leaf node to a particular node is
called as height of that node.
Height of a tree is the height of root node.
Height of all leaf nodes = 0
Example-
Here,
Height of node A = 3
Height of node B = 2
Height of node C = 2
Height of node D = 0
Height of node E = 1
Height of node F = 0
Height of node G = 1
Height of node H = 0
Height of node I = 0
Height of node J = 0
Height of node K = 0
11. Depth-
Total number of edges from root node to a particular node is called as depth of that node.
Depth of a tree is the total number of edges from root node to a leaf node in the longest
path.
Depth of the root node = 0
The terms “level” and “depth” are used interchangeably.
Example-
Here,
Depth of node A = 0
Depth of node B = 1
Depth of node C = 1
Depth of node D = 2
Depth of node E = 2
Depth of node F = 2
Depth of node G = 2
Depth of node H = 2
Depth of node I = 3
Depth of node J = 3
Depth of node K = 3
12. Subtree-
Example-
13. Forest-
Example-
File System: This allows for efficient navigation and organization of files.
Data Compression: Huffman coding is a popular technique for data compression that
involves constructing a binary tree where the leaves represent characters and their
frequency of occurrence. The resulting tree is used to encode the data in a way that
minimizes the amount of storage required.
Compiler Design: In compiler design, a syntax tree is used to represent the structure of a
program.
are used in database indexing to efficiently search for and retrieve data.
A Binary Tree Data Structure is a hierarchical data structure in which each node has at most two
children, referred to as the left child and the right child. It is commonly used in computer science
for efficient storage and retrieval of data, with various operations such as insertion, deletion and
traversal.
A Binary Search Tree is a data structure used in computer science for organizing and storing
data in a sorted manner. Each node in a Binary Search Tree has at most two children,
a left child and a right child, with the left child containing values less than the parent node
and the right child containing values greater than the parent node. This hierarchical structure
allows for efficient searching, insertion, and deletion operations on the data stored in the
tree.
Tree Traversal
Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and
postorder traversal of the Binary Search
Inorder Traversal:
Follow the below steps to implement the idea:
Traverse left subtree
Visit the root and print the data.
Traverse the right subtree
Preorder Traversal:
Postorder Traversal:
Follow the below steps to implement the idea:
Traverse left subtree
Traverse the right subtree
Visit the root and print the data.
Example:
Output:
InorderTraversal: 8 12 20 22 25 30 40
PreorderTraversal: 22 12 8 20 30 25 40
Postorde Traversal: 8 20 12 25 40 30 22
Given the Inorder and Preorder Traversal of a binary tree, we need to construct the unique
binary tree represented by them.
Example:
Here 10 (first element of preorder) is the root element. So we can find its index in the inorder
traversal(say elem). The left subtree of the root will be present to the left side of in order
whereas the right subtree of root will be present on the right side of elem in the inorder
traversal:
We can define a recursive function that creates one node at a time. First, we create the root
node, and then we can take the help of recursion to create its left and right subtrees. In order to
make recursion work, we need to provide the correct inorder and preorder traversal of the
subtree for every recursive call.
Given Inorder and PostOrder Traversal of a binary tree, we need to construct the unique binary
tree represented by them.
Example:
Here 10 (last element of postorder) is the root element. So we can find its index in the inorder
traversal(say elem). The left subtree of the root will be present to the left side of inorder
whereas the right subtree of root will be present on the right side of elem in the inorder
traversal:
We can define a recursive function that creates one node at a time. First, we create the root
node, and then we can take the help of recursion to create its left and right subtrees. In order to
make recursion work, we need to provide the correct inorder and postorder traversal of the
subtree for every recursive call.
Expression Tree
he expression tree is a binary tree in which each internal node corresponds to the operator
and each leaf node corresponds to the operand so for example expression tree for 3 +
((5+9)*2) would be:
Prefix Expression; + 3 * + 5 9 2
Postorder Expression: 3 5 9 + 2 * +
nfix expression:
(a+(b*c))+(d*(e + f))
Postfix Expression:
abc*+def+*+
Prefix Expression:
++a*bc*d+ef
A binary tree data structure is represented using two methods. Those methods are as follows...
1. Array Representation
number on each node we can easily use the tree for array representation of binary tree and the
array will look like this:
return root;
}
Graph Data Structure is a collection of nodes connected by edges. It’s used to represent
relationships between different entities. Graph algorithms are methods used to manipulate
and analyze graphs, solving various problems like finding the shortest path or detecting
cycles.
Consider the following directed graph G (in which the vertices are ordered as v 1, v2, v3, v4, and
v5), and its equivalent adjacency matrix representation on the right:
v1 v2 v3 v4 v5
v1 0 1 0 1 1
v2 0 0 0 1 0
v3 0 0 0 0 1
v4 0 0 0 0 0
v5 0 1 0 0 0
2. Adjacency List
An adjacency list represents a graph as an array of linked lists.
The index of the array represents a vertex and each element in its linked list represents the
other vertices that form an edge with the vertex.
The adjacency list for the graph we made in the first example is as follows:
Breadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used
for traversing or searching graphs and trees. This article covers the basic difference between
Breadth-First Search and Depth-First Searc
Approach It works on the concept of FIFO It works on the concept of LIFO (Last In
used (First In First Out). First Out).
Hashing refers to the process of generating a fixed-size output from an input of variable size
using the mathematical formulas known as hash functions. This technique determines an index
or location for the storage of an item in a data structure.
Hash Table is a data structure which stores data in an associative manner. In a hash table, data
is stored in an array format, where each data value has its own unique index value. Access of
data becomes very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash
technique to generate an index where an element is to be inserted or is to be located from.
Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an array.
We're going to use modulo operator to get a range of key values. Consider an example of hash
table of size 20, and the following items are to be stored. Item are in the (key,value) format.
(1,20)
(2,70)
(42,80)
(4,25)
(12,44)
(14,32)
(17,11)
(13,78)
(37,98)
Linear Probing
As we can see, it may happen that the hashing technique is used to create an already used index
of the array. In such a case, we can search the next empty location in the array by looking into
the next cell until we find an empty cell. This technique is called linear probing.
Hash Collision
Two or more keys can generate same hash values sometimes. This is called a collision. A
collision can be handled using various techniques. Linear probing is a scheme in computer
programming for resolving collisions in hash tables, data structures for maintaining a collection
of key–value pairs and looking up the value associated with a given key.
PESITM , Dept of CSE , Shivamogga Prepared By, Mrs.Prathibha S
Data Structures and Applications [BCS304]
et us consider a simple hash function as “key mod 7” and a sequence of keys as 50, 700, 76, 85,
92, 73, 101,
which means hash(key)= key% S, here S=size of the table =7,indexed from 0 to 6.We can define
the hash function as per our choice if we want to create a hash table,although it is fixed
internally with a pre-defined formula.