Basic Concept of Data Structure
Basic Concept of Data Structure
Organizing of data
Accessing method
Storing Data
Searching the data
Related term:-
1. Cell:- It is a basic memory cell where the data is going to store. It
may be one byte , one word or more then that
2. Fields: - Field is a entity that is use to represent storage of
different type of data. It can store name, number etc data
3. Record: - A record is an example of basic data structure which
allowed several kinds of data to be store like field or member.
4. List:- A collection of variable and number of items. New item can
be added to the list as well as the data can be removed from the
list.
Classification of data structure:-
Data Structure
1
Primitive Data Structure:- The data which directly operated at
machine level Language is call primitive data structure like int , char ,
float and pointer.
1. Int:- The number is positive or negative but without decimal point
Ex. ‘ A’ , ‘a’
Int a[10] here a is a one array and 10 indicate the limit of that
array means this array can store 10 value with integer data. It’s
index always start with 0.
2
2. Link list:- link list represent the queue of item in which individual
node can be remove , added at any point . each node is link to
previous and the next node. There are various type of link list
of queue.
Basically two function are used for allocate the memory at run time
3
Malloc
Calloc
Syntax:- p= (int *)Malloc( Size of (int) * 10);
2. In Dynamic Memory
2. In Static Memory Allocation Allocation memory is allocated
Memory may be wasted. when it is needed. So there is
no wastage of memory.
Algorithm :-
o Algorithm is a step by step solution of any given problem.
o An algorithm is an effective method express as a finite list of well
define instruction for calculating a function.
o Algorithm is a finite set of instructions that is followed to
accomplish a particular task.
o In mathematics and computing, an algorithm is a procedure for
accomplishing some task which will terminate in a defined end-
state.
o The computational complexity and efficient implementation of
the algorithm are important in computing, and this depends on
suitable data structure.
4
Example:- To compute the average of elements of an array.
1. Initialize sum to 0.
2. Repeat following steps for i=0 to n-1
a. Add a[i] to sum
3. Compute avg as sum/n
4. Return avg
Property required for algorithm.
Space complexity :-
Big O Notation :-
5
The worse case time complexity of the algorithm is the function
defined by the maximum number of operations performed, taken
across all instances of size n.
Best case.
The best case time complexity of the algorithm is the
Average Case.
The average-case time complexity of the algorithm is the function
defined by an average number of operations performed, taken
across all instances of size n.
Worst case
Number of Step
Average case
Best case
1 2 3 4 5 6
Problem Size
Array
Type.
A[0], A[1], A[2], A[3], A[4] in each location we can store the
different value and it is very easy to access the array using the for
loop.
6
Int a[10],I;
For(i=0;i<10;i++)
Scanf(%d”,&a[i]);
Characteristic of Array.
That means for example we can store the 5 different values without
having declare five variables. And different identifier instead of that
using array we can store five different value of the same type with
unique identifier.
Marks array
Here above example we can see the different five block is for different
five values and is initialized by 0 means index and marks[0] is call the
value store in the o location of array.
int a[3][3]
7
Here the first 3 indicate the row and the second 3 indicate the column.
Means the each row contain 3 different values.
When more than two dimensional are indicated is call the multi
dimension array like three dimension and more. In that type of
array contain that much indicator for indicate the different direction
like in three dimension it contain three index like x, y and z.
1 2 3
4 5 6
7 8 9
1 2 3 4 5 6 7 8 9
8
1 4 7 2 5 8 3 6 9
List Array
(4) There are two types of List: (4) There are two types of
array:
1. Linier List
1. One dimensional array
2. Non Linier List
2. Two dimensional array
Array permits the efficient random access. But not efficient for
insertion and deletion.
Array is more convenient for sorting a fixed amount of data
which will be access in unpredictable fashion, while link list
are best for access sequentially and update often with
insertion and deletion.
Searching of array is good locality of reference and so it much
faster to search the data, while in link list has to jump around
in memory.
Array can also be use to represent complex data through
structure, heaps, hash table string, stack and queue but in his
way only.
9
One disadvantage of array is the size of array is always fixed
the size of array can be extended with more expensive
operation but at end of array user want to add some more
data then it has no more space available
String operation:-
o Definition:- String is a collection of more then one character.
Algorithm:-
1. [initialization of counter]
I0
While(S1[I]<> NULL)
I= I + 1
3. [Display Length]
Write (Length = I)
4. [Finished]
Exit.
This algo is use to find out the length of given string. Here initially we
enter the string and declare counter I is 0 then after from initial character
of string to the NULL character of string we check all he character one by
10
one and until we find the NULL character for each iteration we increment
the counter by 1 so at the end of the loop we found the value of counter I
equal to the number of character available in string so it means the length
of string. This way we can find the length of any string.
#include<stdio.h>
#include<conio.h>
void main()
char s1[10];
int len=0, n;
gets(s1);
while(s1[i]!='\0')
len++;
getch();
Output :-
India
Length of string = 5
11
Algorithm:-
1. [initialization]
S2 = NULL
I0
While(S1[I] != NULL)
Begin
S2[I] S1[I]
I=I+1
End
3. [Display String]
Write ( S2)
4.[ Finished ]
Exit
I N D I A \0
S2= NULL
After doing copy from first to second we get the second string as
INDIA because of the character is copy from source to destination one by
one.
12
S2= “INDIA”
I N D I A \0
#include<stdio.h>
#include<conio.h>
void main()
int i, len;
gets(s1);
getch();
Output :-
India
Algorithm:-
13
1. [initialization]
Len= LEN(S1)
I len
J0
While(S2[J] != NULL)
Begin
S1[I] = S2[J]
I = I +1
J = J +1
End
3. [Display String]
Write ( S1)
4. [ Finished ]
Exit
Here we want to concatenation two string means join two string. That
means from last character of first string copy the first character of second
string and so on. Similarly we increment the two pointer and copy all the
character of second string at end of first string and finally the first string
become join of both is call the concatenation of two string and finally we
can display the first string.
I N D I A \0
S2= “GREAT”
G R E A T \0
Here at the end of fist string means from termination of first string
the second string character to start copy so until the second string
become NULL all the character of second are copy to end of first so we
find the combine string. : “INDIAGREAT”
14
I N D I A G R E A T \0
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char s1[10],s2[10];
int i,j,len,k=0;
clrscr();
gets(s1);
gets(s2);
len=strlen(s1);
j=0;
for(i=len;s2[j]!='\0';i++)
s1[i]=s2[j];
j++;
s1[i]='\0';
getch();
15
}
Output :-
India
Great
Algorithm:-
1. [initialization ]
I0
Flag = 0
Begin
Flag = 1
Break
I = I +1
End
If (Flag ==1)
Else
3. [ Finished ]
16
Exit
Here we want to compare two string even they are equal or not. And for
that we have to compare them character by character up to the last
character. So in above algorithm we have initialize variable I and flag
both 0. Then until the last character of any string or the flag variable’s
value is 1 we repeated the procedure for comparing the string character.
Lastly if we found the value of flag is 1 then automatically we can
understand that any of the character from the both string are differ so
string are not same and at the end of the loop if we not found flag‘s value
1 and it is remain as it is 0 then directly we can write the string are same
means all the character of both string are same.
I N D I A \0
S2= “INDONESIA”
I N D O N E S I A \0
Now comparing two string the first three character are same but the
fourth character are different so we found string are not same
I N D I A \0
S2= “INDIA”
I N D I A \0
All the character in both string are same so we found the string are same.
Program for compare to given string either they are same or not.
#include<stdio.h>
#include<conio.h>
void main()
char s1[10],s2[10];
int i, len,k=0;
clrscr();
17
printf("enter string first");
gets(s1);
gets(s2);
k=1;
if(k==1)
else
getch();
Output 1:-
India
Indonesia
India
India
18
Reverse of String
In this algo. we want to reverse the given string means the string is
read from last and one by one character copy to second string from initial
position. In other way we can exchange the value of first and last
character of same string and increment and decrement the start and end
respectively.
Algorithm:-
Begin
S2 [i] S1 [len-1]
Len len -1
End
4. [display string]
Write S2
5. [ finished ]
Here in above algo. We can see the from first string s1 we want to
reverse so the last character from s1 is copy to the first character of s2
and it will repeat in loop until the string become NULL. So at the end of
lop we found the reverse string of s1 in to the s2 and display it. In this
way we get the reverse string.
I N D I A \0
S2= “”
A I D N I \0
In similar way we can also solve the problem like to find sub string and
convert to upper and lower respectively.
19