Dsa 6

Télécharger au format docx, pdf ou txt
Télécharger au format docx, pdf ou txt
Vous êtes sur la page 1sur 7

Practical 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>

// Structure to represent a term in the polynomial


typedef struct Term {
int coefficient;
int exponent;
struct Term *next;
} Term;

// Function to create a new term


Term* createTerm(int coeff, int exp) {
Term* newTerm = (Term*)malloc(sizeof(Term));
newTerm->coefficient = coeff;
newTerm->exponent = exp;
newTerm->next = NULL;
return newTerm;
}

// Function to add a term to the polynomial


void addTerm(Term** poly, int coeff, int exp) {
Term* newTerm = createTerm(coeff, exp);
if (*poly == NULL || (*poly)->exponent < exp) {
newTerm->next = *poly;
*poly = newTerm;
} else {
Term* current = *poly;
while (current->next != NULL && current->next->exponent > exp) {
current = current->next;
}
if (current->exponent == exp) {
current->coefficient += coeff;
free(newTerm); // Free the new term if it is merged
} else {
newTerm->next = current->next;
current->next = newTerm;
}
}
}

// Function to add two polynomials


Term* addPolynomials(Term* poly1, Term* poly2) {
Term* result = NULL;

while (poly1 != NULL) {


addTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
}

while (poly2 != NULL) {


addTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
}

return result;
}

// Function to print the polynomial


void printPolynomial(Term* poly) {
if (poly == NULL) {
printf("0\n");
return;
}

while (poly != NULL) {


if (poly->coefficient != 0) {
if (poly->exponent == 0) {
printf("%d ", poly->coefficient);
} else {
printf("%dx^%d ", poly->coefficient, poly->exponent);
}
}
poly = poly->next;
if (poly != NULL && poly->coefficient > 0) {
printf("+ ");
}
}
printf("\n");
}

// Function to free the polynomial


void freePolynomial(Term* poly) {
Term* temp;
while (poly != NULL) {
temp = poly;
poly = poly->next;
free(temp);
}
}

// Main function
int main() {
Term* poly1 = NULL;
Term* poly2 = NULL;

// Creating first polynomial: 3x^2 + 2x + 1


addTerm(&poly1, 3, 2);
addTerm(&poly1, 2, 1);
addTerm(&poly1, 1, 0);
// Creating second polynomial: 5x^1 + 4x^0
addTerm(&poly2, 5, 1);
addTerm(&poly2, 4, 0);

// Print the polynomials


printf("Polynomial 1: ");
printPolynomial(poly1);
printf("Polynomial 2: ");
printPolynomial(poly2);

// Add the two polynomials


Term* result = addPolynomials(poly1, poly2);
printf("Resultant Polynomial: ");
printPolynomial(result);

// Free allocated memory


freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);

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;
}

Vous aimerez peut-être aussi