-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #335 from Simranverma123/master
Program added
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|