Skip to content

Instantly share code, notes, and snippets.

@Rajssss
Last active August 7, 2019 12:20
Show Gist options
  • Save Rajssss/1ab73997b8d19b65c821e86dad4bb74e to your computer and use it in GitHub Desktop.
Save Rajssss/1ab73997b8d19b65c821e86dad4bb74e to your computer and use it in GitHub Desktop.
C Programe to Add Two Polynomials in Linked List....
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct Poly
{
int Coeff;
int Pow;
struct Poly *next;
} *first, *second, *Sum;
void Create();
void Display();
void addPoly();
int main(void)
{
int option, index;
printf("Enter value for 1st Polynomial...\n");
first = (struct Poly*)malloc(sizeof(struct Poly));
printf("Enter Coefficient and Power resp. =>");
scanf("%d %d", &first->Coeff, &first->Pow);
first->next = 0;
printf("More Elements?\n\t1. Yes\n\t2. No\n\t=>");
scanf("%d", &option);
while(option == 1)
{
Create(first);
printf("More?\n\t1. Yes\n\t2. No\n\t=>");
scanf("%d", &option);
}
//2nd Polynomial
printf("Enter value for 2nd Polynomial...\n");
second = (struct Poly*)malloc(sizeof(struct Poly));
printf("Enter Coefficient and Power resp. =>");
scanf("%d %d", &second->Coeff, &second->Pow);
second->next = 0;
printf("More Elements?\n\t1. Yes\n\t2. No\n\t=>");
scanf("%d", &option);
while(option == 1)
{
Create(second);
printf("More?\n\t1. Yes\n\t2. No\n\t=>");
scanf("%d", &option);
}
printf("1st Polynomial is => ");
printf("\n");
Display(first);
printf("\n2nd Polynomial is =>\n");
Display(second);
printf("Calculating Sum of both Polynomials......\n");
Sum = (struct Poly*)malloc(sizeof(struct Poly));
addPoly(first, second, Sum);
printf("\n");
Display(Sum);
return 0;
}
void Create(struct Ploy* head)
{
struct Poly *temp = head, *newElement;
newElement = (struct Poly*)malloc(sizeof(struct Poly));
printf("Enter Coefficient and Power resp. =>");
scanf("%d %d", &newElement->Coeff, &newElement->Pow);
newElement->next = 0;
while(temp->next != 0)
temp = temp->next;
temp->next = newElement;
}
void Display(struct Poly *head)
{
if(head)
{
//Display(first->next); //Reverse Print
printf("%d\t%d\n", head->Coeff, head->Pow);
Display(head->next);// Normal Print
}
}
void addPoly(struct Poly *head1, struct Poly *head2, struct Poly *Sum)
{
while(head1 && head2)
{
if(head1->Pow>head2->Pow)
{
Sum->Pow = head1->Pow;
Sum->Coeff = head1->Coeff;
head1 = head1->next;
}
else if(head1->Pow<head2->Pow)
{
Sum->Pow = head2->Pow;
Sum->Coeff = head2->Coeff;
head2 = head2->next;
}
else
{
Sum->Pow = head1->Pow;
Sum->Coeff = head1->Coeff + head2->Coeff;
head1 = head1->next;
head2 = head2->next;
}
if (head1 && head2)
{
Sum->next = (struct Poly*)malloc(sizeof(struct Poly));
Sum = Sum->next;
Sum->next = 0;
}
}
while(head1 || head2)
{
if(head1)
{
Sum->Pow = head1->Pow;
Sum->Coeff = head1->Coeff;
head1 = head1->next;
}
else if(head2)
{
Sum->Pow = head2->Pow;
Sum->Coeff = head2->Coeff;
head2 = head2->next;
}
if (head1 && head2)
{
Sum->next = (struct Poly *) malloc(sizeof(struct Poly));
Sum = Sum->next;
Sum->next = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment