Skip to content

Commit

Permalink
Merge pull request #335 from Simranverma123/master
Browse files Browse the repository at this point in the history
Program added
  • Loading branch information
swaaz authored Oct 16, 2021
2 parents 94b4ffa + fa4d780 commit 2b2a596
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions C/program-83/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Program 83
Program in C to represent polynomial expression in one variable using linked list.
67 changes: 67 additions & 0 deletions C/program-83/polynomial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include<stdio.h>
#include<stdlib.h>

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;
}

0 comments on commit 2b2a596

Please sign in to comment.