Data Structure: Using C
Data Structure: Using C
Data Structure: Using C
Using C
Why Data Structure?
• The data structure name indicates itself that organizing the data in memory.
There are many ways of organizing the data in the memory as we have already
seen one of the data structures, i.e., array in C language.
• The data structure is not any programming language like C, C++, java, etc. It is a
set of algorithms that we can use in any programming language to structure the
data in the memory.
• To structure the data in memory, 'n' number of algorithms were proposed, and all
these algorithms are known as Abstract data types. These abstract data types are
the set of rules.
Need of Data Structures
• As applications are getting complexed and amount of data is increasing day by day, there may
arrise the following problems:
• Processor speed: To handle very large amout of data, high speed processing is required, but as
the data is growing day by day to the billions of files per entity, processor may fail to deal with
that much amount of data.
• Data Search: Consider an inventory size of 106 items in a store, If our application needs to search
for a particular item, it needs to traverse 106 items every time, results in slowing down the search
process.
• Multiple requests: If thousands of users are searching the data simultaneously on a web
server, then there are the chances that a very large server can be failed during that process
• in order to solve the above problems, data structures are used. Data is organized to form a
data structure in such a way that all items are not required to be searched and required data
can be searched instantly.
Difference between data type and data structure
• A data type is the most basic and the most common classification of data. It is
this through which the compiler gets to know the form or the type of
information that will be used throughout the code.
• So basically data type is a type of information transmitted between the
programmer and the compiler where the programmer informs the compiler
about what type of data is to be stored and also tells how much space it
requires in the memory. Some basic examples are int, string etc. It is the type
of any variable used in the code.
• A data structure is a collection of different forms and different types of data
that has a set of specific operations that can be performed. It is a collection of
data types. It is a way of organizing the items in terms of memory, and also the
way of accessing each item through some defined logic. Some examples of data
structures are array, stacks, queues, linked lists, binary tree and many more.
Introduction
• Data Structure can be defined as the group of data elements which provides an
efficient way of storing and organizing data in the computer so that it can be used
efficiently.
• Some examples of Data Structures are arrays, Linked List, Stack, Queue, etc. Data
Structures are widely used in almost every aspect of Computer Science i.e.
Operating System, Compiler Design, Artifical intelligence, Graphics and many
more.
Types of Data Structures
• There are two types of data structures:
• Primitive data structure
• Non-primitive data structure
• Primitive Data structure
• 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.
• Non-Primitive Data structure
• The non-primitive data structure is divided into two types:
• Linear data structure
• Non-linear data structure
Operations on data structure
• The most commonly used operation on data structure are broadly
categorized into following types:
• Create
• Insertion
• Selection
• Updating
• Searching
• Sorting
• Merging
• Destroy or Delete
Advantages of Data Structures
• Efficiency: Efficiency of a program depends upon the choice of data structures.
For example: suppose, we have some data and we need to perform the search
for a perticular record. In that case, if we organize our data in an array, we will
have to search sequentially element by element. hence, using array may not be
very efficient here. There are better data structures which can make the search
process efficient like binary search tree or hash tables.
• Reusability: Data structures are reusable, i.e. once we have implemented a
particular data structure, we can use it at any other place. Implementation of
data structures can be compiled into libraries which can be used by different
clients.
• Abstraction: Data structure is specified by the ADT which provides a level of
abstraction. The client program uses the data structure through interface only,
without getting into the implementation details.
Array
• An array is a group of consective memory locations with same name
and datatype.
• An array is a collection of items stored at contiguous memory locations.
• Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc.
• The idea is to store multiple items of the same type together. This makes it easier
to calculate the position of each element by simply adding an offset to a base
value, i.e., the memory location of the first element of the array (generally
denoted by the name of the array).
8
i=6
arr[i+1]=arr[i] ()
i--= 5;
Arr[i+1]=arr[i]=> arr[6]=arr[5]
2 4 8 6 9 12 7 3
0 1 2 3 4 5 6 7 8 9
Contd..
• Simple variable is a single memory location with unique name and a
type. But an Array is collection of different adjacent memory
locations. All these memory locations have one collective name and
type.
• The memory locations in the array are known as elements of array.
The total number of elements in the array is called length.
• The elements of array is accessed with reference to its position in
array, that is call index or subscript.
Advantages of Array
• Array provides the single name for the group of variables of the same
type therefore, it is easy to remember the name of all the elements of
an array.
• Traversing an array is a very simple process, we just need to
increment the base address of the array in order to visit each element
one by one.
• Any element in the array can be directly accessed by using the index.
Types of Arrays:
• One-Dimensional Array
• Two-Dimensional Array
• Multi-Dimensional Array
One-D array Intialization
10 11 12 13 14 15 16 ….
B+W*[I-LB]
100+2[14-10]
108
Example:
• Given the base address of an array B[1300…..1900] as 1020 and size of
each element is 2 bytes in the memory. Find the address of B[1700].
Solution:
• The given values are: B = 1020, LB = 1300, W = 2, I = 1700
• Address of A [ I ] = B + W * ( I – LB )
• = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]
C Multidimensional Arrays
In C programming, you can create an array of arrays. These
arrays are known as multidimensional arrays. For example,
float x[3][4];
ROW MAJOR
A [ I ][ J ] = B + W * [ N *I +J]
COLUMN MAJOR
A[I][J]= B + W*[M*J+I]
Q1. T[20][12],
B.A=100,CHARACTER,ARRAY[100][200]
• 100+1[200*20+12]=100+4012=4112
• =100+1[100*12+20]=1220+100=1320
• Q2. Given an array [6][7] of integers. Calculate address of element T[4,6], where BA=900.
A [ I ][ J ] = B + W * [ N *I +J]
ROW MAJOR =900+4[7(4)+6]=1036
COLUMN MAJOR= 900+4[6*6+4]=1060
Array of size A[20][30], T[10][20], B.A=100,
W=2
• A[M][N], M=20, N=30
• ROW MAJOR:
• 100+2(30*10+20)=740
• COULMN MAJOR:
• 100+2(20*20+10)=920
A[0….10][0….25]
X[5][24], B.A=1500, W=1
• M=10-0+1=11
• N=25-0+1=26
• 3*6= I*N+J
100 1 2 3 4 5
dfgdf
A[3][2]
• Important : Usually number of rows and columns of a matrix are given
( like A[20][30] or A[40][60] ) but if it is given as A[Lr- – – – – Ur, Lc- – – –
– Uc]. In this case number of rows and columns are calculated using the
following methods:
• b) A matrix M[15][10] is stored in the memory with each element requires 4 bytes of storage . if the
address at B[1][y] is 716 and the address at B[8][5] is 1000. Determine the value of ‘y’, when the matrix is
stored in Row Major Wise.
• =M=15,N=10,W=4,
• 716=B+4[10*1+Y] …….1 => 716-40-4Y=B
• 1000=B+4[10*8+5] ………2 => 1000-340=B
• 1000-340=716-40-4Y=>-16=-4Y
• Individual Element:
Array_name[index];
• Using Loop:
int marks[5];
for(int i=0;i<=4;i++)
marks[i]=i;
Searching In Array
5 6 9 8 1
INSERTION
printf("Enter the index where you want to insert new
element: ");
scanf("%d", &pos);
return 0;
}
Deletion Operation
• Deletion refers to removing an existing element from the array and re-
organizing all elements of an array.
• Deletion refers to removing an existing element from the array and re-
organizing all elements of an array.
Deletion
Deletion
#include<stdio.h> if (pos >= n+1)
int main() printf("Deletion not possible.\n");
else
{
{
int array[20], pos, i, n; for (i =pos-1; i<n-1; i++)
printf("Enter number of elements in array"); array[i] = array[i+1];
scanf("%d", &n);
printf("Enter %d elements\n", n); printf("Resultant array:\n");
for (i = 0; i<n-1; i++)
for (i = 0; i<n; i++)
printf("%d\n", array[i]);
scanf("%d", &array[i]]); }
printf("Enter the location where you wish to
delete element\n"); return 0;
scanf("%d", &pos); }
Search Operation
• Linear search
• Binary search
2 4 5 2 3 6 5 2
2 4 5 3 6
Sequential Search
2 4 7 9 10 15 18 23 29
X=15
M=L+H/2=4
A[M]=X
A[M]>X, 18>15
H=M-1
}cout<<endl;}
}return 0;
}
Merge two sorted arrays while (i < m && j < n) while (i < m)
{ {
#include <stdio.h> if (array1[i] < array2[j]) array3[k] = array1[i];
{ i++;
void main() k++;
array3[k] = array1[i];
{ }
i++;
int array1[10], array2[10], array3[20], m, n, i, j, k=0;
} printf("\n After merging: \n");
printf("\n Enter size of array Array 1: "); else for (i = 0; i < m + n; i++)
scanf("%d", &m); { {
array3[k] = array2[j];
printf("\n%d", array3[i]);
printf("\n Enter sorted elements of array 1: \n");
}
for (i = 0; i < m; i++) j++;
} }
scanf("%d", &array1[i]);
k++;
printf("\n Enter size of array 2: ");
}
scanf("%d", &n); while (j < n)
printf("\n Enter sorted elements of array 2: \n"); {
for (i = 0; i < n; i++) array3[k] = array2[j];
scanf("%d", &array2[i]); j++;
k++;
i = 0;
}
j = 0;
2,3,4,2,3,5,3,3,4
2,3,4,5