Dsa Notes
Dsa Notes
-
Algorithm to copy one
string in to
another string
Algorithm to concatenate
two strings.
for compare two string
for compare two string
Algorithm to find
whether a second
string is a substring of
first string
Algorithm to reverse the
string.
UNIT-3.STACKS AND
QUEUES
Stack data
structure:
Algorithms for Stack
Operation
PUSH(S,TOP,VAL)
(2) Algorithm for POP
Operation.
POP{S,TOP)
Example
Applications of Stack :
Recursion:
• Recursion means function call
itself.
• It is the technique of defining a
process in terms of itself.
• Stack is widely used in recursion
because of its Last in First out
Property.
• In terms of C language the function
calling itself is known as recursi•on.
• There are two types of recursion:
•Primitive Recursive
function: Example Factorial
Function
• Non primitive
Recursive function:
Example Ackerman's
function {not in your
syllabus).
Applications of Stack :
Recursion:
Applications of Stack :
Rules for converting infix
notation to
Examples polish
conversion
Examples reverse polish
conversion
A lications of Stack :
Queue
Queue
QDELETE (Q, Front, Rear)
Example
Circular Q.ueue
For example consider the following
operations:
CQINSERT (Q, Front, Rear, N, Val)
CQDELETE (Q, FRONT, REAR,N)
Com arison of LIFO
and FIFO
Unit 4
Pointers in C
A pointer is a variable whose
value is the address of another
variable, i.e., direct address of the
memory location.
Structure in c
Structure in
c
Structure using
pointers
Dynamic Memory
Allocation in C
PROGRAM
Linked list
Linked list
Linked list
Traversing a Linked
List
Count nodes from
Linked List
Searching for a Value in
a Linked List
Inserting a New Node in
a Linked List
Inserting a Node at the
Beginning of a Linked
List
Inserting a Node at the
Beginning of a Linked
List
Inserting a Node at the
Beginning of a Linked
List
Inserting a Node at the
End of a
Inserting a Node at the
End of a
Linked List
Inserting a Node at the
End of a Linked List
Inserting a Node After a
Given Node in a Linked
List
Inserting a Node After a
Given Node in a Linked
List
Inserting a Node After a
Given Node in a Linked
List
Node in a Linked List
Suppose we want to add a new node
with value 9 before the node containing
3.
Inserting a Node Before a
Given Node in a Linked
List
Inserting a Node Before a
Given Node in a Linked
List
Deleting a Node from a
Linked List
Case 1: The first node is
deleted.
Case 2: The last node is
deleted.
Case 3: The node after a given
node is deleted.
Deleting the First Node
from a
Deleting the First Node
from a Linked List
DELETEFI RST(START)
Deleting the Last Node
from a
Linked List
Deleting the Last Node
from a
Deleting the Last Node
from a
Linked List
Deleting a Node After
a Given Node in a
Linked List
Suppose we want to delete the
node that succeeds the node
which contains data value 3.
Deleting a Node After a
Given Node in a Linked
List
Deleting a Node After
a Given Node in a
Linked List
CIRCULAR LINKED
LISTS
DOUBLY LINKED
LISTS
DOUBLY LINKED
LISTS
DOUBLY
LINKED LISTS
Inserting a Node at the
Beginning of a Doubly
Linked List
INSERTBEG_DQ(START)
Inserting a Node at the
End end of a Doubly
Linked List
Inserting a Node at the
End end of a Doubly
Linked List
Move PTR so that it points to the last
node of the list. Add the new node after
the node pointed by PTR.
Step 1: IF AVAIL== NULL
Inserting a Node After a
Given Node in a
Doubly Linked List
Suppose we want to add a new
node with value 9 after the node
containing 3.
INSERTAFTERNUM_DLL(START)
Inserting a Node
before a Given Node
in a Doubly Linked
List
Supposewe want to add a new
node with value 9 before the node
containing 3.
Inserting a Node before a
Given Node in a Doubly
Linked List
INSERTB4NUM_DLL(START)
Deleting a Node from a
Doubly Linked List
Case 1: The first node is
deleted.
Case 2: The last node is deleted.
Case 3: The node after a given
node is deleted.
Case 4: The node before a given
node is deleted.
Deleting the First Node
from a Doubly Linked
List
DELETEFIRST _DLL(START)
Deleting the Last Node
from a
Doubly Linked List
Deleting the Last
Node from a
Doubly Linked List
Move PTR so that it now points to the
last node of the Ii st.
DELETELAST_DLL(STARn
APPLICATIONS OF
LINKED LISTS
Polynomial
Representation
Difference between
Linked list and
Unit 5
TREES
Root node : The root node R is the
topmost node in the tree. If R =
NULL, then it means the tree is empty.
Sub-trees : If the root node R is not
NULL, then the trees Tl, T2, and T3
are called the sub-trees of R.
Descendant node : A descendant node
is any successor node on any path from
the node to a leaf node. Leaf nodes do
not have any descendants. In the tree
given in Fig. 6.1, nodes C, G, J, and Kare
the descendants of node A.
TYPES OF TREES
General Trees
Forests
Binary Trees
Root node
level higher than its parents. So all
child nodes are defined to have level
number as parent's level number + 1.
Leaf node : A node that has no children
is called a leaf node or a terminal node.
The leaf nodes in the tree are: 8, 9, 5, 10,
11, and 12.
Similar binary trees : Two binary trees
T and T1 are said to
In-degree/out-degree of a node It is the
number of edges arriving at a node. The
root node is the only node that has an in-
degree equal to zero. Similarly, out-
degree of a node is the number of edges
To make a file more beautiful, you can:
MERGE SORT
•A smaller list takes fewer steps and
thus less time to sort than a large list.
EXAMPLE
Step 1: [INITIALIZE] SET I= BEG, J = MID+ 1,
INDEX= 0
ELSE
QUICK SORT
Technique
EXAMPLE
RADIX SORT
Algorithm for RadixSort (ARR, N)
Step 1: Find the largest number in
ARR as LARGE
EXAMPLE
345,654,924,123,567,472,5
55,808,911
Hashing and Collision
In a small company of 100 employees,
each employee is assigned an
Emp_lD in the range 0-99. To store
the records in an array, each
employee's Emp_lD acts as an index
into the array where the employee's
record will be stored as shown in Fig.
we need a way to convert a five-digit
key number to a two-digit array index.
We need a function which will do the
transformation. In this case, we will use
the term hash table for an array and the
function that will carry out the
transformation will be called a hash
function.
HASH FUNCTIONS
Solution We will use A = 0.618033, m =
1000, and k =
To make a file more
beautiful, you can follow
these suggestions:
5. Use whitespace
effectively: Utilize
whitespace to create
breathing room between
sections and paragraphs. This
helps improve clarity and
readability.
By implementing these
suggestions, you can make
your file more visually
appealing and engaging for
the reader.
12345
Step 1: Square the value of the key.
That is, find kA2. Step 2: Extract the
middle r digits of the result
obtained in Step 1.
Calculate the hash value for keys
1234 and 5642 using the mid-square
method. The hash table has 100
memory locations.
Folding Method
Given a hash table of 100 locations,
calculate the hash value using folding
method for keys 5678, 321, and 34567.
COLLISIONS
Consider a hash table of size
10. Using linear probing,
insert the keys 72, 27, 36,
24, 63, 81, 92, and 101
into
the table.
Step 2 Key = 27
h(27, 0) = (27 mod 10 + 0) mod 10
Step 4 Key = 24
h(24, 0) = (24 mod 10 + 0) mod 10
h(l0l, 0) = {101 mod 10 + 0) mod 10