From fa4d780db135414055082d7e3867dce21d81f105 Mon Sep 17 00:00:00 2001 From: Simran Date: Tue, 12 Oct 2021 01:50:16 +0530 Subject: [PATCH] Program added --- C/program-83/README.md | 2 ++ C/program-83/polynomial.c | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 C/program-83/README.md create mode 100644 C/program-83/polynomial.c diff --git a/C/program-83/README.md b/C/program-83/README.md new file mode 100644 index 00000000..c5f883bb --- /dev/null +++ b/C/program-83/README.md @@ -0,0 +1,2 @@ +## Program 83 +Program in C to represent polynomial expression in one variable using linked list. \ No newline at end of file diff --git a/C/program-83/polynomial.c b/C/program-83/polynomial.c new file mode 100644 index 00000000..2d1c0049 --- /dev/null +++ b/C/program-83/polynomial.c @@ -0,0 +1,67 @@ +#include +#include + +struct poly +{ + int coeff; + int exp; + struct poly *next; +}; + +void add(struct poly **p,int c,int e) // adding nodes for each order +{ + struct poly *new=*p,*temp; + new=(struct poly *)malloc(sizeof(struct poly)); + new->coeff=c; + new->exp=e; + new->next=NULL; + + if(*p==NULL) // if start is null + *p=new; + else + { + struct poly *temp=*p; + while(temp->next!=NULL) + temp=temp->next; + temp->next=new; + } + +} + +void display(struct poly **p) // for displaying each node that contains different order of X +{ +struct poly*temp=*p; + while(temp!=NULL) + { + printf("+%d",temp->coeff); + if(temp->exp!=0) + printf("x^%d",temp->exp); + temp=temp->next; + } +} + +int main() +{ + int co,ex; + char ch='y'; + struct poly *start=NULL; + printf("Enter the polynomial from highest order to lowest\n"); + while(ch=='y') + { + printf("Enter the coefficient of x:\n"); + scanf("%d",&co); + printf("enter the exponent of x:\n"); + scanf("%d",&ex); + add(&start,co,ex); //calling add function + printf("\n\tDo u want to add more (y/n)"); + getchar(); + scanf("%c",&ch); + + } + + printf("\n\tThe polynomial expression is:\t"); + display(&start); //final display + printf("\n"); + return 0; +} +