Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Name: Manav Verma

Roll Number: R2142211002


SAP ID: 500091766

DATA STRUCTURE ASSIGNMENT - 1


Q1) What is dynamic memory allocation?

The concept of dynamic memory allocation in c language enables the C


programmer to allocate memory at runtime. Dynamic memory allocation uses
the heap space of the memory. It can be increased while executing the
program. Dynamic Memory Allocation is used in linked list.

Dynamic memory allocation in c language is possible by 4 functions of stdlib.h


header file.

 malloc(): Allocates single block of requested memory.


The syntax of malloc() function:
Ptr = (cast-type *) malloc (byte - size)

 calloc(): allocates multiple block of requested memory.

The syntax of calloc() function:

Ptr = (cast-type * ) calloc (number, byte-size)

 realloc() : reallocates the memory occupied by malloc() or calloc()


functions.

The syntax of realloc() function :

ptr=realloc (ptr, new-size)

 free() : frees the dynamically allocated memory.

the syntax of free() function :


free(ptr)
Q2) DIFFERENCE BETWEEN STRUCTURES AND UNIONS?

Parameter Structure Union

Keyword A user can deploy the A user can deploy the


keyword struct to define a keyword union to define a Union.
Structure.

Internal The implementation of Structure In the case of a Union, the memory


Implementation in C occurs internally- because it allocation occurs for only one
contains separate memory member with the largest size
locations allotted to every input among all the input variables. It
member. shares the same location among all
these members/objects.

Accessing A user can access individual A user can access only one member
Members members at a given time. at a given time.

Syntax The Syntax of declaring a The Syntax of declaring a Union in


Structure in C is: C is:
struct [structure name] union [union name]
{ {
type element_1; type element_1;
type element_2; type element_2;
. .
. .
} variable_1, variable_2, …; } variable_1, variable_2, …;

Size A Structure does not have a A Union does not have a separate
shared location for all of its location for every member in it. It
members. It makes the size of a makes its size equal to the size of
Structure to be greater than or the largest member among all the
equal to the sum of the size of its data members.
data members.

Value Altering Altering the values of a single When you alter the values of a
member does not affect the other single member, it affects the values
members of a Structure. of other members.

Storage of Value In the case of a Structure, there is In the case of a Union, there is an
a specific memory location for allocation of only one shared
every input data member. Thus, it memory for all the input data
can store multiple values of the members. Thus, it stores one value
various members. at a time for all of its members.
Initialization In the case of a Structure, a user In the case of a Union, a user can
can initialize multiple members only initiate the first member at a
at the same time. time.

Q3) DESIGN A STRUCTURE ‘PRODUCT’ TO STORE THE DETAILS OF


THEPRODUCT PURCHASED LIKE PRODUCT NAME, PRICE, PER UNIT,
NUMBER OF QUANTITIES PURCHASED, AND AMOUNT SPENT. GETTHE
NAME, PRICE PER UNIT, AND NUMBER OF QUANTITIES OF THEPRODUCT
PURCHASED. CALCULATE THE AMOUNT SPENT ONPRODUCT AND THEN
DISPLAY ALL THE DETAILS OF THE PROCUREDPRODUCT USING
STRUCTURE POINTERS
Q4) WHAT ARE PRIORITY QUEUES AND IMPLEMENT PRIORITY
QUEUE WITH EXAMPLE INSERTION, DELETION AND DISPLAY?

A priority queue in C++ is a derived class in STL(standard template


library) that processes only the highest priority element. It means that the
first element of the queue is greatest from the rest of the elements of the
queue. Later all the elements are arranged in non-increasing order, i.e., you
can see each element of the queue has fixed priority.

Priority queue can be implemented using an array, a linked list, a heap data
structure, or a binary search tree. Among these data structures, heap data
structure provides an efficient implementation of priority queues.

The queue data structure follows the FIFO strategy, i.e., first in, first out,
whereas the priority queue only checks the priority of the elements for
retrieving the elements. Priority queues are implemented as container
adapters. It is a class that uses an encapsulated object of a specific container
class and deliver a specific set of member function to access the queue
elements.

The syntax of the priority queue:-


priority_queue<int> variableName;
Inserting an Element into the Priority Queue

Inserting an element into a priority queue (max-heap) is done by the following steps.

 Insert the new element at the end of the tree.

 Insert an element at the end of the queue

Heapify after insertion

Deleting an Element from the Priority Queue

Deleting an element from a priority queue (max-heap) is done as follows:

 Select the element to be deleted.

 Select the element to be deleted


 Swap it with the last element.

 Swap with the last leaf node element

 Remove the last element.

 Remove the last element leaf

 Heapify the tree.

 Heapify the priority queue

Peeking from the Priority Queue (Find max/min)

Peek operation returns the maximum element from Max Heap or minimum element from Min

