You are on page 1of 13

UNIT 1 Data Structures Using C 1

Data Structure
The organized collection of data in a mathematical or logical way is called Data Structure.
OR
A way of storing and organizing data in a computer is called data structure.
Classification of Data Structure:

Data
Structure

Primitive Data Non-Primitive Data


Structure Structure

Linear Data Non-linear Data


int Structure Structure

char Array Trees

float Graphs
Linked List
Double
Stack

Queue

Primitive Data Structure: A data structure can be manipulated by machine level instructions
is called primitive data structure. Example: int, float, char, double, long etc.
Non-Primitive Data Structure: A data structure which cannot be manipulated directly by
machine level instructions is called non-primitive data structure. They are two types:
1. Linear Data Structure
2. Non-linear Data Structure
Linear Data Structure: A Data Structure which stores all the elements sequentially in memory
is called linear Data Structure. Example: Array, Linked List, Stack and Queue.
Array: It is a definite collection of homogenous elements that can be stored sequentially.
Example: int A[3];
Memory representation of an array
101 102 103 104 105 106 107 108

A[0] A[1] A[2] A[3]


Array may be 1D, 2D and Multi-dimensional.
Linked List: A list is a linear data structure, it is a collection of data items named as nodes
each node is divided into two fields that is data and link field. Example

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 2

50 60 70 80 NULL
The types of linked list are:
1. Singly List
2. Doubly List
3. Circular List
Stack: It is a linear data structure, the data can be inserted and deleted at one end is known
TOP. Stack works in the fashion of LIFO.

C top
B
A
Queue: Queue is a linear data structure which has two ends. One end is used to insert the
element named as REAR and the other end is used to delete the element named as FRONT.
FRONT REAR

A B C D E F G
Non-Linear Data Structure
Data structures where data elements are arranged sequentially or linearly are called non-linear
data structures. Example: Tree, Graph.
Tree: It is a finite set of nodes or vertices each tree has a root node and remaining nodes are
considered as leaf nodes or sub trees.

B C

D E

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 3

Here, A is root node, C,D,E is leaf node, B is a internal node.


Graph: Graph is a finite set of ordered pair G=(V,E) where V is the vertices and E is the edges.
It is a collection of adjacent vertices.

B C

Operations on Data Structures:


The data structure can be classified as
• Insertion
• Deletion
• Traverse
• Searching
• Sorting
• Merging
Insertion: Adding a new element to the existing data structure.
Deletion: Deleting an element from Data Structure.
Traverse: Accessing each element exactly once.
Searching: Search a particular element in the list of elements present in the Data Structure.
Sorting: Arranging the elements in ascending or descending order.
Merging: Combining two sorted Data Structure into a single sorted Data Structure.

Algorithm: An algorithm is a step by step procedure to perform a specific task in a finite


amount of time.

Algorithm complexity: It is a measure of amount of time and space required by an algorithm


for an input of a given size ‘N’.
Time Complexity: Time complexity describes the amount of time an algorithm takes in terms
of the amount of input to the algorithm.
Space Complexity: Space complexity describes the amount of memory space an algorithm
takes in terms of the amount of input to the algorithm.

Mathematical notations and functions


1. Floor & Ciel function
Floor function provides the greatest integer that is not greater than the given value. The
symbol for floor function is ⌊ ⌋
Example x=4.678
⌊𝒙⌋ =4

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 4

Ceil function provides the smallest integer that is greater or equal to the given value.
The symbol for ceil function is ⌈ ⌉
Example x=4.678
⌈𝒙⌉ = 5
2. Modular function or remainder function
If x is an integer and m is a positive integer then x(mod m) gives the integer remainder.
Example x=25 and m=3
25 (mod 3)
=1
3. Integer function
If x is a real number the integer function int(x) returns the integer value and it truncates
the fractional part.
Example INT(45.98)
=45
INT(-900.456)
=-900
4. Absolute function
The function ABS(x) gives the absolute value of x, which means it gives +ve value of
x even if it is -ve.
Example x= - 900 x=500
ABS(x)=900 ABS(x)=500
5. Summation
Consider a sequence of terms 𝒂𝟏 + 𝒂𝟐 + 𝒂𝟑 + ⋯ + 𝒂𝒏 and it is denoted by ∑𝟏≤𝒊≤𝒏 𝒂𝒊
6. Factorial Function
The product of positive integers from one to n (1 to n) is called 𝑛!
𝑛! = 1 × 2 × 3 × … … … .× 𝑛 − 1 × 𝑛
7. Permutation
A permutation is a set of arrangement of elements of the set in some order.
Eg: LOW
The possibilities of arrangement is LOW, LWO, OLW, OWL, WLO, WOL
8. Exponents & Logarithms
Exponent means how many times a number is multiplied by itself if a is the base m is
an exponent.
Eg: 23 = 2 × 2 × 2
Logarithm: If B is a positive number then the logarithm of positive integer, x to the
base b is written as log 𝑏 𝑥
9. Algorithmic Notation
Algorithm: It is a step by step procedure to perform a particular task

