Wednesday, 7 December 2016

Representing Polynomials using Linked List

Here's a simple application of a singly linked list. It can be used to represent polynomials and we can add and subtract two polynomials with the help of it.

#include <stdio.h>
#include <stdlib.h>
struct node{
int c,e;
struct node *next;
};
struct node *create(struct node *p);
struct node *insert(struct node *root, int c, int e);
struct node *add_poly(struct node *p1, struct node *p2, struct node *root);
struct node *sub_poly(struct node *p1, struct node *p2, struct node *root);
void display(struct node *p);
int main()
{
struct node *p1 = NULL, *p2 = NULL, *p3 = NULL, *p4 = NULL;
printf("1st polynomial:\n");
p1 = create(p1);
printf("2nd polynomial:\n");
p2 = create(p2);
printf("First polynomial looks like:\n");
display(p1);
printf("Second polynomial looks like:\n");
display(p2);
p3 = add_poly(p1, p2, p3);
printf("Result of addition:\n");
display(p3);
p4 = sub_poly(p1, p2, p4);
printf("Result of subtraction:\n");
display(p4);
return 0;
}
struct node *create(struct node *p){
struct node *nn, *last = NULL;
int coeff,exp;
printf("Enter coeff and exp or enter -1 to stop: ");
scanf("%d%d", &coeff,&exp);
while(coeff != -1 || exp != -1){
nn = (struct node *)malloc(sizeof(struct node));
nn->c = coeff;
nn->e = exp;
nn->next = NULL;
if(p == NULL){
p = nn;
last = nn;
}else{
last->next = nn;
last = nn;
}
printf("Enter coeff and exp or enter -1 to stop: ");
scanf("%d%d", &coeff,&exp);
}
return p;
};
void display(struct node *p){
while(p != NULL){
if(p -> e != 0) {
printf("%dx^%d ", p->c, p->e);
} else {
printf("%d", p -> c);
}
p = p->next;
}
printf("\n");
}
struct node *add_poly(struct node *p1, struct node *p2, struct node *root){
// Adds p1 and p2 and stores result in root tree
while(p1!= NULL && p2 != NULL){
if(p1->e == p2->e){
root = insert(root, p1->c + p2->c, p1->e);
p1 = p1->next;
p2 =
p2->next;
}
else if(p1->e > p2->e){
root = insert(root, p1->c,p1->e);
p1 = p1->next;
}
else{
root = insert(root,p2->c, p2->e);
p2 = p2->next;
}
}
while(p1 != NULL){
root = insert(root,p1->c, p1->e);
p1 = p1->next;
}
while(p2 != NULL){
root = insert(root,p2->c, p2->e);
p2 = p2->next;
}
return root;
}
struct node *sub_poly(struct node *p1, struct node *p2, struct node *root){
//Subtracts p1 and p2 and stores result in root tree
while(p1 != NULL && p2 != NULL) {
if(p1 -> e == p2 -> e) {
root = insert(root, p1 -> c - p2 -> c, p1 -> e);
p1 = p1 -> next;
p2 = p2 -> next;
} else if(p1 -> e > p2 -> e) {
root = insert(root, p1 -> c, p1 -> e);
p1 = p1 -> next;
} else {
root = insert(root, -p2 -> c, p2 -> e);
p2 = p2 -> next;
}
}
while(p1 != NULL) {
root = insert(root, p1 -> c, p1 -> e);
p1 = p1 -> next;
}
while(p2 != NULL) {
root = insert(root, -p2 -> c, p2 -> e);
p2 = p2 -> next;
}
return root;
}
struct node *insert(struct node *root, int c, int e){
//Inserts a node in tree of root
struct node *nn, *p;
nn = (struct node *)malloc(sizeof(struct node));
nn->c = c;
nn->e = e;
nn->next = NULL;
if(root == NULL){
root = nn;
}else{
p = root;
while(p -> next != NULL) {
p = p -> next;
}
p -> next = nn;
}
return root;
}

No comments:

Post a Comment