You are on page 1of 19

UNIT-1 – BASIC CONCEPTS OF DATA STRUCTURE

 Data Structure: - Data Structure is a Possible way of organizing


the data Means not only how data is store but also the relationship
between each other.

Data structure basically doing following operation

 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:-

Normally there are two Type of data structure

 Primitive data structure


 Non Primitive data structure

Data Structure

Primitives D.S Non Primitives D.S

Integer Float Char Pointer Array Link List File

Linear Non Linear

Stack Queue Graph Tree

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. +25 , -45, 78

2. Float:- number with fractional point or decimal point.

Ex. +2.4 , -5.67, 3.5, -2.78

3. Char:- Data may be hold the single character information may be


ASCII or any other code

Ex. ‘ A’ , ‘a’

4. Pointer:- A variable , Value of which is address

Declaration int *p;

Here p is a pointer variable and it contain directly address of the variable


so process become very fast as compare to other normal variable.

 Non Primitive Data structure:- It means group of homogeneous(


same type) and Heterogeneous( Different Type) data item they can be
classified with array, list and file.

1. Array:- array is a set of finite homogeneous element means array


can contain only same type of variable like int only , float only or
char only etc.

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.

Array can be read and write through loop

For ( I=0; I <10; I ++)

Scanf (“%d”, &a[i]); or printf (“%d”, a[i]);

If array is two dimension then we have to use nested loop.

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

 Order singly link list


 Unordered singly link list
 Doubly link list
 Circular link list

3. Stack:- Stack is also an order collection of elements like array, but


it has a special future that deletion and insertion of
element can be done only from one end. And the end is
called TOP. Due to this property stack is also known as Last
in first out (LIFO).
4. QUEUE:- Queue is first in first out type data structure (FIFO). In a
queue new elements are added to the queue at one end is
call REAR and the element is always delete from the other
end is called FRONT end.

Ex. People standing in railway reservation row is a example

of queue.

5. Graph:- graph is a mathematical non-linear data structure capable


of representing many kind of physical structure. It shows the
pair of elements.
6. TREE: - Tree can define as finite set of data items. Tree is a non
linear type of data structure in which data is store and or
arrange in sorted sequence. tree represent the hierarchical
relationship between various item.
 Memory Location: - memory location can be done by two ways

 Compile time or static memory location


 Run time or dynamic memory location
Compile time or static time require amount of memory is allocated
to the starting of program element. Here the memory is to be
allocated to variable and is determined by the compiler at compile
time.

Run time allocation memory is allocated to variable during the run


time is called dynamic memory allocation.

Basically two function are used for allocate the memory at run time

3
 Malloc
 Calloc
Syntax:- p= (int *)Malloc( Size of (int) * 10);

p = (int *) calloc(10, size of(int));

 Difference between Static Memory allocation and Dynamic


Memory allocation.
Static Memory Allocation Dynamic Memory Allocation

1. Static Memory Allocation is 1. Dynamic Memory Allocation


Performed at Compile Time. is Performed at Runtime.

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.

3. Array is an example of static


3. Linked List is an example of
memory allocation.
dynamic memory allocation. We
can use malloc () function to
allocate memory at runtime.

4. In static memory allocation 4. In dynamic memory


memory is allocated allocation memory is not
sequentially. allocated sequentially.

 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.

Steps: Assume that array contains n integer values.

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.

1. Finiteness: “An algorithm must always terminate


after a finite number of steps”.

2. Definiteness: “Each steps of algorithm must be


precisely defined”.
3. Input: “quantities which are given to it initially
before the algorithm begins”.
4. Output: “quantities which have a specified relation
to the input”.
5. Effectiveness: “all the operations to be performed
in the algorithm must be sufficient”.
 Time complexity :-

Time complexity is an algorithm to find the amount of time taken by


an algorithm to run or execute.

 Space complexity :-

Amount of memory require to execute an algorithm.

 Big O Notation :-

Time complexity is of an algorithm is commonly express by big O


notation.

 Algorithm classified based on of Time complexity.


 Worse case

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

Definition: - Array is the collection of variable with the same data

Type.

Ex. Int A [5]

Means here A is declare as array and it can contain 5 different


value but all must be integer because we have declare array A as
integer.

In above example the element are store in array from starting


0 ( zero) index. Means

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.

For entering the array we ca use the syntax

6
Int a[10],I;

For(i=0;i<10;i++)

Scanf(%d”,&a[i]);

From above syntax we can enter 10 different value at the


runtime from keyboard.

 Characteristic of Array.

The initial location of array is A [0] then A [1] and so on.



Array holds a sequence of data element usually of same size

of data type.
 Individual element can access by their position in array.
 The position is given by index and the index is consecutive
range of integer.
 Array can also be one dimension, two dimension and multi
dimension.
 One dimension 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.

Ex. int marks[5]


0 1 2 3 4

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.

 Two dimensional array :-

Two dimensional arrays are containing two different indexes for


indicating two dimensional like x and y means here each block contain
more sub block and so on. So two dimension array represent like that.

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.

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


A[1][0] A[1][1] A[1][2]
A[2][0] A[2][1] A[2][2]
Here the two dimension array always declares with two indexes and
among the two indexes the first is called the row and second called
the column. All the matrix operation is done using the two
dimension array. Like addition, multiplication, transposition etc.

