0% found this document useful (0 votes)
6 views23 pages

Assingment 3

Uploaded by

lks992004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views23 pages

Assingment 3

Uploaded by

lks992004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Assingment 3

Q1
#include <stdio.h>
#include <stdlib.h>

typedef struct Term {


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

void insertTerm(Term** poly, int coefficient, int exponent) {


Term* newTerm = (Term*)malloc(sizeof(Term));
newTerm->coefficient = coefficient;
newTerm->exponent = exponent;
newTerm->next = NULL;

if (*poly == NULL) {
*poly = newTerm;
} else {
Term* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}
Term* addPolynomials(Term* poly1, Term* poly2) {
Term* result = NULL;

while (poly1 != NULL || poly2 != NULL) {


int coef1 = (poly1 != NULL) ? poly1->coefficient : 0;
int coef2 = (poly2 != NULL) ? poly2->coefficient : 0;
int exp1 = (poly1 != NULL) ? poly1->exponent : 0;
int exp2 = (poly2 != NULL) ? poly2->exponent : 0;

if (exp1 == exp2) {
int sum_coef = coef1 + coef2;
insertTerm(&result, sum_coef, exp1);

if (poly1 != NULL) poly1 = poly1->next;


if (poly2 != NULL) poly2 = poly2->next;
} else if (exp1 > exp2) {
insertTerm(&result, coef1, exp1);
if (poly1 != NULL) poly1 = poly1->next;
} else {
insertTerm(&result, coef2, exp2);
if (poly2 != NULL) poly2 = poly2->next;
}
}

return result;
}

void printPolynomial(Term* poly) {


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

void freePolynomial(Term* poly) {


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

int main() {
int numTerms1, numTerms2;

printf("Enter the number of terms for Polynomial 1: ");


scanf("%d", &numTerms1);

Term* poly1 = NULL;


printf("Enter the coefficients and exponents for Polynomial 1:\n");
for (int i = 0; i < numTerms1; i++) {
int coefficient, exponent;
printf("Term %d: Coefficient Exponent: ", i + 1);
scanf("%d %d", &coefficient, &exponent);
insertTerm(&poly1, coefficient, exponent);
}

printf("Enter the number of terms for Polynomial 2: ");


scanf("%d", &numTerms2);

Term* poly2 = NULL;


printf("Enter the coefficients and exponents for Polynomial 2:\n");
for (int i = 0; i < numTerms2; i++) {
int coefficient, exponent;
printf("Term %d: Coefficient Exponent (e.g., 5 8): ", i + 1);
scanf("%d %d", &coefficient, &exponent);
insertTerm(&poly2, coefficient, exponent);
}

Term* result = addPolynomials(poly1, poly2);

printf("Input Polynomial Equations:\n");


printf("P1: ");
printPolynomial(poly1);
printf("P2: ");
printPolynomial(poly2);

printf("Output Polynomial Equation:\n");


printf("P3: ");
printPolynomial(result);

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

return 0;
}

Output

Q2
#include <stdio.h>
#include <stdlib.h>
typedef struct Term {
int coefficient;
int exponent;
struct Term* next;
} Term;
void insertTerm(Term** poly, int coefficient, int exponent);
Term* multiplyPolynomials(Term* poly1, Term* poly2);
void printPolynomial(Term* poly);
void freePolynomial(Term* poly);
Term* addPolynomials(Term* poly1, Term* poly2);
int main() {
int numTerms1, numTerms2;
printf("Enter the number of terms for Polynomial 1: ");
scanf("%d", &numTerms1);

Term* poly1 = NULL;


printf("Enter the coefficients and exponents for Polynomial 1:\n");
for (int i = 0; i < numTerms1; i++) {
int coefficient, exponent;
printf("Term %d: Coefficient Exponent (e.g., 5 8): ", i + 1);
scanf("%d %d", &coefficient, &exponent);
insertTerm(&poly1, coefficient, exponent);
}

printf("Enter the number of terms for Polynomial 2: ");


scanf("%d", &numTerms2);

Term* poly2 = NULL;


printf("Enter the coefficients and exponents for Polynomial 2:\n");
for (int i = 0; i < numTerms2; i++) {
int coefficient, exponent;
printf("Term %d: Coefficient Exponent (e.g., 5 8): ", i + 1);
scanf("%d %d", &coefficient, &exponent);
insertTerm(&poly2, coefficient, exponent);
}

Term* result = multiplyPolynomials(poly1, poly2);

printf("Input Polynomial Equations:\n");


printf("P1: ");
printPolynomial(poly1);
printf("P2: ");
printPolynomial(poly2);

printf("Output Polynomial Equation:\n");


printf("P3: ");
printPolynomial(result);

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

return 0;
}

void insertTerm(Term** poly, int coefficient, int exponent) {


Term* newTerm = (Term*)malloc(sizeof(Term));
newTerm->coefficient = coefficient;
newTerm->exponent = exponent;
newTerm->next = NULL;

if (*poly == NULL) {
*poly = newTerm;
} else {
Term* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}

Term* multiplyPolynomials(Term* poly1, Term* poly2) {


Term* result = NULL;
Term* tempResult = NULL;

while (poly1 != NULL) {


Term* current2 = poly2;

while (current2 != NULL) {


int coef = poly1->coefficient * current2->coefficient;
int exp = poly1->exponent + current2->exponent;

insertTerm(&tempResult, coef, exp);

current2 = current2->next;
}

result = addPolynomials(result, tempResult);


freePolynomial(tempResult);
tempResult = NULL;

poly1 = poly1->next;
}

return result;
}
void printPolynomial(Term* poly) {
while (poly != NULL) {
printf("%dx^%d", poly->coefficient, poly->exponent);
if (poly->next != NULL) {
printf(" + ");
}
poly = poly->next;
}
printf("\n");
}
void freePolynomial(Term* poly) {
Term* current = poly;
while (current != NULL) {
Term* temp = current;
current = current->next;
free(temp);
}
}
Term* addPolynomials(Term* poly1, Term* poly2) {
Term* result = NULL;

while (poly1 != NULL || poly2 != NULL) {


int coef1 = (poly1 != NULL) ? poly1->coefficient : 0;
int coef2 = (poly2 != NULL) ? poly2->coefficient : 0;
int exp1 = (poly1 != NULL) ? poly1->exponent : 0;
int exp2 = (poly2 != NULL) ? poly2->exponent : 0;

if (exp1 == exp2) {
int sum_coef = coef1 + coef2;
insertTerm(&result, sum_coef, exp1);
if (poly1 != NULL) poly1 = poly1->next;
if (poly2 != NULL) poly2 = poly2->next;
} else if (exp1 > exp2) {
insertTerm(&result, coef1, exp1);
if (poly1 != NULL) poly1 = poly1->next;
} else {
insertTerm(&result, coef2, exp2);
if (poly2 != NULL) poly2 = poly2->next;
}
}
return result;
}

Output

Q3
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
Node* create(int size) {
Node* head = NULL;
Node* tail = NULL;

printf("Enter %d elements for the doubly linked list:\n", size);


for (int i = 0; i < size; i++) {
int data;
printf("Element %d: ", i + 1);
scanf("%d", &data);
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}

return head;
}
void print(Node* head) {
printf("Doubly Linked List: ");
while (head != NULL) {
printf("%d", head->data);
if (head->next != NULL) {
printf(" <-> ");
}
head = head->next;
}
printf("\n");
}
int main() {
int size;
printf("Enter the size of the doubly linked list: ");
scanf("%d", &size);
Node* head = create(size);
print(head);
return 0;
}

Output

Q4
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
Node* create(int size) {
Node* head = NULL;
Node* tail = NULL;

printf("Enter %d elements for the doubly linked list:\n", size);


for (int i = 0; i < size; i++) {
int data;
printf("Element %d: ", i + 1);
scanf("%d", &data);
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}

return head;
}
Node* insert(Node* head, int data1, int data2, int data3) {
Node* newNode1 = (Node*)malloc(sizeof(Node));
newNode1->data = data1;
newNode1->prev = NULL;
newNode1->next = NULL;
Node* newNode2 = (Node*)malloc(sizeof(Node));
newNode2->data = data2;
newNode2->prev = NULL;
newNode2->next = NULL;

Node* newNode3 = (Node*)malloc(sizeof(Node));


newNode3->data = data3;
newNode3->prev = NULL;
newNode3->next = NULL;
Node* tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = newNode1;
newNode1->prev = tail;

newNode1->next = newNode2;
newNode2->prev = newNode1;

newNode2->next = newNode3;
newNode3->prev = newNode2;

return head;
}
void print(Node* head) {
printf("Doubly Linked List: ");
while (head != NULL) {
printf("%d", head->data);
if (head->next != NULL) {
printf(" <-> ");
}
head = head->next;
}
printf("\n");
}
int main() {
int size;
printf("Enter the size of the doubly linked list: ");
scanf("%d", &size);
Node* head = createDoublyLinkedList(size);
int data1, data2, data3;
printf("Enter the last three elements to insert at the end: ");
scanf("%d %d %d", &data1, &data2, &data3);
head = insertAtEnd(head, data1, data2, data3);
printDoublyLinkedList(head);
return 0;
}

Output

Q5
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
Node* create(int size) {
Node* head = NULL;
Node* tail = NULL;

printf("Enter %d elements for the doubly linked list:\n", size);


for (int i = 0; i < size; i++) {
int data;
printf("Element %d: ", i + 1);
scanf("%d", &data);
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}

return head;
}
Node* delete(Node* head, int value) {
Node* current = head;

while (current != NULL) {


if (current->data == value) {
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
head = current->next;
}

if (current->next != NULL) {
current->next->prev = current->prev;
}

free(current);
break;
}

current = current->next;
}

return head;
}
void print(Node* head) {
printf("Doubly Linked List: ");
while (head != NULL) {
printf("%d", head->data);
if (head->next != NULL) {
printf(" <-> ");
}
head = head->next;
}
printf("\n");
}
int main() {
int size;
printf("Enter the size of the doubly linked list: ");
scanf("%d", &size);
Node* head = create(size);
int valueToDelete;
printf("Enter the value to delete from the doubly linked list: ");
scanf("%d", &valueToDelete);
head = delete(head, valueToDelete);
print(head);

return 0;
}

Output

Bonus
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
Node* create(int size) {
Node* head = NULL;
Node* tail = NULL;
printf("Enter %d elements for the doubly linked list:\n", size);
for (int i = 0; i < size; i++) {
int data;
printf("Element %d: ", i + 1);
scanf("%d", &data);
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}

return head;
}
Node* insert(Node* head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
} else {
Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}

return head;
}
Node* delete(Node* head, int value) {
Node* current = head;

while (current != NULL) {


if (current->data == value) {
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
break;
}

current = current->next;
}

return head;
}
void display(Node* head) {
printf("Doubly Linked List: ");
while (head != NULL) {
printf("%d", head->data);
if (head->next != NULL) {
printf(" <-> ");
}
head = head->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
int choice, size, data, valueToDelete;

do {
printf("\nSelect an option:\n");
printf("1. Create a doubly linked list\n");
printf("2. Insert a node in the doubly linked list\n");
printf("3. Delete a node in the doubly linked list\n");
printf("4. Display the elements of the doubly linked list\n");
printf("0. Exit\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the size of the doubly linked list: ");
scanf("%d", &size);
head = create(size);
break;

case 2:
printf("Enter the data for the new node: ");
scanf("%d", &data);
head = insert(head, data);
break;

case 3:
printf("Enter the value to delete from the doubly linked list: ");
scanf("%d", &valueToDelete);
head = delete(head, valueToDelete);
break;

case 4:
display(head);
break;
case 0:
printf("Exiting the program.\n");
break;

default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 0);
return 0;
}

Output

You might also like