ARRAY
Array
It is a collection of element of same datatype.
Or

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 5

It is a collection of elements or data items of homogenous type.


Linear Array
It is a list of fixed size number N of similar datatype element (or) data item
The elements in linear array are stored sequentially in memory. Here n is the size of the
array. To find the length of the array
length of the array =UB – LB + 1
where LB is the lower bound, UB is the upper bound
Ex: int a[10]
Length of array=10-0+1
=11
It holds 11 elements
Array ADT
An array is a fixed collection of elements of same type. The array data such as size, type, the
index. The operation such as traverse, search, insert, delete, sort, merge.
Classification of Array:
It can be classified into
1. One Dimensional Array (1D)
2. Two-Dimensional Array (2D)
3. Multi-Dimensional Array (m-D)
One Dimensional Array
An array which has only one subscript is called one-Dimensional array.
General syntax: 𝑑𝑎𝑡𝑎𝑡𝑦𝑝𝑒 𝑎𝑟𝑟𝑎𝑦_𝑛𝑎𝑚𝑒 [𝑠𝑖𝑧𝑒];
Ex: int a[10];
Here a is the name of the array, int is the datatype and size is 10
Two-Dimensional Array
An array which has two subscripts is called two-Dimensional array.
General syntax: 𝑑𝑎𝑡𝑎𝑡𝑦𝑝𝑒 𝑎𝑟𝑟𝑎𝑦_𝑛𝑎𝑚𝑒 [𝑟𝑜𝑤_𝑠𝑖𝑧𝑒][𝑐𝑜𝑙_𝑠𝑖𝑧𝑒];
Ex: int mat[2][2];
Here mat is the name of the array, int is the datatype and row_size is 2, col_size is 2. It can
hold 4 elements.
Multi-Dimensional Array
An array which has more subscripts is called multi-Dimensional array.
General syntax: 𝑑𝑎𝑡𝑎𝑡𝑦𝑝𝑒 𝑎𝑟𝑟𝑎𝑦_𝑛𝑎𝑚𝑒 [𝑠𝑖𝑧𝑒1][𝑠𝑖𝑧𝑒2]… . … .[𝑠𝑖𝑧𝑒_𝑛];

Operation on Array
1. Traversal: processing each element in array
2. Insertion: Adding a new element in array
3. Deletion: Deleting an element from the array
4. Search: find the location of the given element
5. Sorting: Arranging the elements in some order
6. Merging: combining two arrays into one array

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 6

1. Traversal
It means accessing all elements in the array.
Algorithm
TRAVERSE( )
1. Start the program
2. Repeat I = LB to UB
Process a[ I ]
End Loop
3. Stop the program
Here a is the array, LB is the lower bound and UB is the upper bound we process all the
elements of array using loop.
Example
# include<stdio.h>
void main()
{
int a[ ]={10, 20, 30, 40 }
for(int i=0; i<4; i++)
printf(“%d\t”, a[i]);
}
Output
10 20 30 40
2. Insertion
Inserting an element means to add an element in the array.
Algorithm
INSERT(A, N, LOC, ITEM )
1. Read the array, N value, LOC to insert at position, ITEM to insert
2. Initialize I=N
3. Repeat while( I >= LOC )
Set A[ I + 1 ]=A[ I ]
Set I= I – 1
End loop
4. Insert the element at the position by A[ LOC ] = ITEM
5. Increment N by 1, N = N + 1
6. End of the program
Here A is an array, N is the number of elements in the array, LOC is the position to insert an
element, ITEM is the element to be inserted. To create the space for the new item, move the
element of A by 1 up to the space created. Then insert the new item using A[ LOC ] = ITEM
and increment the size of the array by 1 by using N = N + 1
void INSERT(int A[],int n) // A is the array and n is the size of the array
{

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 7

int LOC,ITEM,i;
printf(“\n Enter the location where you wish to insert an element: “);
scanf(“%d”,&LOC);
if(LOC >= n+1)
printf(“\n Insertion is not possible.\n”);
else
{
printf(“ \nEnter the value to insert: ”);
scanf(“%d”, &ITEM);
for(i=n-1;i>=LOC-1;i--)
A[i+1] = A[i];
A[LOC-1] = ITEM;
n= n+1;
}
}

