0% found this document useful (0 votes)
36 views8 pages

Implementation of Grounded Header Linked List

This document contains C code implementing linked lists and polynomials. It defines Node structures for linked lists and Term structures for polynomials. Functions are defined to initialize and manipulate linked lists, add/multiply/display polynomials, and free allocated memory. The main function tests the polynomial functions by creating sample polynomials, performing operations on them, and printing the results.

Uploaded by

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

Implementation of Grounded Header Linked List

This document contains C code implementing linked lists and polynomials. It defines Node structures for linked lists and Term structures for polynomials. Functions are defined to initialize and manipulate linked lists, add/multiply/display polynomials, and free allocated memory. The main function tests the polynomial functions by creating sample polynomials, performing operations on them, and printing the results.

Uploaded by

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

22053786

Mohammad Adib Akhtar Ansari


CSE-18 DSA assignment
// IMPLEMENTATION OF GROUNDED HEADER 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));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Node* init_22053786() {
struct Node* header = createNode(0);
return header;
}
void insert_22053786(struct Node* header, int data) {
struct Node* newNode = createNode(data);
struct Node* current = header;

while (current->next != NULL) {


current = current->next;
}

current->next = newNode;
}
void print_22053786(struct Node* header) {
struct Node* current = header->next;

if (current == NULL) {
printf("Linked list is empty.\n");
return;
}

printf("Linked List: ");


while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void free_22053786(struct Node* header) {
struct Node* current = header->next;
while (current != NULL) {
struct Node* temp = current;
current = current->next;
free(temp);
}
free(header);
}
int main() {
struct Node* header = init_22053786();

insert_22053786(header, 2205);
22053786
Mohammad Adib Akhtar Ansari
CSE-18 DSA assignment
insert_22053786(header, 3);
insert_22053786(header, 786);

print_22053786(header);

free_22053786(header);

return 0;
}

// Implementation of circular header 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));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Node* init_22053786() {
struct Node* header = createNode(0);
header->next = header;
return header;
}
void insert_22053786(struct Node* header, int data) {
struct Node* newNode = createNode(data);
struct Node* current = header;
22053786
Mohammad Adib Akhtar Ansari
CSE-18 DSA assignment

while (current->next != header) {


current = current->next;
}

current->next = newNode;
newNode->next = header;
}
void print_22053786(struct Node* header) {
struct Node* current = header->next;

if (current == header) {
printf("Circular linked list is empty.\n");
return;
}

printf("Circular Linked List: ");


do {
printf("%d -> ", current->data);
current = current->next;
} while (current != header);
printf("HEAD\n");
}
int main() {
struct Node* header = init_22053786();

insert_22053786(header, 10);
insert_22053786(header, 20);
insert_22053786(header, 30);

print_22053786(header);

return 0;
}
22053786
Mohammad Adib Akhtar Ansari
CSE-18 DSA assignment
// Implementation of Add polynomial;
#include <stdio.h>
#include <stdlib.h>

struct Term {
int coefficient;
int exponent;
struct Term* next;
};
struct Term* createTerm(int coefficient, int exponent) {
struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));
if (newTerm == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
newTerm->coefficient = coefficient;
newTerm->exponent = exponent;
newTerm->next = NULL;
return newTerm;
}
void addTerm(struct Term** poly, int coefficient, int exponent) {
struct Term* newTerm = createTerm(coefficient, exponent);
if (*poly == NULL) {
*poly = newTerm;
} else {
struct Term* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}
struct Term* _22054426_add_polynomials(struct Term* poly1, struct Term* poly2) {
struct Term* result = NULL;

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


if (poly1->exponent > poly2->exponent) {
addTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
} else if (poly1->exponent < poly2->exponent) {
addTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
} else {
int sum_coeff = poly1->coefficient + poly2->coefficient;
if (sum_coeff != 0) {
addTerm(&result, sum_coeff, poly1->exponent);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}

while (poly1 != NULL) {


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

while (poly2 != NULL) {


addTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
22053786
Mohammad Adib Akhtar Ansari
CSE-18 DSA assignment
}

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

while (poly != NULL) {


printf("%d", poly->coefficient);
if (poly->exponent != 0) {
printf("x^%d", poly->exponent);
}

if (poly->next != NULL) {
printf(" + ");
} else {
printf("\n");
}

poly = poly->next;
}
}
void freePolynomial(struct Term* poly) {
while (poly != NULL) {
struct Term* temp = poly;
poly = poly->next;
free(temp);
}
}
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;

addTerm(&poly1, 3, 2);
addTerm(&poly1, 2, 1);
addTerm(&poly1, 1, 0);

addTerm(&poly2, 4, 3);
addTerm(&poly2, 2, 2);
addTerm(&poly2, 5, 0);

printf("First Polynomial: ");


displayPolynomial(poly1);

printf("Second Polynomial: ");


displayPolynomial(poly2);

struct Term* result = _22054426_add_polynomials(poly1, poly2);


printf("Sum of Polynomials: ");
displayPolynomial(result);

freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);
22053786
Mohammad Adib Akhtar Ansari
CSE-18 DSA assignment
return 0;
}

// implementation of multiplication polynomial;


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

struct Term {
int coefficient;
int exponent;
struct Term* next;
};
struct Term* createTerm(int coefficient, int exponent) {
struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));
if (newTerm == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
newTerm->coefficient = coefficient;
newTerm->exponent = exponent;
newTerm->next = NULL;
return newTerm;
}
void addTerm(struct Term** poly, int coefficient, int exponent) {
struct Term* newTerm = createTerm(coefficient, exponent);
if (*poly == NULL) {
*poly = newTerm;
} else {
struct Term* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}
struct Term* _22054426_polynomial_multiplication(struct Term* poly1, struct Term* poly2) {
struct Term* result = NULL;
while (poly1 != NULL) {
struct Term* temp = poly2;
while (temp != NULL) {
int new_coefficient = poly1->coefficient * temp->coefficient;
int new_exponent = poly1->exponent + temp->exponent;
addTerm(&result, new_coefficient, new_exponent);
temp = temp->next;
22053786
Mohammad Adib Akhtar Ansari
CSE-18 DSA assignment
}
poly1 = poly1->next;
}
return result;
}
void displayPolynomial(struct Term* poly) {
if (poly == NULL) {
printf("0\n");
return;
}
while (poly != NULL) {
printf("%d", poly->coefficient);
if (poly->exponent != 0) {
printf("x^%d", poly->exponent);
}
if (poly->next != NULL) {
printf(" + ");
} else {
printf("\n");
}
poly = poly->next;
}
}
void freePolynomial(struct Term* poly) {
while (poly != NULL) {
struct Term* temp = poly;
poly = poly->next;
free(temp);
}
}
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;

addTerm(&poly1, 3, 2);
addTerm(&poly1, 2, 1);
addTerm(&poly1, 1, 0);

addTerm(&poly2, 4, 3);
addTerm(&poly2, 2, 2);
addTerm(&poly2, 5, 0);

printf("First Polynomial: ");


displayPolynomial(poly1);
printf("Second Polynomial: ");
displayPolynomial(poly2);

struct Term* result = _22054426_polynomial_multiplication(poly1, poly2);


printf("Multiplication of Polynomials: ");
displayPolynomial(result);
freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);
return 0;
}
22053786
Mohammad Adib Akhtar Ansari
CSE-18 DSA assignment

You might also like