Multi dimensional array:-

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.

(Multi dimensional array)

Storage Representation of Array:-

One dimensional arrays represent simple array discuss in


earlier topic. Two dimensional array are particularly represent the
matrices. Generally we exceeds not more than three dimension.

Suppose if we represent one two dimensional array like

1 2 3

4 5 6

7 8 9

It is most common to index this array using the RC –convention


where the element is referred in row, column fashion. Such as

A1,1 = 1 A1,2 = 2 . . . . A3,2 = 8 , A3,3 = 9

Row major order is

1 2 3 4 5 6 7 8 9

The column major order is

8
1 4 7 2 5 8 3 6 9

Difference between List and Array

List Array

(1) No of elements in a list are (1) No of elements in an array


not fixed. is fixed.

(2) It uses pointer variable (2) It does not use pointer


which occupies an extra memory variable so it does not occupies
space. extra memory space.

(3) Insertion and deletion (3) Insertion and deletion


operation are very easy to operation are very difficult to
perform perform.

(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

(5) Searching is slower in case (5) Searching is faster in case


of List. of array.

Advantage and disadvantage of 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.

String always define under double quotation mark ( “ahmedabad”) .

Basic character of string may be the

 Alphabets like A to Z and a to z


 Numeric like 0 to 9
 Special Symbol like +, -, *, %, =, {, }, [, ] etc.
Every string is always ended with the null character like ‘\0’

Ex. char name[10]

India’\0’ here we can show the null character but in real we


can’t see it but when we want to compare the character for finding
the end of string then it will compare with null and match with it
also. For that we can see the following ex of finding the length of
string.

Calculate the length of string:-

Algorithm:-

1. [initialization of counter]
I0

2. [Read character until NULL character]

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.

Ex. s1[5] = “INDIA”

Length of String is 5 because the number of character in string is 5 so.

Program to find the length of given string.

#include<stdio.h>

#include<conio.h>

void main()

char s1[10];

int len=0, n;

printf("enter the string ");

gets(s1);

while(s1[i]!='\0')

len++;

printf(" length of string = %d",len);

getch();

Output :-

Enter the string

India

Length of string = 5

Copy the one string to other.

11
Algorithm:-

(S1 is source string S2 is destination string which is NULL)

1. [initialization]
S2 = NULL

I0

2. [Repeat this step until the NULL character of source string ]

While(S1[I] != NULL)

Begin

S2[I]  S1[I]

I=I+1

End

3. [Display String]

Write ( S2)

4.[ Finished ]

Exit

In above algorithm we want to do copy the source string to destination.


For that we have first enter the source string and the destination string
made NULL in advance. Then also the counter I is initialize to 0. then until
the NULL character of first (Source) string found one by one we copy the
character from source to destination and at the end we found all the
character in destination string. This is the way to copy the sting and lastly
we display the destination string which contains the value of source
string.

Ex. S1= “INDIA”

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

Program for copy one string to other is

#include<stdio.h>

#include<conio.h>

void main()

char s1[10], s2[10];

int i, len;

printf("enter string first ");

gets(s1);

for(i=0;s1[i]!='\0';i++) /* increment up to the

{ last character of first

s2[i]=s1[i]; String come.*/

printf("the second string =%s",s2);

getch();

Output :-

Enter string first

India

The second string = india

Concatenation of two string:-

Algorithm:-

Suppose S1= “ HELLO” & S2= “ GREAT”

13
1. [initialization]
Len= LEN(S1)

I  len

J0

2. [Repeat until the NULL character of second string ]

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.

Ex. S1= “INDIA”

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

Program for joining two string

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char s1[10],s2[10];

int i,j,len,k=0;

clrscr();

printf("enter string first ");

gets(s1);

printf("enter the second string \n");

gets(s2);

len=strlen(s1);

j=0;

for(i=len;s2[j]!='\0';i++)

s1[i]=s2[j];

j++;

s1[i]='\0';

printf("combine string = %s",s1);

getch();

15
}

Output :-

Enter string first

India

Enter string second

Great

Combine string = indiagreat

Comparison of two string:-

Algorithm:-

1. [initialization ]
I0

Flag = 0

2. [ Repeat until the NULL character of source or Flag become 1 ]


While (S1 [i]! = NULL && Flag = False (0))

Begin

If (S1 [I]! = S2 [I])

Flag = 1

Break

I = I +1

End

If (Flag ==1)

Write (“String Are not same”)

Else

Write (“String Are Same”)

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.

Ex1. S1= “INDIA”

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

Ex2. S1= “INDIA”

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);

printf("enter the second string \n");

gets(s2);

For (i=0;(s1[i]!='\0' || s2[i]!='\0');i++)

If (s1 [i]! =s2 [i])

k=1;

if(k==1)

printf("given string are not same");

else

printf("given string are same");

getch();

Output 1:-

Enter string first

India

Enter string second

Indonesia

Given string are not same

Output 2:- enter string first

India

Enter string second

India

Given string are same

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:-

1. [Initialize the string and variable]


I0

2. [find the length of string]


Len  strlen(S1)]

3. [repeat until string become null]


While S1 <> NULL

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.

Ex1. S1= “INDIA”

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

You might also like