Dsa 6
Dsa 6
Dsa 6
Set A
2) Write a program that adds two single variablepolynomials. Each polynomial should be
represented as a list with linked list implementation.
#include <stdio.h>
#include <stdlib.h>
return result;
}
// Main function
int main() {
Term* poly1 = NULL;
Term* poly2 = NULL;
return 0;
}
Set B
1) Write a program that sorts the elements of linked list using any of sorting technique.
#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 append(struct Node** head_ref, int new_data) {
struct Node* newNode = createNode(new_data);
if (*head_ref == NULL) {
*head_ref = newNode;
return;
}
struct Node* last = *head_ref;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
void display(struct Node* node) {
if (node == NULL) {
printf("List is empty.\n");
return;
}
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
void swap(struct Node* a, struct Node* b) {
int temp = a->data;
a->data = b->data;
b->data = temp;
}
void bubbleSort(struct Node* head) {
int swapped;
struct Node* ptr1;
struct Node* lptr = NULL;
if (head == NULL)
return;
do {
swapped = 0;
ptr1 = head;
while (ptr1->next != lptr) {
if (ptr1->data > ptr1->next->data) {
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
void freeList(struct Node** head_ref) {
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
*head_ref = NULL;
}
int main() {
struct Node* head = NULL;
int data;
char choice;
printf("Enter elements of the linked list (enter 'q' to stop):\n");
while (1) {
printf("Enter data: ");
if (scanf("%d", &data) != 1) {
break; // Exit loop if the input is not a valid number
}
append(&head, data);
}
while ((choice = getchar()) != '\n' && choice != EOF);
printf("Original Linked List: ");
display(head);
bubbleSort(head);
printf("Sorted Linked List: ");
display(head);
freeList(&head);
return 0;
}
Set C
1) Write a program to find common elements of two linked lists and create third list.
Ensure that the common elements appear only once in the third 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 append(struct Node** head_ref, int data) {
struct Node* newNode = createNode(data);
if (*head_ref == NULL) {
*head_ref = newNode;
return;
}
struct Node* last = *head_ref;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
void display(struct Node* node) {
if (node == NULL) {
printf("List is empty.\n");
return;
}
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int exists(struct Node* head, int data) {
struct Node* current = head;
while (current != NULL) {
if (current->data == data) {
return 1;
}
current = current->next;
}
return 0;
}
struct Node* findCommonElements(struct Node* list1, struct Node* list2) {
struct Node* commonList = NULL;
struct Node* ptr1 = list1;
struct Node* ptr2;
while (ptr1 != NULL) {
ptr2 = list2;
while (ptr2 != NULL) {
if (ptr1->data == ptr2->data && !exists(commonList, ptr1->data)) {
append(&commonList, ptr1->data);
}
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
return commonList;
}
void freeList(struct Node** head_ref) {
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
*head_ref = NULL;
}
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
struct Node* commonList = NULL;
int n1, n2, data;
printf("Enter the number of elements in the first linked list: ");
scanf("%d", &n1);
printf("Enter the elements of the first list:\n");
for (int i = 0; i < n1; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &data);
append(&list1, data);
}
printf("Enter the number of elements in the second linked list: ");
scanf("%d", &n2);
printf("Enter the elements of the second list:\n");
for (int i = 0; i < n2; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &data);
append(&list2, data);
}
printf("\nFirst Linked List: ");
display(list1);
printf("Second Linked List: ");
display(list2);
commonList = findCommonElements(list1, list2);
printf("\n third List: ");
display(commonList);
freeList(&list1);
freeList(&list2);
freeList(&commonList);
return 0;
}