4. Deletion
Deleting an element means removing an element from the array.
Algorithm
DELETE(A, N, LOC, ITEM )
1. Read the array, N value, LOC is the location to delete, ITEM to delete
2. Set ITEM=A[ LOC ] and delete the ITEM
3. Repeat for I= LOC to N
Set A[ I ] = A[ I + 1 ]
End for
4. decrement N by 1, N = N – 1
5. End of the program
Here A is an array, N is the number of elements in the array, LOC is the position to delete an
element, ITEM is the element to be delete. Assign ITEM= A[LOC ] and delete the data. Then
left shift one value in the array until the size of the array is reached and+ decrement the size of
the array by 1
void DELETE(int A[],int n) // A is the array and n is the size of the array
{
int LOC,ITEM,i;
printf(“\n Enter the location where you wish to delete an element: “);
scanf(“%d”,&LOC);
if(LOC >= n+1)
printf(“\n Deletion is not possible.\n”);
else

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 8

{
for(i=LOC-1;i<n-1;i++)
A[i] = A[i+1];
A[LOC-1] = ITEM;
n= n-1;
}
}

5. Searching.
It refers to finding an element in an array.
Linear Search
It is also known as sequential search. It searches all the elements sequentially in list. If the
element is present in the list it print the element is found at index position, otherwise it return
-1(not found).
Algorithm:
LINEAR SEARCH(A, N, KEY)
Set I=0
While ( I <= N ) do
If A[ I ]= = KEY then
Return I
End Loop
Return -1
This algorithm sets the index value I as 0 initially it checks all the elements in the array
sequentially. If the array element is equal to the KEY then it print the location by using the I
value otherwise it return -1.
Complexity of linear search
For worst case it takes ‘O(n)’ comparisons
int linear_search(int item,int array[100],int n)
{
int i;
for(i=0;i<n;i++)
{
if(item == array[i])
{
return i+1;
}
}
return -1;
}

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 9

Binary Search
Binary Search follows the divide and conquer approach. The list must be in sorted manner.
The following algorithm
Algorithm:
BINARY SEARCH ( A, N, ITEM)
Read the Array A, the size of the array N, the ITEM to be searched
Initialize FIRST = 0, LAST= N-1, MID= (FIRST+LAST)/2
While (FIRST<=LAST)
if (ITEM == A[MID] )
return MID+1
else if ( ITEM < A[MID] )
LAST= MID – 1
Else
FIRST= MID + 1
End while
Return -1
The required entry is first compared with the middle element. If the match is found then, the
location of the middle element is returned. Otherwise, if the middle element is greater than the
search element then it searches the second half of the array. If the search element is less than
the middle element then it searches the first half of the array. This is repeated until the search
element is found otherwise not found.
int binary_search(int item,int array[100],int n)
{
int mid,first,last;
first = 0;
last = n-1;
mid = (first + last)/2;
while(first<=last)
{
if(item == array[mid])
{
return mid +1;
}
else if(item <= array[mid])
{
last = mid+1;
mid = (first+last)/2;
}
else
{
first = mid+1;
mid = (first+last)/2;
}

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 10

}
return -1;
}
Complexity of binary search
For worst case it takes ‘’ comparisons
Sparse matrix
Matrix which contain more number of zero entries than non-zero entries is called sparse
matrix.
𝟎 𝟎 𝟕 𝟎 𝟎 𝟓
𝟏 𝟎 𝟎 𝟎 𝟎 𝟎
Example 𝟎 𝟎 𝟎 𝟎 𝟗 𝟎
𝟎 𝟎 𝟎 𝟎 𝟎 𝟖
[𝟎 𝟎 𝟑 𝟎 𝟎 𝟎]
The above matrix contains 30 entries but it contains only 6 non-zero entries. This matrix takes
60 bytes to store the data. Even it has only 6 non-zero element. So, represent a new matrix for
storing non-zero present in the above matrix.
Row Column Value
1 3 7
1 6 5
2 1 1
3 5 9
4 6 8
5 3 3

