Data Structures Notes
Data Structures Notes
The primitive data structures are primitive data types. The int, char, float,
double, and pointer are the primitive data structures that can hold a single
value.
The focus of these data structures is on forming a set of data elements that is
either homogeneous (same data type) or heterogeneous (different data types).
Static data structure: It is a type of data structure where the size is allocated at
the compile time. Therefore, the maximum size is fixed.
Dynamic data structure: It is a type of data structure where the size is allocated at
the run time. Therefore, the maximum size is flexible.
Reusability: The data structure provides reusability means that multiple client
programs can use the data structure.
Abstraction: The data structure specified by an ADT also provides the level of
abstraction. The client cannot see the internal working of the data structure, so it
does not have to worry about the implementation part. The client can only see the
interface.
What is an Algorithm?
Algorithm Analysis
Asymptotic Notations
The commonly used asymptotic notations used for calculating the running time
complexity of an algorithm is given below:
Itis the formal way to express the upper boundary of an algorithm running time.
It measures the worst case of time complexity or the algorithm's longest amount
of time to complete its operation. It is represented as shown below:
If f(n) and g(n) are the two functions defined for positive integers,
then f(n) = O(g(n)) as f(n) is big oh of g(n) or f(n) is on the order of g(n)) if there exists
constants c and no such that:
f(n)<=c.g(n)
2*1+3<=5*1
5<=5
2*2+3<=5*2
7<=10
We know that for any value of n, it will satisfy the above condition, i.e.,
2n+3<=c.n. If the value of c is equal to 5, then it will satisfy the condition
2n+3<=c.n. We can take any value of n starting from 1, it will always satisfy.
Therefore, we can say that for some constants c and for some constants n0, it will
always satisfy 2n+3<=c.n. As it is satisfying the above condition, so f(n) is big oh
of g(n) or we can say that f(n) grows linearly. Therefore, it concludes that c.g(n) is
the upper bound of the f(n). It can be represented graphically as:
If f(n) and g(n) are the two functions defined for positive integers,
then f(n) = Ω (g(n)) as f(n) is Omega of g(n) or f(n) is on the order of g(n)) if
there exists constants c and no such that:
Is f(n)= Ω (g(n))?
f(n)>=c.g(n)
To check the above condition, we first replace f(n) by 2n+3 and g(n) by n.
2n+3>=c*n
Suppose c=1
Big theta is mainly used when the value of worst-case and the best-case is same.
It is the formal way to express both the upper bound and lower bound of an
algorithm running time.
Let f(n) and g(n) be the functions of n where n is the steps required to execute
the program then:
f(n)= θg(n)
c1.g(n)<=f(n)<=c2.g(n)
where the function is bounded by two limits, i.e., upper and lower limit, and
f(n) comes in between. The condition f(n)= θg(n) will be true if and only if
c1.g(n) is less than or equal to f(n) and c2.g(n) is greater than or equal to f(n).
The graphical representation of theta notation is given below:
c1.g(n)<=f(n)<=c2.g(n)
c1.n <=2n+3<=c2.n
If n=2
1*2<=2*2+3<=2*2
Keyword in Context (KWIC) Indexing system is based on the principle that the
title of the document represents its contents. It is believed that the title of the
document is one line abstract of the document. The significant words in the title
indicate the subject of the document. a KWIC index makes an entry under each
significant word in the title, along with the remaining part of the title to keep the
context intact. The entries are derived using terms one by one as the lead term
along with the entire context for each entry.
Structure
ii) Context: The rest of the terms of the title provided along with the
keywords specifies the context fo the document.
iii) Identification or Location Code: A code (usually the social number of
the entry) which provides address of the document where its full
bibliographical details will be available.
In order to indicate the end of the title a “/” symbol is used. The
identification code is put on the extreme right to indicate the location of the
document.
Step II: After the selection of keywords, the computer moves the title laterally in
such a way that a significant word (keyword) for a particular entry always appears
either on the extreme left-hand side or in the center. The same thing can be
performed manually following the structure of KWIC to generate entries.
Step III: After all the index entries for a document are generated, each entry is filed
at its appropriate place in the alphabetical sequence.
In C programming, they are the derived data types that can store the primitive type
of data such as int, char, double, float, etc. For example, if we want to store the
marks of a student in 6 subjects, then we don't need to define a different variable
for the marks in different subjects.
Representation of an array
As per the above illustration, there are some of the following important points
As stated above, all the data elements of an array are stored at contiguous locations
in the main memory. The name of the array represents the base address or the
address of the first element in the main memory. Each element of the array is
represented by proper indexing.
n (n - based indexing): The first element of the array can reside at any random
index number.
2D Array
The syntax of declaring two dimensional array is very much similar to that of a one
dimensional array, given as follows.
1. int arr[max_rows][max_columns];
PUSH operation
1 If TOP = Max-1
3 If the stack is not full, increments top to point next empty space.
5 Returns success
Before deleting the element from the stack, we check whether the stack
is empty.
If we try to delete the element from the empty stack, then
the underflow condition occurs.
If the stack is not empty, we first access the element which is pointed by
the top
Once the pop operation is performed, the top is decremented by 1,
i.e., top=top-1.
3 If the stack is not empty, accesses the data element at which top is pointing.
5 Returns success.
Peek()
The Peek operation is employed when it is necessary to return the value of the
topmost stack element without erasing it. This operation first determines whether
the Stack is empty, i.e., TOP = NULL; if it is, then the value will be returned;
otherwise, an appropriate notice will be displayed.
Algorithm
1.START
3. END
isFull()
isFull() operation checks whether the stack is full. This operation is used to check
the status of the stack with the help of top pointer.
isEmpty()
The isEmpty() operation verifies whether the stack is empty. This operation is used
to check the status of the stack with the help of top pointer.
each program has an opening and closing braces; when the opening
braces come, we push the braces in a stack, and when the closing braces
appear, we pop the opening braces from the stack. Therefore, the net
value comes out to be zero. If any symbol is left in the stack, it means
that some syntax occurs in a program.
2.Recursion
The recursion means that the function is calling itself again. To maintain
the previous states, the compiler creates a system stack in which all the
previous records of the function are maintained .
3.Memory management
4.Function call
In the Stack, return addresses are displayed in the order that the
functions were called, as can be seen in the image above. Each function
is finished, followed by the pop operation, which starts execution at the
position where the Stack was removed.
Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are used in-
between operands. It is easy for us humans to read, write, and speak in infix
notation but the same does not go well with computing devices. An algorithm to
process infix notation could be difficult and costly in terms of time and space
consumption.
Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead of
operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix
notation is also known as Polish Notation.
Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style, the
operator is postfixed to the operands i.e., the operator is written after the operands.
For example, ab+. This is equivalent to its infix notation a + b.
Infix to prefix
Q Q
+ + Q
T + QT
* +* QT
V +* QTV
/ +*/ QTV
U +*/ QTVU
/ +*// QTVU
W +*// QTVUW
* +*//* QTVUW
) +*//*) QTVUW
P +*//*) QTVUWP
^ +*//*)^ QTVUWP
O +*//*)^ QTVUWPO
( +*//* QTVUWPO^
+ ++ QTVUWPO^*//*
N ++ QTVUWPO^*//*N
* ++* QTVUWPO^*//*N
M ++* QTVUWPO^*//*NM
- ++- QTVUWPO^*//*NM*
L ++- QTVUWPO^*//*NM*L
K ++-+ QTVUWPO^*//*NM*LK
QTVUWPO^*//*NM*LK+-++
K K
+ +
L + KL
- - K L+
M - K L+ M
* -* K L+ M
N -* KL+MN
+ + K L + M N*
K L + M N* -
( +( K L + M N *-
O +( KL+MN*-O
^ +(^ K L + M N* - O
P +(^ K L + M N* - O P
) + K L + M N* - O P ^
* +* K L + M N* - O P ^
/ +/ K L + M N* - O P ^ W *
U +/ K L + M N* - O P ^W*U
/ +/ K L + M N* - O P ^W*U/
V +/ KL + MN*-OP^W*U/V
* +* KL+MN*-OP^W*U/V/
T +* KL+MN*-OP^W*U/V/T
+ + KL+MN*-OP^W*U/V/T*
KL+MN*-OP^W*U/V/T*+
Q + KL+MN*-OP^W*U/V/T*Q
KL+MN*-OP^W*U/V/T*+Q+