Harshit SLLAssignment
Harshit SLLAssignment
Q1. Form a list containing the UNION of the elements of two lists (singly
linked list).
def union(list1, list2):
result = set(list1) for
item in list2:
result.add(item)
return list(result)
Q2. Form a list containing the INTERSECTION of the elements of two lists
(singly linked list).
def intersection(list1, list2):
set1 = set(list1) set2 =
set(list2) return
list(set1.intersection(set2))
Q3. Insert an element after the nth element of a list (singly linked list).
class Node: def
__init__(self, data):
self.data = data
self.next = None
Q4. Place the elements of a list in increasing order (singly linked list).
Assignment
def sort_linked_list(head):
values = []
current = head
while current:
values.append(current.data)
current = current.next
values.sort() current = head
for value in values:
current.data = value current
= current.next
def sum_doubly_linked_list(head):
total = 0
current = head
while current:
total += current.data
current = current.next return
total
Q8. Move node (p) forward n positions in the list (circular doubly linked
list).
def move_node_forward(head, node, n):
current = head while current
and current != node:
current = current.next
if not current:
return for _
in range(n):
current = current.next node.data,
current.data = current.data, node.data
Q9. Recursive function to find the number of nodes in a linked list (singly
linked list).
def count_nodes_recursive(node):
if not node: return 0
return 1 + count_nodes_recursive(node.next)
def create_polynomial(coefficients):
head = None for coef, x, y,
z in coefficients:
new_node = PolynomialNode(coef, x, y, z)
if not head:
head = new_node
head.next = head else:
temp = head
while temp.next != head:
temp = temp.next
temp.next = new_node
new_node.next = head return
head
Q15. Take the PARTIAL DERIVATIVE of polynomial with respect to any of its
variables (doubly linked list).
#include <stdio.h>
#include <stdlib.h>
Assignment
typedef struct Node {
int coeff, x, y, z;
} Node;
newNode;
Node* temp = head; Node* result = NULL; while (temp) { if ((var == 'x' && temp->x
> 0) || (var == 'y' && temp->y > 0) || (var == 'z' && temp->z > 0)) { int coeff = temp-
if (var == 'y') coeff *= y, y--; if (var == 'z') coeff *= z, z--; result = insert(result,
coeff, x, y, z);
temp = temp->next;
}
return result;
Assignment
}
head = head->next;
printf("\n");
int main() {
= insert(poly, 4, 0, 1, 2);
display(deriv);
return 0;
#include <stdlib.h>
Node* next;
} Node;
if (!head) {
newNode->next = newNode;
return newNode;
head;
*remainder = dividend;
Assignment
while (*remainder && (*remainder)->exp >= divisor->exp) {
(tempRem) {
if (tempSub->exp == tempRem->exp) {
found = 1; break;
tempSub = tempSub->next;
}
Assignment
*remainder = newRem;
if (!(*remainder)) break;
return quotient;
if (!head) {
printf("0\n"); return;
printf("+ ");
temp = temp->next;
printf("\n");
int main() {
= insert(divisor, 1, 0);
printf("Dividend: ");
display(dividend); printf("Divisor:
"); display(divisor);
printf("Remainder: ");
display(remainder);
return 0;