Heap without deleting the node.


Q5) WHAT IS GARBAGE COLLECTION AND COMPACTION?
Garbage Collection: In computer science, garbage collection is a type of memory
management. It automatically cleans up unused objects and pointers in memory,
allowing the resources to be used again. Garbage collection may also be done at
compile-time, when a program's source code is compiled into an executable
program.

Subsequently, question is, what is garbage collection in Python? The process by


which Python periodically frees and reclaims blocks of memory that no longer
are in use is called Garbage Collection. Python's garbage collector runs during
program execution and is triggered when an object's reference count reaches
zero.

Waste compaction is the process of compacting waste, reducing it in


size. Garbage compactors and waste collection vehicles compress waste so that
more of it can be stored in the same space. Waste is compacted again, more
thoroughly, at the landfill to conserve valuable airspace and to extend the
landfill's life span

Q6) WHAT IS DOUBLE ENDED QUEUE AND IMPLEMENT THEM


ALONG WITH INSERTION, DELETION AND DISPLAY?
The deque stands for Double Ended Queue. Deque is a linear data structure
where the insertion and deletion operations are performed from both ends. We
can say that deque is a generalized version of the queue.

Though the insertion and deletion in a deque can be performed on both ends, it
does not follow the FIFO rule. The representation of a deque is given as follows -

Types of deque

There are two types of deque -

o Input restricted queue


o Output restricted queue
Input restricted Queue

In input restricted queue, insertion operation can be performed at only one end,
while deletion can be performed from both ends.

Output restricted Queue

In output restricted queue, deletion operation can be performed at only one end,
while insertion can be performed from both ends.
Q7) WHAT IS OVERFLOW AND UNDERFLOW AND SHOW HOW IT
WORKS IN THE LINKED LIST?
Overflow is the situation where you try to store a number that exceeds the value
range for the data type. When you try to store too large of a positive or negative
number, the binary representation of the number (remember that all values are
stored as a 0 and 1 pattern) is corrupted and you get a meaningless or erroneous
result.
Underflow occurs in floating point numbers and is the situation where in
numbers very close to zero, there are not enough significant digits to represent
the number exactly.
I have a created an example of an overflow and an underflow. Through these
examples, I am going to jump ahead and introduce something called macro
constants. These will help us explore overflows and underflows.
Macro constants
A macro constant is a name for a constant, typically written in all capitals, that is
created as a pre-processor directive. Since these are pre-processor directives,
before the compiler builds the program, the macro constant will be replaced with
an actual value. You can think of the macro constants as aliases or place holders
for the real values. We will discuss how you can create your own macro constants
in the next section.

The output
Q8) IMPLEMENT INFIX AND POSTFIX EXPRESSION.

Algorithm for converting an infix expression into postfix operation

Add "("at the beginning and ")" at the end of an


infix expression Q.
2. Scan Q from left to right and repeat
Step 3 to step 6.
3. If an operand is encountered, add it into postfix P.
4. If a left parenthesis is encountered,
push it onto the stack S
5. If an operator op is encountered, then,
(a) Repeatedly pop from Stack S and add
it to postfix each operator which has
same precedence as or higher priority than op.
(b) Add op to Stack S.
6. If the right parenthesis is encountered, then
(a) Repeatedly pop from Stack S and add
it to postfix each operator until left parenthesis is encountered on stacks.
(b) Remove the left parenthesis.
Q9) CONVERT INFIX TO POSTFIX USING STACK AND ARRAY
OUTPUT:
Q10) EXPLAIN MERGE SORT WITH ITERATIVE.
In Iterative merge sort, we implement merge sort in a bottom-up manner. This is
how it works:

1. We start by sorting all sub-arrays of length 1


2. Then, we sort all sub-arrays of length 2 by merging length-1 sub-arrays
3. Then, we sort all sub-arrays of length 4 by merging length-2 sub-arrays
4. We repeat the above step for sub-arrays of lengths 8, 16, 32, and so on
until the whole array is sorted.
Q11) EXPLAIN LINEAR AND BINARY SEARCH WITH EXAMPLE AND
CODE.
Linear search, also called as sequential search, is a very simple method used for
searching an array for a particular value. It works by comparing the value to
be searched with every element of the array one by one in a sequence until a
match is found. Complexity of Linear Search Algorithm: Linear search executes in
O(n) time where n is the number of elements in the array.
Binary search is a searching algorithm that works efficiently with a sorted
list. The mechanism of binary search can be better understood by an
analogy of a telephone directory. When we are searching for a particular name
in a directory, we first open the directory from the middle and then
decide whether to look for the name in the first part of the directory or in
the second part of the directory. Again, we open some page in the middle and
the whole process is repeated until we finally find the right name.

OUTPUT:

You might also like