Data Structures Notes
Data Structures Notes
Classification of Algorithm
Sequential construct: in sequential construct program statements are execution of
statements one after another in a sequence.
Ex: 1. Input statement: input a, b, c
2. Assignment: c <- a + b
3. Output statement: output a, b, c
Iteration construct: if some of the statements have to be executed repeatedly we can use
repetitive constructs to perform much iteration. There are two types of looping
constructs:
1
DATA STRUCTURE
Example:
Algorithm: Area of the circle
step 1: read radius
step 2: [compute the area]
Area=3.14*radius*radius
step 3: [print the area]
Print “Area of a circle = area”
step 4: [end of algorithm]
Stop
Characteristics of an Algorithm
Finiteness: each step in algorithm terminates after a finite number of steps.
Definiteness: each step in algorithm is unambiguous. This means that the action
specified by the step cannot be interpreted in multiple ways and can be performed
without any confusion.
Input: an algorithm accepts zero or more inputs.
Output: it produces at least on output
Effectiveness: it consists of basic instructions that are realizable. This means that the
instructions can be performed by using the given inputs in a finite amount of time.
2
DATA STRUCTURE
Efficiency: algorithm efficiency is a measure of the average execution time necessary for
an algorithm to complete work on a set of data.
3
DATA STRUCTURE
Algorithmic notations
Name of the algorithm
Step number
Explanatory comment
Termination
Efficiency of an Algorithm
Algorithm efficiency is measured by the complexity function ‘T(n)’ and ‘n’ the size of the
algorithm’s input.
Time complexity: Time complexity of an algorithm is the amount of computer time needed to
complete the execution. “Time” can mean the number of memory accessing is performed, the
number of comparisons between integers, the numbers of times the inner loop is executed, or
some other natural unit related to the amount of computer of real time the algorithm will take.
Space complexity: Space complexity is function describing the amount of memory (space) an
algorithm takes in terms of the amount of input to the algorithm.
Worst case: It gives the maximum value of T(n) for any possible input. The worst case occurs
when element is found at last position / not found at any position.
Tworst(n) = n
Best case: It gives the minimum of T(n) for any possible input. The best case occurs when the
element to be searched is found at first location.
Tbest(n) = 1
Average case: it gives the expected value of T(n). The following assumptions are made to find
the average case.
The probility of successful search is P(where 0<=P<=1)
The probility of first match occurs in ith position of the array is the same of every i
4
DATA STRUCTURE
In case of successful search, the probility of first match occurs in i th position of the array
is P/n for every i
In case of successful search, the number of comparisons is ‘n’ with probility of such a
search being (1-P).
Taverage(n) = (p(n+1)/2)+(n(1-p))
Where P=1, search successful and Taverage(n) = (n+1)/2
P=0, search successful and Taverage(n) = n
Asymptotic notation
The Asymptotic efficiency of algorithms are concerned with how the running program
increases with the size of the input ‘m’ the input limit as the size of the input increases without
bound.
Big-Oh notation (O): it is upper bound of function. The function f(n) will be considered for
worst case that it does not consume more than this computing time.
The names of common Big-Oh expression are,
EXPRESSION NAME
O(1) Constant
O(log n) Logarithmic
O(log2n) Log squared
O(n) Linear
O(n log n) N log n
O(n2) Quadratic
O(n3) Cubic
n
O(2 ) Exponential
Omega notation (Ω): it is used to find the lower bound behavior of f(n). The lower bound
implies that below the given time the algorithm cannot perform better. The function f(n) will be
considered for average case.
5
DATA STRUCTURE
Theta notation (Ɵ): it can be used when function f(n) is bounded both from above and below,
where upper and lower bound are same. In finding maximum and minimum element in an array,
the computing time is O(n) and Ω(n). There exists a notation Ɵ. The function f(n) will be
considered for best case.
6
DATA STRUCTURE
7
DATA STRUCTURE
8
DATA STRUCTURE
Robustness: program developed using the specific data structure should produce correct
outputs for all inputs when executed on all hardware platforms.
Adaptability: modern software should also be able to adapt the data structure designed.
Re-usability: software developed using data structure can be reusable in future software
applications thus reducing cost and time of software development.
9
DATA STRUCTURE
Integer: it is a simple type data which when created is used to store integer values. When
this type is used we can store only numeric value at a time. But values can be varied at
any point of time.
a) Sign and magnitude method
b) Radix complement representation
c) Diminished complement representation Storage structure
d) Pure BCD representation
Note: Storage Structure is the representation of a particular structure in the memory of a
computer. It is also known as memory representation.
Real/Floating-point numbers: it is also a simple type data like integer, but here
fractional or real values are stored.
a) Fixed decimal point representation
b) Floating point representation
Character: it is a non-numerical data type which can store only one character at a time.
a) ASCII(American Standard Code for information Interchange)
b) EBCDIC(Extended Binary Coded Decimal Interchange Code)
Logical data/ Boolean number: it is a data type which can store only two possible
values such as true/false, 0/1, high/low.
Pointer data/Links: it is a reference to a data structure, also a data type which stores the
address of the other data element.
10
DATA STRUCTURE
Selection operation: it is used to access data within a data structure. For complex
structures method of access is one of the important properties of a structure. In case of
files the access can be sequential or random depending on the nature of files.
Ex: scanf(“%d”, &a);
Updation operation: this operation is used to change or modify data value of the
element in the structure. An Assignment operator is a good example of an update
operation.
Ex: y = 5; //modifies the value of y to store the new value 5 in it.
11
DATA STRUCTURE
12
DATA STRUCTURE
Traversing: it is the process of visiting each element in the data structure exactly to
perform certain operation on it.
Sorting: it is the process of arranging the elements of a particular data structure in some
logical order. The order may be either ascending or alphabetic order depending on the
data item present.
Merging: it is the process of combing the elements in two different structures into a
single structure.
Searching: it is the process of finding the location of the element with given key value in
a particular data structure or finding the location of an element, which satisfies the given
condition.
Insertion: it is the process of adding a new element to the structure. Most of the times
this operation is performed by identifying the position where the new element is to be
inserted
Deletion: it is the process of removing an item from the structure
Where as in the case of Non-Linear data structure, a node be connected to more than one
successor and one predecessor as shown below:
For node 2, we have one predecessor and two successors i.e., node4 and node5.
The Linear Data Structure exhibits adjacency relationship where as non - linear data
structure exhibits hierarchical/parent- child relationship.
13
DATA STRUCTURE
CHAPTER 3: ARRAYS
Definition of Arrays
An array is a type of linear data structure consisting of finite number of
similar/homogeneous objects. Here we can derive linear relationship between the
elements because elements are arranged one after the other.
It is an ordered list of homogeneous data elements.
It is defined as a set a set of homogeneous data items, in which variable holds multiple
values of the same data types.
Classification of Arrays
One-Dimensional / Single / Linear Array
Multi-Dimensional Array
o Two-Dimensional Array
o Three-Dimensional Array
o n-Dimensional Array
Operations on an Array
Traversal: processing each element in an array exactly once.
Search: find the location of element with a given value in an array
Insertion: inserting an element into an array (Note: size of the array does not change).
Deletion: deleting an element from an array (Note: size of the array does not change).
Sorting: arranging the elements in some type or particular order (ascending / descending)
Merging: combine one or more arrays to form a single array.
14
DATA STRUCTURE
1 2 3 4
arr
15
DATA STRUCTURE
16
DATA STRUCTURE
Processing of an array
Single operation involving entire array are not permissible in C. But, it allows the programmer to
perform certain operations (comparison, assignment) on an element – by – element basis.
Ex: Let num[4], here UB=3 and LB=0, hence the LENGTH = 3 – 0 + 1 = 4. Here index set is an
integer. The elements of array are num[0], num[1], num[2], num[3].
Ex: Consider an array A of size 10. Suppose this is stored at the storage address 200 with two
words per memory. Find the address of A[4].
17
DATA STRUCTURE
18
DATA STRUCTURE
Insertion
An element can be inserted into the array provided the memory space allocated for the array is
large enough to accommodate the additional element. Inserting an element at the end of the array
does not require any movement of data. On the other hand, if an element is inserted in the middle
of the array, then on the average half of the element must be moved one location downward to
new locations to accommodate the new element and keep the order of the other elements.
Ex: Consider an array of 6 elements. To add an element 35 into location 4, all elements from
A[4] have to be moved downwards. For this consider an array A which can store maximum 10
elements. Let the length of the array be 6. i.e., N=6. i.e., out of 10 memory locations we are using
only 8 of it as shown below:
19
DATA STRUCTURE
Deletion
As in insertion, deleting an element at the end of an array is easy but deleting an element
somewhere in the middle of an array will require subsequent elements to be moved one location
upwards as shown below:
Ex: Consider an array of 7 elements. To remove an element 19 from location 3, all elements
from A[4] have to be moved upwards. For this consider an array A which can store maximum 10
elements. Let the length of the array be 7. i.e., N=7. i.e., out of 10 memory locations we are using
only 7 of it as shown below:
20
DATA STRUCTURE
Multi-dimensional array
Multi-dimensional array uses more than one subscript to refer a particular element then it is
called multi-dimensional array.
Ex: A[i,j] - two dimensional array
A[i,j,k] - three dimensional array multi-dimensional array
21
DATA STRUCTURE
22
DATA STRUCTURE
For ex: Consider an array ‘a’ of size 3*3, the matrix representation of array elements are given
by,
The fact that computer does not keeps track of the address of all the elements of a[i,j] instead it
keeps track of base address of a[i,j] i.e., base(a). Thus the address of the first element a[1,1] of
‘a’ computes the address of loc(a[i,j]) of a[i,j] using the following formula,
Row major order: Loc(a[i,j]) = base(a)+w[n(i-1)+(j-1)]
Where base(a) is base address of the array, ‘w’ is the number of words per memory location, ‘n’
is the total number of rows, ‘i’ no. of rows and ‘j’ no. of columns.
23
DATA STRUCTURE
For ex: Consider an array ‘a’ of size 3*3, the matrix representation of array elements are given
by,
The formula to compute the address of memory location of an array of size m*n is given by,
Column major order: Loc(a[i,j]) = base(a) + w [(i-1) + m(j-1)]
Where base(a) is base address of the array, w is the number of words per memory location, m is
the total number of rows, ‘i’ no. of rows and ‘j’ no. of columns.
Characteristics of an array
Zero-based indexing: We refer first element a[0], the second element a[1] and so forth. Its
more natural we start with index a[1], but starting the indexing with0 has some advantage
and has emerged as the convention used in most modern programming languages.
24
DATA STRUCTURE
Array length: Once we create an array, its size is fixed. Code for referring the length of an
array a.length
Bounds checking: when programming with arrays, it our responsibility to use legal indices
when accessing an array element.
Setting array values at compile time: when we small number of literal values, which can be
initialized by listing it between curly braces, separated by a comma.
Ex: char suits[ ] = {“clubs”, “diamonds”, “hearts”, “spades”}
Features
Array size should be positive number only
String array terminates with null character (\0)
Array elements are counted from 0 to n-1
Useful for multiple reading of elements.
25
DATA STRUCTURE
26
DATA STRUCTURE
Applications of an array
Used in matrix manipulation (addition, subtraction and multiplication).
Used to represent records, stacks and queues in memory.
Used in the representation of polynomials.
27
DATA STRUCTURE
CHAPTER 4: STACKS
Definition of Stacks
Stack is a linear list (ordered collection of elements) in which insertion and deletion done at
one end, called the top of the stack.
Data is stored and retrieved in the Last-In-First-Out order (LIFO).
As mentioned earlier, elements are added into or removed from end, indicated by Top of the
stack.
PUSH is the term used to insert an element into a stack.
POP is the term used to delete an element from a stack.
Stack is restricted variant of the list in which elements are inserted and deleted from only one
end. While this make stack both efficient and easy to implement.
28
DATA STRUCTURE
Let ‘top’ be variable which points to the top element of the stack
If top=0, then there is no element in the stack, which is called ‘stack underflow’ i.e., no more
elements can be poped from the stack.
If top=n, then the stack is full, which is called as ‘stack overflow’ i.e., no more elements can
be pushed into the stack.
Operations on Stacks
Stack performs the following functions and operations:
PUSH: inserts an element to the top of stack. It takes an integer element as argument. If the
stack is full then error is returned.
POP: removes top element from the stack. If the stack is empty then error is returned. The
element is deleted from the top of the stack.
DISPLAY: traverse each element and displays stack contents.
Search: this function takes an integer element as an argument and returns the location on the
element. If number is not found then 0 is returned.
REPLACE: this function takes two integers as arguments, first is to find and second is to
replace. It first performs search operation then replaces the integers.
29
DATA STRUCTURE
Here on this case attempt to insert one more item ‘fff’ to the array stack will result in
OVERFLOW, because TOP has got the value 4 and the maximum SIZE of the stack which is
constructed using array is also 4
30
DATA STRUCTURE
Here in this case attempt to delete item from the TOP of stack will result in UNDERFLOW
condition as there are no elements in the stack i.e., top = 0
31
DATA STRUCTURE
32
DATA STRUCTURE
The advantage of the linked list implementation of stack is that all stacks being used by the
program can share the same available list. Then in the stack needs a node, it can be obtained it
from the single available list. Then any stack no longer needed a node, it return the node to that
available list. As long as the total amount of space needed by the entire stack at any one time is
less than the amount space initially available to them all, is stack is able to grow a shrink to any
size. No spaces have been preallocated to the single stack and no stack is using space that it does
not need.
33
DATA STRUCTURE
Recursion
Calling a function itself again and again until some specified condition is satisfied is called
recursive function. This method / programming technique is called recursion.
Function that calls itself directly or indirectly again and again. Such function is called
recursive functions and the process is termed as recursion. When a function calls itself, it is
making a recursive call.
Syntax:
data_type function_name(a)
{
base case; //recursive function terminating condition
else
function_name(x); //recursive part
}
Features
There should be at least one if statement used to terminate recursion.
It should not contain any looping statement.
34
DATA STRUCTURE
return 1
else
return n*fact(n-1)
end if
end
35
DATA STRUCTURE
Advantages of recursion
It is easy to use and implement.
Used to represent compact programming structures.
Used in the divide and conquer method of problem solving
Used for postponing the decisions.
On machine with hardware stack instructions, in fact, the non-recursive function may
actually required more running time than equivalent recursive function.
36
DATA STRUCTURE
If solution to the problem is defined in terms of itself. In such situation one can make use of
recursive version.
Disadvantage of recursion
It is slower than that of looping statement because each time function is called.
Types of expression
An expression can be in 3 forms
Infix expression
Prefix expression
Postfix expression
NOTE: The process of writing the operators of an expression either before their operands or
after them is called ‘notation’.
PRECEDENCE OPERATOR
1. (, ), ^
2. *, /, %
3. +,-
37
DATA STRUCTURE
Infix notation
Operators are written in between their operands.
The operations(order of evaluation) are performed from left to right and obeys precedence
rules
Brackets can be used to change the order of evaluation
Ex: 1) A+B 2) X*(Y+Z)
Prefix notation
Operators are written before their operands.
Order of evaluation from right to left.
Brackets cannot be used to change the order of evaluation.
Postfix notation
Operators are written after their operands
The order of evaluation of operators is always from left to right.
Brackets cannot be used to change the order of evaluation.
This notation is also known as Suffix Notation.
Ex: 1) AB+ 2) XYZ+*
38
DATA STRUCTURE
SL NO INFIX POSTFIX
1 (A + B) * C = [AB + ] * C = AB + C *
2 A+B-C = [AB + ] - C = AB + C -
3 (A+B) / (X-Y) = [AB + ] / [XY - ] = AB + XY - /
4 A^B*C–D = [AB ^ ] *C – D = AB ^ C * ] - D = AB ^ C * D -
((A + B) * C - (D - E)) ^ (X + Y) = ([AB + ] * C - [DE - ]) ^ [XY + ])
= AB + C * DE -
5 = ([AB + C * ] - [DE - ]) ^ [XY + ]
XY + ^
= ([AB + C * DE - - ] ^ [XY + ])
Note: The process of writing the operators of an expression after their operands is called
‘Reverse Polish Notation’.
39
DATA STRUCTURE
Example: 5 3 7 * + 2 -
SCANNED SYMBOL STACK VALUE
40
DATA STRUCTURE
#
5 #5
3 #53
7 #537
* # 5 21 3 * 7 = 21
+ # 26 5 + 21 = 26
2 # 26 2
- # 26 26 – 2 = 24
# 24
Applications of stacks
Used to implement recursion function (factorial, fibonacci, GCD of a given number).
Expression conversion (infix to postfix) and evaluation(postfix expression)
Reversing a string (to check given number is palindrome or not)
Stack usage in four function calculator. Most the calculator today accept standard format of
an infix notation (operand-operator-operand). In contrast mainly calculators still today made
today using postfix notation (operand-operand-operator).
Used to indicate the order of processing data when certain steps of the processing must be
postponed until some other conditions are fulfilled.
Direct applications
o page visited history in a web browser
o undo sequence in a text editor
41
DATA STRUCTURE
CHAPTER 5: QUEUE
Definition of Queue
Linear (ordinary) queue is a special type of data structure where elements are inserted
from one end and deleted from the other end.
Queue defines as sequence-oriented object container where element are access and
deletion is restricted to the first element in the sequence, which is called front of the
queue and the insertion is restricted to the end of the sequence, which is called the rear of
the queue.
The end from where elements are inserted is called rear end.
The end from where elements are deleted is called front end.
Since the first element inserted is the first item to be deleted from queue, so it is also
called First In First Out (FIFO) data structure.
42
DATA STRUCTURE
The above figure shows the way the array Q will be stored in memory with elements. It also
shows how the elements are inserted or deleted from the queue.
Operations on queue
Insertion: Inserts an element to the rear end of the queue. If the queue is full then error is
returned.
Deletion: Removes the element from the front end of the queue. If the queue is empty then
error is returned.
Traversal: Display the contents of queue.
43
DATA STRUCTURE
44
DATA STRUCTURE
exit
end if
Step 2: [print the content of queue]
for i = f to r do
write “q[i]”
end for
Step 3: end
45
DATA STRUCTURE
46
DATA STRUCTURE
47
DATA STRUCTURE
Circular queue
Queue of having logic terminate when the limit of the array used to store the queue is
reached, both REAR and FRONT loop back to the array. In this way any number of elements
could be placed on the queue, so long elements were also taken off. This implementation of a
queue is called circular queue.
It is essence, the queue is full when the REAR is one less than FRONT; otherwise there is a
room in the queue for another event.
In a circular queue, the elements can be stored efficiently in an array so as to “wrap around”
so the end of the queue is followed by front of the queue.
It representation allows the entire array to store the elements without shifting data within
queue.
48
DATA STRUCTURE
read “item”
end if
Step 3: if(*front= = -1)
[Insert item as first element of the queue]
*front=0
*rear=0
else if(*rear==MAX-1)
[Reset the rear pointer]
*rear=0
else
[Increment rear pointer]
*rear=*rear+1
end if
Step 4: Q[*rear]=item
Step 5: end
49
DATA STRUCTURE
*rear=-1
else if(*front==MAX-1)
[Reset the front pointer]
*front=0
else
[Increment front pointer]
*front=*front+1
*count=*count-1
end if
step 4: end
50
DATA STRUCTURE
Step 3: end
Advantages of circular queue over ordinary queue
In circular queue we utilize memory efficiently
Rear insertion is denied in ordinary queue even if free memory is available; we cannot access
the memory locations. This is the major disadvantage of ordinary queue.
In circular queue, the elements of a given queue can be stored efficiently in an array so as to
“wrap around” so that end of queue is followed by the front queue.
Circular representation allows the entire array to store the elements without shifting any data
within the queue.
Fig: Deque
51
DATA STRUCTURE
52
DATA STRUCTURE
53
DATA STRUCTURE
end if
Step 4: end
Priority queue
A queue in which we are able to insert items or remove items from any position depending
on some property is called priority queue.
If an element of higher priority is processed before any element of lower priority.
If two elements with same priority are processed according to the order in which they are
added to the queue (FIFO).
The difference between priority queue and other queues is that the strict ordering on the
queue is not purely FIFO. Instead ordering is a combination of FIFO and the priority
assigned to the elements.
Ex: Refer fig1 that represents a priority queue of jobs of supervisors(S), teachers (T) and
students (ST) respectively. Therefore, if a job is initialed with priority P, it is inserted
immediately at the end of the queue of other jobs with priority P, P = 1, 2, 3. Here the jobs are
always removed from the front of the queue.
54
DATA STRUCTURE
queen then insertion may mean that the new element must be placed in the middle of the queue.
This can require the movement of several elements. Thus, we can say that it is better to split a
priority queen into several queues, each having its own storage structure.
Applications of queue
Queues are used in various operations. Some of them are:
Queues are useful in time sharing systems where many users’ jobs will be waiting in the
system queue for processing. These jobs may request the service of the cpu, main memory or
external device such as printer etc. all these jobs will be given a fixed time for processing and
are allowed to use one after the other. This is the case of an ordinary queen where priority is
the same for all the jobs and whichever job is submitted first, that job will be processed.
Perhaps the most common use of circular queues is in operating system where the circular
queue holds the information read from and returns to disk files or the console.
In a computer network, message from one computer to another are generally created
synchronously.
Priority queues are used in designing CPU schedulers where jobs are dispatched to the CPU
based on priority of the job.
Operating systems often use a priority queue for the ready queue of processes to run on the
CPU. Important programs, like those that the user is interacting with, receive a high priority;
lower priority is accorded to less urgent tasks like checking periodically for new e-mail or
rearranging files on disk for more efficient access. As a result, the operating system works on
the background tasks only when the user is not interacting with the computer.
55
DATA STRUCTURE
UNIT 2
CHAPTER 6: POINTERS
Pointers
Pointer is a special data type which is derived from basic data types (int, float, char,
double,.). So pointer is called derived data type.
Definition: pointer variable is a variable used to hold the information of the other variable.
The pointer takes the values from 0 to 65535, if the size of the RAM is 64K.
Any variable, which is declared and initialized, has three things associated with it
Y, a memory location which hold the value of the variable.
The initialized value, which is stored in the location
The address of that memory location.
Pointer variable
A variable which holds address of the variable is called a pointer variable.
Declare a data variable
Steps to be followed while using pointer Declare a pointer variable
Initialize a pointer variable
Access data using pointer variable
56
DATA STRUCTURE
p = &i; // Assignment
printf(“%d”, *p); //500 (value of the data variable)
printf(“%p”,p); //1055 (address of the data variable)
}
Pointer declaration
A pointer is a variable that contains the address of the memory location of another
variable. To create a pointer variable we use the syntax in the figure
Pointer operator
A pointer operator is used to classify a variable as a pointer and not as a normal variable
For ex: int *prt;
Address operator
Once pointer variable is declared, it must be pointed to something. It can be achieved by
assigning to the pointer the address of the variable which is needed.
ptr = #
57
DATA STRUCTURE
Pointer constants
Computer store information in memory, where it is divided into number of locations called
storage cells.
All 65536 locations arranged sequentially, but physically divided into even bank (address)
and odd bank (address). These addresses are called pointer constants.
Pointer values
Memory is divided into number of storage cells called locations. 0 to 65536 addresses are
sequentially arranged. Out of these memory addresses assigned to variables by the system are
called pointer values.
Dangling pointer
int *p;
This indicates p is a pointer variable and corresponding memory location contain address of
an integer variable, but declaration will not initialize the memory location and memory
contains garbage value.
A pointer variable should contain a valid address, which does not contain a valid address is
called dangling pointer.
NULL pointer
int *p = NULL;
A NULL pointer is defined as a special pointer value that points to nowhere in the memory. If it
is early to assign value to the pointer then it is better to assign NULL (i.e., \0 or 0) to the pointer.
58
DATA STRUCTURE
Garbage Collection
Computers do not have an infinite amount of storage and cannot manufacture more storage for
immediate utilization. Therefore there are finite numbers of nodes available and it is impossible
to use more than that number at any given instant. If it desired more memory over a period of
time, some nodes must be reused. Suppose memory space becomes reusable because a node is
deleted from a list or an entire list is deleted. One way to bring this free space, immediately
reinsert the space in free-storage list. But this is time consuming. So operating system
periodically collects all deleted space onto the free-storage list, this technique of collection is
called garbage collection.
Garbage collection usually takes place in two steps. First the computer run through all lists,
tagging those cells which are currently in use, and second then the computer runs through the
memory, collecting all untagged space onto the free-storage list. Garbage collection takes place
only when there is minimum amount of space or no space at all left in free storage list or when
CPU is idle (free) and has time to do the collection. However garbage collection is invisible to
the programmer. The free( ) function returns a node to the free pool, I.e., availability list. This
function makes a node that is no longer being used in the linked list available for reuse.
Note: When a node is deleted from a list or an entire list is deleted then that memory cells can be
reused. Thus the process of collecting all reusable cells into the free storage list is called
garbage collection.
Operations on pointers
A pointer variable which holds address of the other variable. This address can be incremented or
decremented. The pointer variables cannot be multiplied and divided because those operations
are performed on the addresses.
Assignment operator
int *ptr, var;
ptr = &var;
59
DATA STRUCTURE
Logical operator
Ex: int *r, *s, *t;
then,
(*r > *s && *s > *t && *r > *t)
printf(“%d is greatest\n”, *r);
(*r < *s || *r < *t || *s < *t)
printf(“%d is smallest\n”, *t); etc, can be performed using decision making statements and
relational operators.
60
DATA STRUCTURE
Call-by-value
When a function is invoked a correspondence is established between actual and formal
parameter, where a temporary storage is created were the value of the actual parameter is stored.
The formal parameter picks up its value from this storage area. This mechanism of data transfer,
between actual and formal parameter which allows the actual parameter to be an expression,
function arrays and etc. such parameter is value parameter and mechanism of data transfer is
referred to as call-by-value.
Call-by-reference
Whenever the function call is made if we pass the address of the variable to function, the
parameters receive the address to the pointers. The process of calling function using pointers to
pass the address of variable is called call-by-reference
61
DATA STRUCTURE
Pointers to Pointers
It makes a pointer to point to another pointer variable. The variable which contains
address of a pointer variable is called pointer to a pointer.
62
DATA STRUCTURE
int x;
float y;
}struct complex *p; //to declare p as a pointer of type struct complex
void main( )
{
*p.x = 8; //to access the first member of the struct
p -> y = 10.5; //another way to access the first member of the struct
printf(“value of X = %d\n”, (*p).x);
printf(“Value of Y = %f\n”, p -> y);
getch();
}
Advantages of pointers
Memory allocation to a variable More than one value can be returned using pointer
concept(pass by reference)
Very compact code can be written using pointers.
Data accessing is much faster when compared to arrays.
Using pointers, we can access byte or word locations and the cpu registers directly. The
pointers in c are mainly useful in processing of non-primitive data structure such as arrays,
linked lists etc.
Disadvantages
Un-initialized pointers or pointers containing invalid address can came system crash.
It is very easy to use pointers incorrectly, causing bugs that are very difficult to identify and
correct.
They are confusing and difficult to understand in the beginning and if they are misused the
result is not predictable.
63
DATA STRUCTURE
Malloc ( )
It allocates the specified number of bytes.
Syntax: (type_cast) malloc (no. of element * size of each element);
Example: int *ptr;
ptr = (int*) malloc (10 * sizeof(int));
64
DATA STRUCTURE
Calloc ( )
It allocates the specified number of bytes and initailizes them to zero.
Syntax: (type_cast) calloc ( no. of blocks, size of each block);
Example: int ptr = (int*) calloc (10, sizeof(int));
Realloc ( )
It increases / decreases the size of the specified block of memory.
Syntax: (type_cast) realloc (pointer, new size);
Example: int ptr = (int*) realloc (number, count * sizeof(int));
Free ( )
It releases the specified block of memory back to the system.
Syntax: free (pointer_variable);
Example: free(ptr);
65
DATA STRUCTURE
Calloc
The syntax of calloc is: ptr = (data_type*) calloc(n, size); takes two arguments number of
blocks to be allocated size is number of bytes to be allocated for each block.
Allocates multiple blocks of memory, each with the same size.
Each byte of allocated space is initialized to zero
Calloc() is slightly more computationally expensive because of zero filling but, occasionally,
more convenient than malloc()
This function can allocate the required number of blocks contiguously. If required memory
can not be allocated contiguously, it returns null.
Allocation and initialization of allocated memory with 0’s can be done using the following
statement: p = calloc(sizeof(int)*n);
66
DATA STRUCTURE
Linked List
Linked list is a type of data structure for storing information as a list, which consisting of a
group of nodes together represents a sequence.
A linked list is a list with each data item containing a link to the location of the next data item
in the list.
A linked list represents a linear collection of items, called nodes. Nodes of a linked list can
be scattered about in the memory. They need not necessarily represent a set of consecutive
memory locations. Each node in a list has two parts: information part and link part.
Node: It is a structure of a linked list consists of two fields INFO and LINK as member of the
structure.
Info: It is an information field in a node that accommodates the actual data element value.
These data elements can be int, float, char, string or even another structure.
Link: It is next address field containing a pointer value to locate next node in sequence.
Null list: A list with empty node, without any information of next node (Null, /0)
Empty node: It a node with requisite amount of memory location to it, where info field
contains a 0, link field contains arbitrary pointer value.
67
DATA STRUCTURE
Header node: It is preferred to keep an additional blank node at the front portion of the list
which never contains data element in its info field.
68
DATA STRUCTURE
A linked list in its simplest form is a collection of nodes that together form a linear sequence.
The ordering determined as “Follow the leader”, in each node usually consists of a structure
that includes information fields and a link pointer.
The above figure shows a list of numbers, which is represented, in the form of a linked list.
HEAD is a pointer, which gives the address of the first node in the linked list. Link field of the
last node contains the NULL, which indicates it does not contain any address.
We have seen that a linked list is a collection of nodes of the same type and hence it can be
defined as follows:
The structure representation of singly linked list is,
struct node
{
int data; //information field
struct node*link; //a pointer which points to next node
};
typedef struct node *NODE; //structure to create a node in SLL
NODE *HEAD;
69
DATA STRUCTURE
70
DATA STRUCTURE
71
DATA STRUCTURE
Memory allocation
The maintenance of the linked list in memory assumes the possibility of inserting new node into
the lists and hence requires some mechanism, which provides memory space for the new nodes.
This is achieved by the memory space of deleted nodes becomes available for future use.
Together with linked list in memory, a special list is maintained which consists of unused
memory cells. This list, which has its own pointer, is called the list of available space
(AVAILABILITY LIST) or free-storage list or free pool. The AVAIL pointer points to the first
node in the availability list.
Whenever a node is to be inserted into a linked list, it is necessary to have a function (GetNode)
that supplies an unused node from the availability list. If there is a free node, then the address of
the available free node in which the new data can be placed is also to be determined. The
following will allocate a free node and makes it available to the program.
72
DATA STRUCTURE
73
DATA STRUCTURE
HEAD newnode
Step 8: end
74
DATA STRUCTURE
75
DATA STRUCTURE
76
DATA STRUCTURE
77
DATA STRUCTURE
78
DATA STRUCTURE
79
DATA STRUCTURE
80
DATA STRUCTURE
81
DATA STRUCTURE
82
DATA STRUCTURE
83
DATA STRUCTURE
84
DATA STRUCTURE
85
DATA STRUCTURE
86
DATA STRUCTURE
87
DATA STRUCTURE
The data field contains the value, the llink field has the address of the previous node in the
list and rlink field has the address of the next node in the list.
The llink field of the first node and rlink field of the last node is NULL, indicating the end of
the list for each direction.
The address of the first node is stored in the special pointer called HEAD and the address of
the second node is stored in the special pointer called TAIL.
88
DATA STRUCTURE
89
DATA STRUCTURE
90
DATA STRUCTURE
91
DATA STRUCTURE
CHAPTER 8: TREES
Trees
Tree is a non-linear data structure. It is an abstract data type that stores elements
hierarchically. It is a collection of nodes. With the exception of the top element, each element
in a tree has a parent element and zero or more children elements.
The top most node/element is called as root and the other nodes are called sub trees / child
node.
92
DATA STRUCTURE
Properties of tree
There is precisely one root node.
All nodes except the root have precisely one parent.
There are no cycles. That is, starting at any given node, there is no path that can take back to
the starting node.
The first two properties – that there exists one root and that all nodes save the root have one
parent – guarantee that no cycles exist.
Binary tree
Binary tree is a tree which is collection of zero or more nodes and finite set of directed lines
called branches that connect the nodes.
A binary tree is an ordered tree in which each internal node can have a maximum of two
child nodes connected to it. In a binary tree, the first child of an internal node is called the
left child, and the second child is called the right child. The sub-tree rooted to at the left and
right of a child is called the left sub-tree and the right sub-tree.
The number of branches associated with each node is called degree of a node.
93
DATA STRUCTURE
o Left subtree: it is a tree connected to the left of root. Since it comes under root, it is
called left subtree.
o Right subtree: it is a tree connected to the right of root. Since it comes under root, it is
called right subtree.
Various Terminologies
Root node: The first node in the tree and with the indegree zero is called root node. It does not
have any parent node.
94
DATA STRUCTURE
Child node: The node, which can be reachable from a node x, using only one edge are called
children of node x and node x is the parent for all those children.
Parent node: A node that has at most one child is called parent node of that child. A node
having left or right or both subtree is said to be parent node.
Ancestors: The nodes in the path from root to the specified node x.
Descendents: The nodes in the path below the parent, the node that are all reachable from
node x are all called descendent.
Left Descendents: The node that lie towards left subtree of node x.
Right Descendents: The node that lie towards right subtree of node x.
Subtree: A node having at most one child node and all the nodes that are descendents of a
node x is called subtrees.
Left Subtree: A node having at most one left child and all the nodes that are left descendents
of a node x is called left subtrees.
Right Subtree: A node having at most one right child and all the nodes that are right
descendents of a node x is called right subtrees.
Leaf: a node in a tree that has an outdegree of zero. A node with an empty left and right child is
called leaf node.
95
DATA STRUCTURE
Internal nodes: the nodes expect leaf nodes in a tree are called internal nodes. A node is a
internal node if it has one more children.
External nodes: the leaf nodes in a tree are called external or terminal node. A node is a
terminal node if it has no children.
Level: the distance of a node from the root is called level of the node. In a tree, the root has a
level 0 and other node is one more than the parent.
Height/depth: the height of the tree is defined as the maximum level of any leaf in the tree or
maximum number of nodes in a branch of tree. It also called as depth.
Degree of a tree: The maximum height of the tree or the maximum degree of nodes in the
tree.
96
DATA STRUCTURE
97
DATA STRUCTURE
98
DATA STRUCTURE
99
DATA STRUCTURE
Note: Memory can be allocated or de-allocated using malloc( ) and free( ) function.
Method1: In representation some of the locations may be used and some may not be used. To
indicate memory location are used or not flag field namely, link is used. If link=0, the
corresponding location is not used and indicates the absence of the node at that position.
So each node contains two fields:
100
DATA STRUCTURE
A structure declaration
#define max 20
struct node
{
int info;
int link;
};
typedef struct node NODE;
An array a of type NODE can be used to store different items and declared as shown below:
NODE a[max];
Method 2: Instead of using separate flag field link to check the presence of a node, one can
initialize each location in the array to 0 indicating the node is not used. Non-zero value in
location indicates the presence of the node.
Creating a tree
It is process of creating a tree which consist of root, subtrees and child nodes
101
DATA STRUCTURE
102
DATA STRUCTURE
103
DATA STRUCTURE
prev -> rlink = temp; //attach the node to the right of the parent
return root;
}
Traversals
Traversing is a method of visiting each node of a tree exactly once in a systematic order based on
the order. During traversing, info field of each node is visited and printed.
104
DATA STRUCTURE
Inorder Traversal
The inorder traversal of a binary tree can be
recursively defined as follows:
1. Traverse the Left subtree in inorder [L]
2. Process the root Node [N]
3. Traverse the Right subtree in inorder [R]
Postorder Traversal
The postorder traversal of a binary tree can
be recursively defined as follows:
1. Traverse the Left subtree in postorder
[L]
2. Traverse the Right subtree in postorder
[R]
3. Process the root Node [N]
105
DATA STRUCTURE
Searching
By traversing the tree in any of the order one can visit each node. As we visit the node we can
compare the info field of each node with the key to be searched. If found, display successful
search, otherwise display unsuccessful search.
106
DATA STRUCTURE
printf(“Search unsuccessful\n”);
}
break;
}
107
DATA STRUCTURE
Copying a tree
Here address of the root node is given and after copying, it returns address of the root node of the
new tree.
108
DATA STRUCTURE
Insertion
It is process of inserting an item into tree.
109
DATA STRUCTURE
Deletion
It is a process of deleting a node from a given tree.
110
DATA STRUCTURE
while(cur!=null)
{
if( item=cur->info)
break;
parent =cur;
cur= (item<cur->info) ? cur->llink : cur->rlink;
}
if(cur==NULL)
{
printf(“item not found\n”);
return root;
}
111
DATA STRUCTURE
Application of trees
It is used to represent hierarchical relationship.
It is used in the construction of symbol table.
112
DATA STRUCTURE
CHAPTER 9: SEARCHING
Searching is an operation refers to finding a particular element and its location in a given list
of elements. There are some different searching techniques, which are fast and efficient but
some are slow in finding the desired element.
The techniques involve searching large amounts of data to find a particular piece of
information. Certain methods of organizing data make the search process more efficient.
LINEAR SEARCH: This method is simple and straight forward. It is also called as
‘Sequential search’. It is applicable when the array elements which are unsorted.
Logic:
The ‘search key’ is compared with first element, if both the values are equal, the search
process stop.
Otherwise same procedure is continued with next element, until the end of array.
Search is successful is the element is found, else unsuccessful.
113
DATA STRUCTURE
Step 6: for i 1 to n do
if a[i] item then
loc i
flag 1
end if
end for
Step 7: if flag 1 then
write “Search is successful, item is found in the location”
else
write “Search is unsuccessful, element not found”
Step 8: end
114
DATA STRUCTURE
Binary search: This method can increase the efficiency of the search operation
Logic
First find the middle element of the array
Compare the mid element with an item, where item is the search element.
115
DATA STRUCTURE
116
DATA STRUCTURE
117
DATA STRUCTURE
Sorting of the data elements involved rearranging, in movement of data from one place to
another within the given array list which reduces the cost of reorganization operation.
Selection Sort
This method is based on comparing and exchanging the top most element with the least
value, until all the elements in an array is sorted in particular order.
118
DATA STRUCTURE
a[j] a[i]
a[i] temp
end if
end for (j)
end for (i)
Step 4: for i = 1 to n do
write “a[i]”
Step 5: end
119
DATA STRUCTURE
Bubble Sort
Bubble sort is the simplest and easiest sorting technique. In this technique, the two successive
elements are swapped.
Bubble sort differs from selection sort, in that, instead of finding the smallest record value
and then perform an interchange. The two values are interchanged immediately after
discovering that the elements are out of order.
120
DATA STRUCTURE
Merge Sort
The technique is as follows
Divide the sequence of elements into two parts.
Recursively sort the elements on left part of the division.
Recursively sort the elements on right part of the division.
The process of merging of two sorted left and right parts of the array into a single sorted
array is called simple merge.
To solve the problem of merge sort technique is that both arrays should be sorted either in
ascending or descending order.
121
DATA STRUCTURE
end for
Step 4: merge_sort(a,0,n-1) // calling function merge_sort
Step 5: write “Array after sorting”
for i 0 to n do
write “a[i]”
end for
Step 6: end
122
DATA STRUCTURE
123
DATA STRUCTURE
The best case occurs, when the array is divided into two exactly equal parts is O(n log n).
The average case is O(n log n)
he worst case is O(n2).
Quick Sort
Quick sorting technique works well on large set of data.
The first step
Is to partition the given table into two sub-tables
The elements towards left of the key element are less than the key element and elements
towards right of the key element are greater than key element
After this step, the array is partitioned into two sub tables.
36 37 11 10 42 72 65 98 88 78
< 42 > 42
124
DATA STRUCTURE
125
DATA STRUCTURE
end if
Step 3: end
126
DATA STRUCTURE
Insertion Sort
Insertion sort was invented in 1959 by D. L. Shell and hence named as shell sort.
This technique is similar to bubble sort, instead of comparing the adjacent elements one after
the other, far apart elements are compared.
The given array is divided into sub-arrays through gap and then sort those sub-array. Once
the gap data is one, the elements will be sorted.
It works very well when the array elements are partially ordered and the elements to be
sorted are very less.
127
DATA STRUCTURE
128
DATA STRUCTURE
129
DATA STRUCTURE
Radix sort
Radix sort technique is used by a mechanical card sorter.
In radix sort they should be more than one digit. This method is based on the values of
individual digits in the positional weight representation of the decimal numbers to be sorted.
For instance a three digit decimal number 275 consist of its most significant digit (MSD) 2 in
hundreds position, digit 7 in tens position and its least significant digit (LSD) 5 in the units
position.
One can compare such numbers of equal lengths. Each digit is sorted in turn, starting with
LSD is compared with adjacent number and move if it’s greater into the respective pocket
and hence continue the process through the other digits in the given list from right-to-left
including MSD.
Ex: Consider 8 integer of 3-digit length, sort by Radix Sort Method
890, 456, 224, 122, 102, 275, 321, 765
130