Sie sind auf Seite 1von 32

Master of Computer Applications

Breaking Barriers and Building Future

Name of the Course : Data Structures Using C


Module1. Introduction to Data Structures

Course Code: 16MCA21


Objectives
 To understand and recognize the abstract data types as relate
to data structures.
 To review about pointers for various data types.
 To understand basic concepts on arrays with its
implementations.
 To understand various string handling functions.

Master of Computer Applicati


2 Breaking Barriers and Building Futu
Data Structures

 Data structure is a particular way of storing and


organizing data in a computer so that it can be used
efficiently.

 Data Structure – The study of how information is


organized in a computer, how it can be manipulated and
how it can be utilized.

Master of Computer Applicati


3 Breaking Barriers and Building Futu
Information & Meaning

 Abstract Data Type (ADT)


 A useful tool for specifying the logical properties of a
data type is the Abstract Data Type.

 Data type - A collection of values and set of operations


on those values.

 ADT is a useful guideline to implementors and a useful


tool for programmers to use the data type correctly.

Master of Computer Applicati


4 Breaking Barriers and Building Futu
 ADT for Rational Number
 Rational Number: A number that can be expressed as
the quotient of 2 integers. (Numerator &
Denominator)
 Operations on Rational Number: Creation from 2
integers, addition, multiplication and equality testing.

Master of Computer Applicati


5 Breaking Barriers and Building Futu
 An ADT consists of 2 parts – Value definition & Operator
definition.
 Value definition – collection of values for the ADT. It consists
of 2 parts – definition clause & condition clause
 Operator definition – It is an abstract function with 3 parts –
Header, PreConditions (Optional) and PostConditions.

Master of Computer Applicati


6 Breaking Barriers and Building Futu
/*value definition*/
abstract typedef <integer,integer> RATIONAL;
condition RATIONAL[1] != 0;

/*operator definition*/
abstract RATIONAL makerational(a,b);
int a, b;
precondition b != 0;
postcondition makerational[0] == a;
makerational[1] == b;

Master of Computer Applicati


7 Breaking Barriers and Building Futu
abstract RATIONAL add(a, b) /*written a + b*/
RATIONAL a, b;
postcondition add[1] == a[1] * b[1];
add[0] == a[0]*b[1] + b[0]*a[1];

abstract RATIONAL mult(a, b) /*written a * b*/


RATIONAL a, b;
poscondition mult[0] == a[0] * b[0];
mult[1] == a[1] * b[1];

abstract equal(a, b) /*written a==b*/


RATIONAL a, b;
postcondition
equal == (a[0]*b[1] == b[0]*a[1]);
Master of Computer Applicati
8 Breaking Barriers and Building Futu
 Note: Refer Notes for the explanations and Examples of
the operations.

Master of Computer Applicati


9 Breaking Barriers and Building Futu
 Data Types in C:
 Basic Data Types : int, float, char & double.

Master of Computer Applicati


10 Breaking Barriers and Building Futu
 Pointers in C:
 C language allows the reference to the location of objects
and the content of the location.
 If x is declared as an integer , &x refers to the location of
x, then &x is called as pointer.

Master of Computer Applicati


11 Breaking Barriers and Building Futu
int *pi;
float *pf;
char *pc;

pi=(int *)pf;
int x;

pi=&x; x=*pi;
*(pi+1)
*pi+1 Master of Computer Applicati
12 Breaking Barriers and Building Futu
By value, by reference
1 x=5;
2 printf(“%d\n”,x);
3 funct(x);
4 printf(“%d\n”,x);
..
5 void funct(int y){
6 ++y;
7 printf(“%d\n”,y);
8} Master of Computer Applicati
13 Breaking Barriers and Building Futu
1 x=5;
2 printf(“%d\n”,x);
3 funct(&x);
4 printf(“%d\n”,x);
5 void funct(int *py){
6 ++(*py);
7 printf(“%d\n”,*py);
8}

Master of Computer Applicati


14 Breaking Barriers and Building Futu
 Data Structures & C:
 The study of Data Structure involves 2 goals.
 First goal is to identify and develop useful mathematical
entities and operations to determine what problems can be
solved by using these entities and operations.
 Second goal is to determine representations for those
abstract entities and to implement the operations.

Master of Computer Applicati


15 Breaking Barriers and Building Futu
 Types of Data Structure: Primitive and Non-
primitive.
 Primitive: It can directly manipulate the machine
instructions. (int, float, char, pointers)
 Non-Primitive: It can’t directly manipulate machine
instructions. It is divided into 2 categories as Linear and
Non-Linear.

Master of Computer Applicati


16 Breaking Barriers and Building Futu
 Linear
 In linear data structures, values are arranged in linear fashion.
Arrays, linked lists, stacks and queues are examples of linear
data structures in which values are stored in a sequence.
 Non-Linear
 This type is opposite to linear. The data values in this
structure are not arranged in order. They are distributed.
Tree, graph, table and sets are examples of non-linear data
structures.

Master of Computer Applicati


