0% found this document useful (0 votes)
3 views

Module 3

Ds notes

Uploaded by

pintonsebastian1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 3

Ds notes

Uploaded by

pintonsebastian1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MODULE -3

1. What is dynamic memory allocation? List any two advantages of dynamic


memory allocation.
Dynamic memory allocation is a process that allocates memory for data structures and
variables when a program requests it during runtime.
Advantages
1. Efficiency:-Memory is only allocated when it's needed, which reduces memory
wastage.
2. Flexibility:-The size and location of memory blocks can be changed based on the
program's logic and data size. This allows for the creation of data structures that can
adjust to different input sizes.
3. Improved performance:-Dynamic memory allocation can optimize memory usage,
which can improve overall performance.
4. Adaptable data structures:-Dynamic allocation makes it easy to create data
structures like linked lists and dynamic arrays.
5. Control over memory usage:-Programs can maintain control over memory usage
throughout their execution.
Write any 2 advantage

2. Write an algorithm to count number of nodes in a singly linked list.


Algorithm
1. Initialize a variable count to 0.
2. Start at the head of the linked list.
3. Traverse the linked list using a loop.
4. For each node visited, increment the count by 1.
5. Continue this process until reaching the end of the list (i.e., when the next pointer of the
current node is NULL).
6. Return the value of count as the total number of nodes in the linked list.
3. Explain the advantages and disadvantages of First-fit and Best-fit memory
allocation schemes.
Advantages of First-Fit Allocation :
 Simple and efficient search algorithm
 Minimizes memory fragmentation
 Fast allocation of memory
Disadvantages of First-Fit Allocation:
 Poor performance in highly fragmented memory
 May lead to poor memory utilization
 May allocate larger blocks of memory than required.
Advantages of Best –Fit Allocation:
 It allocates space optimally.
 Space wastage is very less or negligible
 Every Time it finds the appropriate hole to allocate file
Disadvantage of est- Fit Allocation:
 It is not fast
 It takes time because it requires sorting and searching of the appropriate holes
 Algorithm is somewhat complicated
 It is difficult to implement
4. Write an algorithm to delete a given node in a singly linked list.
 If list is empty (head == NULL), returns the head.
 If the position to delete is 1 (the head node):
o Update head = temp->next
 Traverse the list until reaching the desired position:
o Initialize prev to keep track of the previous node.
o Move temp through the list until the position is reached.
 Check for Valid Position:
o If temp becomes NULL, it means the position exceeds the number of
nodes in the list. Print a message and return the head.
 If the node to delete is found:
o Set prev->next to temp->next, effectively skipping over the node to
be deleted.

5. Explain Worst-fit allocation with an example


In this allocation technique, the process traverses the whole memory and always search
for the largest hole/partition, and then the process is placed in that hole/partition. It is a
slow process because it has to traverse the entire memory to search the largest hole.
6. Compare a Singly Linked List and Doubly Linked List.

Write any 3 difference for 3 Marks


7. Write an algorithm/pseudocode to delete a node at the end of a doubly linked
list.

8. Illustrate how the polynomial 4x3+11 can be represented using an array and a linked
list.
9. Given a pointer HEAD that points to the first element of a singly linked list, a value
ITEM to be stored in the linked list and a value A after which ITEM is to be inserted,
write an algorithm to insert ITEM to the list.
 Traverse the linked list to find the given node.
 If the given node is not found, print “Node not found”.
 Else if the given node is found, create a new node, say new_node nad insert ITEM into
the datafield
 Make the next pointer of new_node as next pointer of A node.
 Update the next pointer of A node point to the new_node.

10. Write an algorithm to perform backward traversal of a doubly linked list.

Algorithm
 Recursively traverse the doubly linked list until it reaches the last node.
 While backtracking, reverse the links between the nodes. To reverse the links:
o Change the next pointer of the current node to point to its previous node.
o Change the prev pointer of the current node to point to its next node.
 Adjust the head of the doubly linked list to point to the last node.

11. Write an algorithm to insert a node after a given node in a doubly linked list.

 Find the given node in the linked list, say curr.


 Once we find it, create a new node say new_node with the given input data.
 Set the new node’s previous pointer to given node, new_node->prev = curr.
 Set the new node’s next pointer to the given node’s next, new_node->next = curr->next.
 Then, we update the next pointer of given node with new node, curr->next = new_node.
 Also, if the new node is not the last node of the linked list, then update previous pointer of
new node’s next node to new node, new_node->next->prev = new_node.

12. What are the applications of a linked list?


 Dynamic Memory Allocation: Managing memory in operating systems.
 Data Structures: Implementation of stacks, queues, and graphs.
 Real-Time Applications: Navigation systems, like undo/redo operations in
editors.
 File Systems: Directory structures in operating systems.
 Hashing: Handling collisions in hash tables.
 Polynomial Representation: For operations on polynomials
Write any 3 applcations
13. Compare a linked list and an array implementation of a general list.

16. Let LIST be a singly linked list in memory. Write an algorithm to find number of times
a given data item called ITEM occurs in LIST.
1. Initialize a variable count=0
2. Then keep tracing the list from start till end and compare each element with the
given value ITEM. If it matches then increment count by 1.
3. Keep the linear search going until end of list is reached.
4. Print the occurrence of ITEM as count
17. Let take initial memory as -

Do the following things with first fit approach and show the memory status:
1. Allocate process C of size 90K
2. Allocate process D of size 70K

18. How a linked list can be used to represent a polynomial 5x3+4x2+3x+2? Give an
algorithm to perform addition of two polynomials using linked list.

Linked list are widely used to represent and manipulate polynomials. Polynomials are the
expressions containing number of terms with nonzero coefficient and exponents.In the linked
representation of polynomials, each term is considered as a node. And such a node contains
three
fields
 Coefficient field
 Exponent field
 Link field
The coefficient field holds the value of the coefficient of a term and the exponent field contains
the exponent value of the term. And the link field contains the address of the next term in the
polynomial. The polynomial node structure is
19. Write an algorithm to insert a node in the beginning and end of a doubly linked list.
Demonstrate with an example
20. How a stack can be implemented using linked list?
PUSH Operation
Adding a node to the stack is referred to as push operation. Pushing an element to a stack in
linked list implementation is different from that of an array implementation.

1. Create a node first and allocate memory to it.


2. If the list is empty then the item is to be pushed as the start node of the list. This includes
assigning value to the data part of the node and assign null to the address part of the
node.
3. If there are some nodes in the list already, then we have to add the new element in the
beginning of the list (to not violate the property of the stack). For this purpose, assign
the address of the starting element to the address field of the new node and make the new
node, the starting node of the list.
POP Operation

1. Check if the stack is empty. If it is, there is nothing to remove.

2. Store the data of the top node in a temporary variable for future use.

3. Update the top pointer to point to the next node in the stack.

4. Free the memory occupied by the previous top node.

5. Return the stored data.

You might also like