Assignment
Roll no: 241030093
Name: Harshit Choudhary
Batch: 24A18
1. Form a list containing the UNION of the elements of two
lists (singly linked list).
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
void appendNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
temp->next = newNode;
int isPresent(struct Node* head, int data) {
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == data) return 1;
temp = temp->next;
return 0;
struct Node* unionLists(struct Node* head1, struct Node* head2) {
struct Node* result = NULL;
struct Node* temp = head1;
while (temp != NULL) {
appendNode(&result, temp->data);
temp = temp->next;
temp = head2;
while (temp != NULL) {
if (!isPresent(result, temp->data)) {
appendNode(&result, temp->data);
temp = temp->next;
return result;
}
2. Form a list containing the INTERSECTION of the elements of
two lists (singly linked list).
struct Node* intersectLists(struct Node* head1, struct Node* head2) {
struct Node* result = NULL;
struct Node* temp1 = head1;
while (temp1 != NULL) {
struct Node* temp2 = head2;
while (temp2 != NULL) {
if (temp1->data == temp2->data && !isPresent(result, temp1-
>data)) {
appendNode(&result, temp1->data);
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}
return result;
}
3. Insert an element after the nth element of a list (singly
linked list).
void insertAfterNth(struct Node* head, int n, int newData) {
struct Node* temp = head;
for (int i = 0; i < n - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp != NULL) {
struct Node* newNode = createNode(newData);
newNode->next = temp->next;
temp->next = newNode;
}
}
4. Place the elements of a list in increasing order (singly linked
list).
void sortList(struct Node** head) {
struct Node* current = *head;
struct Node* nextNode;
int temp;
while (current != NULL) {
nextNode = current->next;
while (nextNode != NULL) {
if (current->data > nextNode->data) {
temp = current->data;
current->data = nextNode->data;
nextNode->data = temp;
}
nextNode = nextNode->next;
}
current = current->next;
}
}
5. Return the number of element in a list (singly linked list).
int countElements(struct Node* head) {
int count = 0;
struct Node* temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
6. Return the sum of integers in a list (doubly linked list)
struct DNode {
int data;
struct DNode* next;
struct DNode* prev;
};
int sumDoublyList(struct DNode* head) {
int sum = 0;
struct DNode* temp = head;
while (temp != NULL) {
sum += temp->data;
temp = temp->next;
}
return sum;
}
7. Concatenate two lists (circular singly linked list)
struct Node* concatenateCircularLists(struct Node* head1, struct Node*
head2) {
if (head1 == NULL) return head2;
if (head2 == NULL) return head1;
struct Node* temp = head1;
while (temp->next != head1) {
temp = temp->next;
}
temp->next = head2;
temp = head2;
while (temp->next != head2) {
temp = temp->next;
}
temp->next = head1;
return head1;
}
8. Move node (p) forward n position in the list (circular doubly
linked list).
void moveNodeForward(struct DNode** head, int p, int n) {
struct DNode* temp = *head;
for (int i = 0; i < p; i++) {
temp = temp->next;
}
struct DNode* movingNode = temp;
for (int i = 0; i < n; i++) {
movingNode = movingNode->next;
}
// Adjust pointers to move the node forward
}
9. Recursive function to find number of nodes in a linked list
(singly linked list).
int countNodesRecursive(struct Node* head) {
if (head == NULL) return 0;
return 1 + countNodesRecursive(head->next);
}
10. Addition of long positive integers using circular lists
struct Node* addLongIntegersCircular(struct Node* head1, struct Node*
head2) {
// Initialize a result list and add corresponding digits with carry
}
11. Addition of long integers using doubly linked lists
struct DNode* addLongIntegersDoubly(struct DNode* head1, struct
DNode* head2) {
// Initialize a result list and add corresponding digits with carry
}
12. Represent polynomial in three variables ( x, y, and z) be
represented by a circular list
struct PolyNode {
int coeff;
int x;
int y;
int z;
struct PolyNode* next;
};
struct PolyNode* representPolynomial() {
// Create a circular linked list to represent polynomial terms
}
13. Add two polynomial (singly linked list)
struct PolyNode* addPolynomials(struct PolyNode* head1, struct
PolyNode* head2) {
// Initialize a result list and add corresponding terms
}
14. Multiply two polynomial (singly linked list).
struct PolyNode* multiplyPolynomials(struct PolyNode* head1,
struct PolyNode* head2) {
// Initialize a result list and multiply corresponding terms
}
15. Take the PARTIAL DERIVATIVE of polynomial with respect
to any of its variables. (doubly linked list)
struct PolyNode* partialDerivative(struct PolyNode* head, char variable) {
// Derive the polynomial with respect to the given variable
}
16. Divide the polynomial by another polynomial creating a
QUOTIENT and a REMAINDER polynomial ( singly circular
linked list)
struct PolyNode* dividePolynomials(struct PolyNode* head1, struct
PolyNode* head2) {
// Initialize quotient and remainder lists and perform division
}