Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}