The following program accepts a matrix as input and prints the 3-tuple representation of it.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10], row, col, i, j;
printf(“Enter the order of the matrix:”);
scanf(“%d%d”, &row, &col);
printf(“\nEnter the elements of the matrix:”);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf(“%d”,&a[i][j]);
printf(“The 3-tuple representation of the matrix :\n”);
for(i=0;i<row;i++)

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 11

for(j=0;j<col;j++)
{
if(a[i][j]!=0)
{
printf(“%d\t%d\t%d”,(i+1),(j+1),a[i][j]);
}
}
}
Output
Enter the order of the matrix: 5 5
Enter the elements of the matrix:
𝟎 𝟎 𝟎 𝟎 𝟖
𝟏 𝟎 𝟎 𝟎 𝟎
𝟎 𝟎 𝟗 𝟎 𝟎
𝟎 𝟎 𝟎 𝟎 𝟎
𝟎 𝟎 𝟔 𝟎 𝟎
Enter the elements of the matrix:
1 5 8
2 1 1
3 3 9
5 3 6

Asymptotic notations
• It is the mathematical way of representing the time complexity of an algorithm.
• Following asymptotic notations are used to calculate the running time complexity of
an algorithm.
• O − Big Oh – upper bound
• Ω − Omega – lower bound
• θ − Theta- average bound
Big Oh Notation
• This method is used to find the upper bound of an algorithm’s running time.
• It is the worst case time complexity of an algorithm.
Definition: The function f(n) = O(g(n)) if and only if there exists a positive constants c and
n0 such that f(n)<=c*g(n) for all n>=n0
The following figure represents Big oh Notation

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 12

Let f(n) = 2n+3


lets take 2n+3 <=10n
let n=1
therefore 5<=10 is true for all n, n>=1
Here f(n) <=cg(n) for all n>=n0 where n0 = 1, c = 10, g(n) = n
Therefore, f(n) =O(n) which is closer to the function.

Omega (Ω) Notation


• This method is used to find the lower bound of an algorithm’s running time.
• It is the best case time complexity of an algorithm.
Definition: The function f(n)= Ω (g(n)) if and only if there exists a positive constants c
and n0 such that f(n)>=c*g(n) for all n>=n0
The following figure represents omega Notation

Let f(n) = 2n+3


lets take 2n+3 >=1 * n
let n=1
therefore 5>=1 is true for all n, n>=1
Here f(n) >=cg(n) for all n>n0 where n0 = 1, c = 1, g(n) = n
Therefore, f(n) = Ω (n) which is closer to the function.

SINDHI COLLEGE BANGALORE - 24


UNIT 1 Data Structures Using C 13

Theta (θ) Notation


The theta notation can be used when the function f(n) can be bounded both from lower bound
and upper bound.
Definition: The function f(n)= θ(g(n)), if and only if there exists a positive constants c1,c2
and n0 such that c1*g(n)<=f(n)<=c2*g(n)
The following figure represents theta Notation

Let the function be F(n)=2n+3


1*n <= 2n+3 <= 10*n
This is in the form c1*g(n)<=f(n)<=c2*g(n)
Therefore, we can write F(n)= θ (n) and this is the average bound of a function f(n).

Row major and column major representation of multidimensional array


In Row Major Order, elements of a multi-dimensional array are arranged sequentially row
by row, which means filling all the index of first row and then moving on to the next row.
Example {1,2,3,4,5,6,7,8}

In Column Major Order, elements of a multidimensional array are arranged sequentially


column-wise which means filling all the index of the first column and then move to the next
column. Example {1,2,3,4,5,6,7,8}

SINDHI COLLEGE BANGALORE - 24

You might also like