Assingment 3
Assingment 3
Q1
#include <stdio.h>
#include <stdlib.h>
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;
if (exp1 == exp2) {
int sum_coef = coef1 + coef2;
insertTerm(&result, sum_coef, exp1);
return result;
}
int main() {
int numTerms1, numTerms2;
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);
freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);
return 0;
}
if (*poly == NULL) {
*poly = newTerm;
} else {
Term* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}
current2 = current2->next;
}
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;
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;
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;
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;
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;
return head;
}
Node* delete(Node* head, int value) {
Node* current = head;
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;
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");
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