DS Module 1 - Upto - Sparse Matrix - pptx-1
DS Module 1 - Upto - Sparse Matrix - pptx-1
o Queues
o Linked Lists
o Linked Lists
o Trees
o Hashing
o Priority Queues
o Binary Search Trees
▪ It can be classified as
an integer.
int n = 10;
if(pi==NULL)
Or
if(!pi)
20 Data structures ,Module-1 8/30/2024
An example of using pointers to print the address and value is given
below.
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number; //stores the address of number variable
printf("Address stored in p variable is %x \n",p);
// p contains the address of the number therefore printing p gives the ad
dress of number.
printf("Address of var variable: %x\n", &number);
printf("Value of p variable is %d \n",*p); // As we know that * is used to derefe
rence a pointer, //so if
we print *p, we will get the value stored at the address contained by p.
return 0;
21} Data structures ,Module-1 8/30/2024
Pointers Can Be Dangerous
Because pointers provide access to a memory location and
because data and executable code exist in memory together,
misuses of pointers can lead to very subtle errors.
Uninitialized pointers
Uninitialized pointer pose a significant threat.
•the value stored in an uninitialized pointer could be
randomly pointing anywhere in memory.
•Storing a value using an uninitialized pointer has the
potential to overwrite anything in your program, including
your program itself
Dangling Pointers
Dangling pointers refer to a pointer which was pointing at an
object
22 that has been
Data structures deleted.
,Module-1 8/30/2024
Dynamic Memory
Allocationthe size of the array you declared may be
•Sometimes
insufficient.
allocation
functions
1. malloc(),
Data structures ,Module-1
2.
23
calloc(), 8/30/2024
mallo
•The name "malloc" stands for memory allocation.
c()
•The malloc() function reserves a block of memory of the specified number of
bytes. And, it returns a pointer of void which can be casted into pointers of any form.
Syntax of malloc()
ptr = (castType*) malloc(size);
Example
ptr = (float*) malloc(100 * sizeof(float));
The above statement allocates 400 bytes of memory. It's because the size
of float is 4 bytes. And, the pointer ptr holds the address of the first byte in
the allocated memory.
•The expression results in a NULL pointer if the memory cannot be
allocated.
Data structures ,Module-1 8/30/2024
24
##Program to demonstrate malloc and free
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,*pi;
float f,*pf;
pi=(int*)malloc(sizeof(int));
pf=(float*)malloc(sizeof(float));
*pi=1024;
*pf=3.14;
printf("an integer=%d, a float=%f \n",*pi,*pf);
free(pi);
free(pf);
25 } Data structures ,Module-1 8/30/2024
Sometimes malloc may fail for lack of sufficient memory,so
that we can check pointer is null.
if((pi= (int*)malloc(sizeof(int)))==NULL||(
pf=(float*)malloc(sizeof(float)))==NULL)
{
fprintf(stderr,”Insufficient memory”);
Exit(EXIT_FAILURE);
}
Or
if(!(pi= (int*)malloc(sizeof(int)))||!(
pf=(float*)malloc(sizeof(float))))
{
fprintf(stderr,”Insufficient memory”);
Exit(EXIT_FAILURE);
}
26 Data structures ,Module-1 8/30/2024
MACRO function for checking NULL pointer
#define MALLOC(p,s) \
if(!((p)=malloc(s))) { \
fprintf(stderr,”Insufficient memory”);\
Exit(EXIT_FAILURE);\
}
Now the two lines that invoke malloc may be replaced by the code
MALLOC(pi,sizeof(int));
MALLOC(pf,sizeof(float));
27 Data structures ,Module-1 8/30/2024
free()
Dynamically allocated memory created with calloc() or malloc() doesn't get
freed on their own. You must explicitly use free() to release the space.
Syntax of free()
free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
Syntax of calloc()
Example:
ptr = (float*) calloc(25, sizeof(float));
The above statement allocates contiguous space in memory for 25
elements of type float.
int *x;
x=calloc(n,sizof(int));
•If the user wishes allocate more numbers, again user has to
change the size of the array and recompile it.
•So to avoid this, During runtime allocate memory for the array.
Example:
int i,n,*list;
printf(“Enter the number of numbers to generate:”);
scanf(“%d”,&n);
if(n<1)
{
fprintf(stderr,”Improper value of n\n”);
Exit(EXIT_FAILURE);
}
MALLOC(list,n*sizeof(int));
35 Data structures ,Module-1 8/30/2024
Multidimensional
Arrays
•C programming language allows multidimensional arrays.
type name[size1][size2]...[sizeN];
type arrayName [ x ][ y ];
•Where type can be any valid C data type and arrayName will be a
valid C identifier.
Ex:int x[3][5];
• consider an example
return x;
}
#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
Output
Enter information:
Enter name: Ram
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: Ram
Roll number: 23
Marks: 34.5
list item1,item2,item3;
item1.data=’a’;
item2.data=’b’;
item3.data=’c’;
item1.link=item2.link=item3.link=NULL;
•Structures item1,item2 and item3 each contain the data item a,b
& c respectively, and the NULL pointer.
Item1.link=&item2;
Item2.link=&item3
,where each term has form axe, where x is the variable, a is the
bi xi Then
▪A(x)+B(x)= Σ (ai+bi)xi
▪A(x).B(x)= Σ (aixi . Σ(bjxj))
•The switch statement performs the comparisons and adds the proper term
to the new polynomial d.
•, if one polynomial becomes empty, we copy the remaining terms from the
from the non empty polynomial into d.
•To preserve space the alternative representation that uses only one global
array,terms,to store all our polynomials .
C Declaration is
int58avail=0;
Data structures ,Module-1 8/30/2024
Array Representation
•Consider the two polynomials A(x) =2 x 1000 + 1 and B(x) = X4 + 10x³ + 3x² + 1.
• These could be stored in the array termArray as shown-in Figure.
•StartA and StartB give the location of the first term of A and B respectively,
whereas FinishA and FinishB give the location of the last term of A and B.
•The static class member avail gives the location of the next free location in the
array termArray.
•Here, StartA = 0, Finish A= 1, StartB = 2, FinishB = 5, and avail = 6.
59 Data structures ,Module-1 8/30/2024
A(x) =2 x 1000 + 1
StartA = 0,
Finish A= 1,
StartB = 2,
FinishB = 5