DS Module 1 - Upto - Sparse Matrix Vtu 3RD SEMESTER Bcs 304
DS Module 1 - Upto - Sparse Matrix Vtu 3RD SEMESTER Bcs 304
DS Module 1 - Upto - Sparse Matrix Vtu 3RD SEMESTER Bcs 304
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 11/06/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 addr
ess 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 dereference 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 11/06/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 11/06/2024
Dynamic Memory Allocation
insufficient.
allocation
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.
•24 Data structures
The expression ,Module-1
results in a NULL pointer if the memory cannot 11/06/2024
be allocated.
##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 11/06/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 11/06/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 11/06/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);
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 11/06/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;
}
41 Data structures ,Module-1 11/06/2024
The C library function void *realloc(void *ptr, size_t
size) attempts to resize the memory block pointed to
by ptr that was previously allocated with a call
to malloc or calloc.
Declaration
void *realloc(void *ptr, size_t size)Parameters
•ptr − This is the pointer to a memory block previously
allocated with malloc, calloc or realloc to be reallocated. If this
is NULL, a new block is allocated and a pointer to it is returned
by the function.
•size − This is the new size for the memory block, in bytes. If it
is 0 and ptr points to an existing block of memory, the memory
block pointed by ptr is deallocated and a NULL pointer is
returned.
42 Data structures ,Module-1 11/06/2024
•This function returns a pointer to the newly allocated memory,
Structures and Unions
person2, p[20];
45 return,Module-1
Data structures 0; 11/06/2024
}
Access members of a structure
#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;
Item1.link=&item2;
Item2.link=&item3
terms. ,where each term has form axe, where x is the variable, a
is the coefficient and e is the exponent
•. The largest exponent is the polynomial’s degree.
•The algorithm works by comparing terms from the two polynomials until
one or both of the polynomials becomes empty.
•The switch statement performs the comparisons and adds the proper
term to the new polynomial 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 11/06/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
Data structures A= 1, StartB = 2, FinishB = 5, and avail = 6.
,Module-1 11/06/2024
59
A(x) =2 x 1000
+1
B(x)=X4 + 10x³ +
3x² + 1
StartA = 0,
Finish A= 1,
StartB = 2,
FinishB = 5