17 Breaking Barriers and Building Futu
 Types of data structures.

Master of Computer Applicati


18 Breaking Barriers and Building Futu
Examples with Representations.

Master of Computer Applicati


19 Breaking Barriers and Building Futu
 Arrays in C:
 Array : Finite ordered set of homogeneous elements.
 One-dimensional array
Example: int a[100];
Basic operations on Arrays: extracting and storing
Lower bound is always 0,
Upper bound is fixed at the time of writing the program.
Range of the Array = Upper – Lower + 1.

Master of Computer Applicati


20 Breaking Barriers and Building Futu
Declaring the bound as the constant identifier:

#define NUMELTS 100


int a[NUMELTS];
for(int i=0;i<NUMELTS;a[i++]=0);

Master of Computer Applicati


21 Breaking Barriers and Building Futu
 Array as an ADT:
 /* Value Definition*/
abstract typedef <<eltype,ub>> ARRTYPE(ub,eltype);
condition type(ub)==int;
/* Operator Definition*/
abstract eltype extract(a,i) /* written a[i] */
ARRTYPE(ub, Eltype) a;
Int i;
precondition 0<=i<ub;
postcondition extract==ai;

Master of Computer Applicati


22 Breaking Barriers and Building Futu
abstract store(a,i,elt) /* written a[i]=elt */
ARRTYPE(ub,eltype) a;
int i;
eltype elt;
precondition 0<=i<ub;
postcondition a[i]==elt;

Master of Computer Applicati


23 Breaking Barriers and Building Futu
 Using One-Dimensional Arrays:
 Example – Read 100 integers, find their average and
determine by how much each integer deviates from the
average.
#define NUMELTS 100
void main(){
int num[NUMELTS];
int I;
int total;
float avg;
float diff;
total=0;
for(i=0;i<NUMELTS;i++){
scanf(“%d”,num[i]);
total+=num[i];
} Master of Computer Applicati
24 Breaking Barriers and Building Futu
avg=(float)total/NUMELTS;
printf(“\nnumber difference”);
for(i=0;i<NUMELTS;i++){
diff=num[i]-avg;
printf(“\n %d %f”, num[i], diff);
}
printf(“\n average= %f”,avg);
}

Master of Computer Applicati


25 Breaking Barriers and Building Futu
 Implementing One-Dimensional Arrays:
 int b[100];
 The address of the first of these locations is called as base
address of the array - base (b).
Base address:
Suppose the size of the element of the array is esize then
b[i] represents
base(b)+i*esize

Master of Computer Applicati


26 Breaking Barriers and Building Futu
 Varying-sized element array: reserve a contiguous set of
memory locations each of which holds an address of an
element.
 The contents of the memory location are the address of
the varying length array element in some other portions
of memory.
 Note: Refer notes for example.

Master of Computer Applicati


27 Breaking Barriers and Building Futu
 Arrays as Parameters:
 Example:
float avg(float a[], int size){
int I;
float sum;
sum=0;
for(i=0;i<size;i++) sum+=a[i];
return (sum/size);
}
#define ARANGE 100
float a[ARANGE];
..
avg(a, ARANGE);
//Array is passed by reference saving space and timeof Computer Applicati
Master
28 Breaking Barriers and Building Futu
 Character Strings in C and their Operations:
#define STRSIZE 80
Char string[STRSIZE];
// To find the length of the given string
int Strlen(char string[]){
int i;
for(i=0;string[i]!=‘\0’;i++);
return i;
}

Master of Computer Applicati


29 Breaking Barriers and Building Futu
// To combine 2 strings S1 & s2
void strcat(char s1[], char s2[]){
int i,j;
for(i=0; s1[i]!=‘\0’; i++);
for(j=0; s2[j]!=‘\0’; s1[i++]=s2[j++]);
}
// To extract String S2 from original String S1
void substr(char s1[],int i, int j, char s2[]){
int k,m;
for(k=i,m=0; m<j;s2[m++]=s1[k++]);
s2[m]=‘\0’;
}
Note: Refer Notes for examples of all the operations.
Master of Computer Applicati
30 Breaking Barriers and Building Futu
Search Techniques using array

 Linear & Binary Search



 Algorithm SequentialSearch(a[1..n],n,key)
 for i = 1 to n do
 if(a[i] = key)
 return i;
 end if
 return -1;
 end for


Master of Computer Applicati
31 Breaking Barriers and Building Futu
 Steps for Binary Search:
 [Initialize segment variables]
 Set low=LB
 high=UB
 mid:=int(low+high)/2
 repeat steps 3 & 4
 while (low<=high and Arr[mid]!=ITEM.)
 if Item<Arr[mid],then;
 Set high:=mid-1.
 else Set low=mid+1.
 Set mid=int(low+high)/2.
 if Arr[mid]=Item then:
 Set Loc:=mid.
 else
 Set Loc:=NULL.
 exit
Master of Computer Applicati
32 Breaking Barriers and Building Futu

Das könnte Ihnen auch gefallen