Introduction To DS - MCH

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

INTRODUCTION TO DATA

STRUCTURES

Dr. Maumita Chakraborty


University of Engineering and Management Kolkata
WHAT IS DATA STRUCTURE?
 Arrangement of data, either in computer’s memory
or disk storage.
 Algorithms are used to insert new data, append
and/or delete existing data in data structures.
 Not only storage, but retrieval of data also is
different for different data structures.
 Types of data structures:

 Linear: E.g. Arrays, Linked Lists, Stacks,


Queues.
 Non-linear: E.g. Trees, Graphs.
ABSTRACT DATA TYPE
 Its behavior is defined by a set of values and a set
of operations.
 It is called “abstract” because it gives an
implementation independent view.
 A black box which hides the inner structure and
design of the data type.
 E.g.: List ADT, Stack ADT, Queue ADT.
REVISING C
POINTERS
 Stores the memory address of another value
located in computer memory.
 A pointer references a location in memory.
 Dereferencing pointer: obtaining the value stored at
that location.
 int a = 5;
 int *ptr = NULL;
 ptr = &a; //assigning address
 print *ptr; //dereferencing
 int **k; //double pointer
 k=&ptr;
 print **k;
POINTERS AND ARRAYS
 int a[5]={8, 6, 4, 9, 11}
 What information do you get from the above
statement?
 int *x;

 x=&a[0];

 x=??

 x=a;

 x=??

 x+1=??

 *x=??

 *(x+1)=??
FUNCTIONS
 Program often broken up into segments known as
functions.
 Takes in inputs, processes it, and then outputs the
result.
 Three intrinsic features of functions:
 Function Call: A function f may use the output from
another function, g. f is the calling function, g is the
called function.
 Function Declaration/Prototype: Identifies a function
with its name, the data types of the arguments passed
and the data type of the output.
 Function Definition: Body of the function containing the
executable code for that function.
AN EXAMPLE (IN C)
 #include<stdio.h>
 int sum_digit(int number); //function prototype
 main()
 {
 int p, s;
 scanf(“%d”,&p);
 s=sum_digits(p); //function call
 printf(“Sum of digits of %d is %d\n”, p, s);
 }
 int sum_digits(int n) //function definition
 {
 int sum=0, digit;
 if(n<0) n=-n;
 while(n!=0)
 {
 digit=n%10;
 sum +=digit;
 n=n/10;
 }
 return sum;
 }
POINTERS AND FUNCTIONS

 interchange (int x, int y)  interchange (int *x, int *y)


 {  {
 int temp;  int temp;
 temp=x;  temp=*x;
 x=y;  *x=*y;
 y=temp;  *y=temp;
 }  }

What is the difference between the above two functions?


STRUCTURES
 User defined data type, used to group items of
possibly different types into a single type.
 struct address
typedef struct
{ {
char name[50]; char name[50];
char street[100]; char street[100];
char city[50];
char city[50];
char state[20];
char state[20]; int pin;
int pin; } address;
};
STRUCTURES AND ARRAYS
typedef struct typedef struct
item_in_store item_in_store
{ {
 int item_code;  int item_code;
 int qty_in_stock;  char item_name[10];
 float price;  int qty_in_stock;
 }item;  float price;
 Item inventory [100];  }item;
 Find out the 2nd  Item *inventory;
inventory’s price.  Find out the 2nd letter of
the inventory’s name.
THANK YOU

You might also like