Illustrate A C Function To Add Two Polynomials. Show The Linked List Representation of Two Polynomials and Its Addition
Illustrate A C Function To Add Two Polynomials. Show The Linked List Representation of Two Polynomials and Its Addition
1 Illustrate a c function to add two polynomials. Show the linked list representation
of two polynomials and its addition.
Each polynomial term is represented as a node in a linked list containing:
1. Coefficient
2. Exponent
3. Pointer to the next node
Example:
• Polynomial (x) = 3x^3 + 5x^2 + 2:
Head → (3,3) → (5,2) → (2,0)
• Polynomial (x) = 4x^3 + 2x + 7:
Head → (4,3) → (2,1) → (7,0)
return result;
}
Structure of a Node
struct Node {
int data; // Data value of the node
struct Node* next; // Pointer to the next node
struct Node* prev; // Pointer to the previous node
};
typedef struct Node* NODE; // Defining NODE as a pointer to struct Node
C Function to Delete a Node from a Circular Doubly Linked List
NODE deleteNode(NODE head, int key) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}
free(cur);
printf("Node with data %d deleted\n", key);
return head;
}
cur = cur->next;
} while (cur != head);
Explanation
1. Structure:
o The Node structure contains data and two pointers, next (to the next node) and prev (to the previous
node).
2. Input Parameters:
o head: Pointer to the head node of the circular doubly linked list.
o key: The data value of the node to delete.
3. Steps in the Function:
o If the list is empty (head == NULL), return with a message.
o Traverse the list to find the node with the given key.
o If the node is found:
▪ If it’s the only node, free it and return NULL.
▪ Otherwise, update the next pointer of the previous node and the prev pointer of the next node.
▪ Update the head if the deleted node is the current head.
o If the key is not found, return the unchanged list with a message.
4. Special Cases:
o List is empty.
o List has only one node.
o Node to delete is the head.
4 Define a tree? With the suitable example, define:
a) Binary tree b) Degree of binary tree c) Level of binary tree d) Complete binary tree
A tree is a hierarchical, non-linear data structure where elements are organized in the form of nodes connected by
edges. It is defined as:
Binary Tree
A binary tree is a tree where each node has at most two children, often referred to as the left child and right
child.
A
/ \
B C
/ \ / \
D E F G
This content aligns with the details from your uploaded PDF. Let me know if further elaboration is needed on any
topic!
This code snippet demonstrates how to insert a new node as the left child in a threaded binary tree.
Explanation:
• Step 1: We check if the left child of node s is null. If it is, we can directly insert r as the left child of s.
• Step 2: Set the left child of r to the current left child of s, and set r's leftThread to the previous
leftThread of s.
• Step 3: Set r as the left child of s, and update s's leftThread to FALSE to indicate that r is now a real
child (not a thread).
• Step 4: If the left child of r was not a thread, we find its in-order successor using the insucc function and
update the successor's right child to point to r.
This code snippet demonstrates how to insert a new node as the right child in a threaded binary tree.
Explanation:
This code snippet demonstrates how to insert a new node as the root of a threaded binary tree.
// Update the header node's left child to point to the new root
(*root)->leftChild = r;
(*root)->leftThread = FALSE;
Explanation:
• Step 1: When inserting a new node as the root, we set the new root r to inherit the left and right child, and
their corresponding threads from the old root.
• Step 2: Update the header node (pointed by root) to point to r as the left child, and set leftThread to
FALSE to indicate that r is now the real root (not a thread).
• Step 3: If r's right child is not a thread, we update the in-order successor of r to point to r as the
predecessor.
These methods ensure that the threaded binary tree remains properly structured, with all the necessary threads set
to allow for efficient in-order traversal without requiring additional data structures like stacks.
10. Discuss collision? What are the methods to resolve collision? Explain linear probing with an example
Collision in Hashing:
A collision occurs in a hash table when two different keys are hashed to the same location. In other words, when
multiple keys map to the same index (or bucket) in the hash table, a collision happens. This can lead to
inefficiencies in both the search and insertion processes.
There are several methods to handle collisions in a hash table. The most common ones are:
Linear probing resolves collisions by finding the next available slot in the table after a collision. If the hash
function H(k) = k % m results in a collision, the next available slot is checked at H(k) + 1, H(k) + 2, and so on,
wrapping around the table if necessary.
Example:
Let's use linear probing to insert keys into a hash table of size 10 with the hash function H(k) = k % 10. We want
to insert the following keys: 72, 27, 36, 24, 63, 81, 92, 101.
Step-by-step insertion:
Index: 0 1 2 3 4 5 6 7 8 9
Values: - 81 72 63 24 92 36 27 101 -
Key Summary:
• The average search length is calculated as the average number of probes needed to insert all keys, which is
1+1+1+1+4+1+1+88=2.25\frac{1+1+1+1+4+1+1+8}{8} = 2.25.
This shows how linear probing helps to resolve collisions by systematically searching for the next available
position in the hash table.
11. Describe hashing? Explain any three hash functions.
Hashing
Hashing is a technique used to map data of arbitrary size to fixed-size values, called hash values, using a hash
function. The hash value serves as an index to store and retrieve data efficiently in a hash table. The primary goal
of hashing is to minimize collisions (when two keys map to the same index) and ensure a uniform distribution of
data across the hash table.
1. Division Method
o Explanation: This method divides the key xx by the size of the hash table MM and uses the
remainder as the hash value. h(x)=xmod Mh(x) = x \mod M
o Advantages:
▪ Simple and fast.
▪ Works well if MM (table size) is chosen as a prime number to reduce clustering.
o Example:
Key = 25, Table Size M=7M = 7: h(25)=25mod 7=4h(25) = 25 \mod 7 = 4 Key 25 is mapped to
index 4.
2. Mid-Square Method
o Explanation: The key is squared, and specific middle digits are extracted as the hash value.
▪ This reduces the chance of clustering since the square operation spreads the values more
uniformly.
o Steps:
1. Square the key KK.
2. Extract the middle ll digits from the result.
o Example:
Key = 1234: K2=1522756⇒H(1234)=27 (middle two digits).K^2 = 1522756 \quad \Rightarrow
\quad H(1234) = 27 \; \text{(middle two digits)}.
3. Folding Method
o Explanation: This method splits the key into equal parts, adds them together, and ignores the carry,
if any.
o Steps:
1. Divide the key into parts (e.g., 123456 into 123, 456).
2. Add the parts together.
3. Use the sum, modulo the table size, as the hash value.
o Example:
Key = 123456, Table Size = 100:
▪ Divide: 123,456123, 456
▪ Add: 123+456=579123 + 456 = 579
▪ Hash Value: 579mod 100=79579 \mod 100 = 79.
These functions ensure uniform distribution and help avoid clustering, which is crucial for efficient hash table
performance.