1st Module
1st Module
Index
Module -1
Introduction
Teaching Hours: 08
CONTENTS
1. Introduction to Data Structures
1.1. Classification of Data Structures
1.2. Data structure Operations
1.3. Review on Pointers and Dynamic Memory Allocation
2. Arrays and Structures
2.1. Arrays, Dynamically Allocated Arrays
2.2. Structures and Unions, Polynomials, Sparse Matrices,
2.3. Representation of Multidimensional Arrays, Strings
3. Stacks
3.1 Stacks, Stacks Using Dynamic Arrays,
3.2 Evaluation and conversion of Expressions
The data types that are derived from primitive data type are called non-primitive data types .These
data types are used to store group of values. Non-primitive data type is also called derived data types
Eg: arrays, pointers, structures, unions, Stacks, Queue, Linked List etc.
The data appearing in our data structures are processed by means of certain operations
1. Traversing: Accessing each record exactly once so that certain items in the record maybe processed. (This accessing and
processing is sometimes called "visiting" the record).
2. Searching: Finding the location of the record with a given key value, or finding the locations of allrecords which satisfy
one or more conditions.
3. Inserting: Adding a new record to the structure.
4. Deleting: Removing a record from the structure.
1. POINTERS
Pointers is a variable which hold the address of another variable
Initialization of a pointer variable is the process of assigning the address of a variable to pointer variable
Eg: int a; /*interger variable a */
int *p; /*pointer variable p */
p=&a; /* address of a is assigned to p */
OR
int a; /*interger variable a */
int *p=&a; /* pointer variable p ,address of a is assigned to p* */
Allocation of memory during run time or execution time is called dynamic memory allocation (DMA)
malloc()
The name malloc stands for "memory allocation". The function malloc() reserves a s i n g l e block of memory
of specified size and return a pointer of type void which can be casted into pointer of any form. The prototype for this
function is available in <stdlib.h> and <alloc.h>
Syntax of malloc()
ptr=(cast-type*)malloc(byte-size)
Eg:ptr=(int*)malloc(100*sizeof(int));
This statement will allocate either 200bytes or 400bytes according to size of int 2 or 4 bytes respectively and the
pointer points to the address of first byte of memory.
calloc()
The name calloc stands for "contiguous allocation".
This function is used to allocate the memory during run time. The prototype for this function is available in
<stdlib.h> and <alloc.h>
The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas
calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
Syntax of calloc()
ptr=(cast-type*)calloc(n,element-size);
This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4
bytes.
realloc()
The name realloc stands for "reallocation".
If the previously allocated memory is insufficient or more than sufficient, then previously allocated memory size can
be changed by using realloc() function. The prototype for this function is available in <stdlib.h> and <alloc.h>
Syntax of realloc()
ptr=relloc(ptr,newsize);
Here ptr is pointer to a block of previously allocated memory either by using malloc() or calloc().
Newsize is new size of the block.
On Success it returns the address of first byte of allocated memory. On failure it return NULL
free()
This function is used to deallocate(or free) the allocated block of memory which is allocated dynamically by
using calloc() or malloc() or realloc().
syntax of free()
free(ptr);
A Single dimensional array is a linear list consisting of related data items of same type.In memory ,all the data items are
stored in continuous memory locations one after the other.
data_type array_name[size];
Here data_type specifies what kind of values an array can store such as int, float, char etc
array_name is the name to identify an array .
size specifies the maximum number of elements an array can store and it must be an integer(+ve).
Suppose, the starting address of age[0] is 2120 and the size of int be 2 bytes. Then, the next address (address of
a[1]) will be 2122, address of a[2] will be 2124 and so on.
Syntax:
data_type array_name[size]={v1,v2,…..vn};
Here data_type specifies what kind of values an array can store such as int, float, char etc
array_name is the name to identify an array.
size specifies the maximum number of elements an array can store and it must be an integer(+ve).
v1,v2,…..vn are the list of values enclosed within {} and separated by comma.
To access the 1st element a [0],2nd element a[1]……i.e using a[0] through
a[4] we can access 5 integers. In general, using a [0] through a[n-1] we can
#include <stdio.h>
denotes the row size and second subscript denotes the column size.
Here data_type specifies what kind of values an array can store such as int, float, char etc
array_name is the name to identify an array.
row size specifies the maximum number of rows in the array and column size specifies the maximum number of
columns in the array
Syntax:
data_type array_name[row size] [column size]={
{a1,a2,…..,an},
{b1,b2,…..,bn},
.
.
.
{z1,z2,…..,zn},
};
OR
data_type array_name[row size] [column size]={a1,a2,…..,an,b1,b2,…..,bn,z1,z2,…..,zn};
Here data_type specifies what kind of values an array can store such as int, float, char etc
array_name is the name to identify an array.
row size specifies the maximum number of rows in the array and
column size specifies the maximum number of columns in the array
Eg: 1) int a[4][3]={
{2,4,6},
{10,20,30},
{5,10,15},
{12,14,16}
};
To access the 1st element ie 1st row and 1st column a [0][0], to access the 2nd element ie 1st row and 2st column a[0][1]
and so on…. In general, to access the element in mth row and nth column a[m] [n], where m is the row number and n is the
column number.
To read and write all the elements in two dimensional array,2 for loops are required (nested for loops).Outer loop gives
the number of rows and inner loop gives the number of columns.
Dept. of ISE, DBIT Page
9
BCS304
for(i=0;i<m;i++) for(i=0;i<m-1;i++)
{ {
for(j=0;j<n;j++) for(j=0;j<=n-1;j++)
{ OR {
scanf(“%d”a[i][j]); scanf(“%d”a[i][j]);
} }
} }
for(i=0;i<m;i++) for(i=0;i<m-1;i++)
{ {
for(j=0;j<n;j++) for(j=0;j<=n-1;j++)
{ OR {
printf(“%d”a[i][j]); printf(“%d”a[i][j]);
} }
printf(“\n”);
printf(“\n”); }
}