DSA_Module 2
DSA_Module 2
As can be seen, the length (size) of the array above is 9. But what if there is a
requirement to change this length (size)? For example,
If there is a situation where only 5 elements are needed to be entered in this array.
In this case, the remaining 4 indices are just wasting memory in this array. So there
is a requirement to lessen the length (size) of the array from 9 to 5.
Take another situation. In this, there is an array of 9 elements with all 9 indices filled.
But there is a need to enter 3 more elements in this array. In this case, 3 indices more
are required. So the length (size) of the array needs to be changed from 9 to 12.
C malloc() method
The “malloc” or “memory allocation” method in C is used to dynamically allocate a
single large block of memory with the specified size. It returns a pointer of type void
which can be cast into a pointer of any form. It doesn’t Initialize memory at execution
time so that it has initialized each block with the default garbage value initially.
Syntax of malloc() in C
ptr = (cast-type*) malloc(byte-size);
For Example:
C calloc() method
1. “calloc” or “contiguous allocation” method in C is used to dynamically allocate the
specified number of blocks of memory of the specified type. it is very much similar
to malloc() but has two different points and these are:
2. It initializes each block with a default value ‘0’.
3. It has two parameters or arguments as compare to malloc().
Syntax of calloc() in C
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of each element.
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size
of the float.
C free() method
“free” method in C is used to dynamically de-allocate the memory. The memory
allocated using functions malloc() and calloc() is not de-allocated on their own. Hence
the free() method is used, whenever the dynamic memory allocation takes place. It
helps to reduce wastage of memory by freeing it.
Notes By- Prof. Ankita Mandore, CSE (Data Science), DSCE.
Syntax of free() in C
free(ptr);
C realloc() method
“realloc” or “re-allocation” method in C is used to dynamically change the memory
allocation of a previously allocated memory. In other words, if the memory previously
allocated with the help of malloc or calloc is insufficient, realloc can be used
to dynamically re-allocate memory. re-allocation of memory maintains the already
present value and new blocks will be initialized with the default garbage value.
Syntax of realloc() in C
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
A Queue Data Structure is a fundamental concept in computer science used for storing and
managing data in a specific order. It follows the principle of “First in, First out” (FIFO), where
the first element added to the queue is the first one to be removed. Queues are commonly used in
various algorithms and applications for their simplicity and efficiency in managing data flow.
A queue can be defined as an ordered list which enables insert operations to be performed at one
end called REAR and delete operations to be performed at another end called FRONT.
A linear data structure that follows A linear data structure that follows
Definition the Last In First Out the First In First Out
(LIFO) principle. (FIFO) principle.
Elements are added and removed from Elements are added at the rear and
Insertion/Removal
the same end (the top). removed from the front.
Real-World A stack of plates: you add and remove A queue at a ticket counter: the first
Analogy plates from the top. person in line is the first to be served.
Circular Queue
A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out)
principle except that the last position is connected to the first position in a circular queue that
forms a circle. It is also known as a Ring Buffer.
o If rear != max - 1, then rear will be incremented to mod(maxsize) and the new value will
be inserted at the rear end of the queue.
o If front != 0 and rear = max - 1, it means that queue is not full, then set the value of rear
to 0 and insert the new element there.
Types of deque
There are two types of deque -
1. Input restricted queue
2. Output restricted queue
Linked Lists
A linked list, or one-way list, is a linear collection of data elements, called nodes, where the
linear order is given by means of pointers. That is, each node is divided into two parts:
The first part contains the information of the element, and
The second part, called the link field or nextpointer field, contains the address of the
next node in the list
The last node of the list contains pointer to the null.
A pointer variable called START or FIRST which contains the address of the first node. A
special case is the list that has no nodes, such a list is called the null list or empty list and is
denoted by the null pointer in the variable START.
A circular linked list is a special type of linked list where all the nodes are connected to form a
circle. Unlike a regular linked list, which ends with a node pointing to NULL, the last node in a
circular linked list points back to the first node. This means that you can keep traversing the list
without ever reaching a NULL value.