Module 3
Module 3
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.
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.
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.
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.