Write an algorithm to represent a polynomial equation in a linked list
Answers
Answered by
2
◆Hey There◆
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link
{
int coef;
int exp;
struct link *next;
};
struct link *node,*start;
int i;
void create_poly(struct link *node);
void display(struct link *node);
void main()
{
clrscr();
printf("\n create polynomial equation");
node=(struct link *)malloc(sizeof(struct link));
create_poly(node);
printf("\n Output\n");
display(node);
getch();
}
void create_poly(struct link *node)
{
char ans;
i=0;
start->next=NULL;
node=start;
fflush(stdin);
printf("\n Enter 'n' for break:-");
ans=getchar();
while(ans!='n')
{
node->next=(struct link *)malloc(sizeof(struct link));
node=node->next;
printf("\n Enter Coeficient value : ");
scanf("%d",&node->coef);
printf("\n Enter exponent value : ");
scanf("%d",&node->exp);
node->next=NULL;
fflush(stdin);
printf("\n Enter 'n' for break:-");
ans=getchar();
i++;
}
}
void display(struct link *node)
{
node=start->next;
while(node)
{
printf("%dX ^ %d",node->coef,node->exp);
node=node->next;
if(node!=NULL)
printf(" + ");
}
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link
{
int coef;
int exp;
struct link *next;
};
struct link *node,*start;
int i;
void create_poly(struct link *node);
void display(struct link *node);
void main()
{
clrscr();
printf("\n create polynomial equation");
node=(struct link *)malloc(sizeof(struct link));
create_poly(node);
printf("\n Output\n");
display(node);
getch();
}
void create_poly(struct link *node)
{
char ans;
i=0;
start->next=NULL;
node=start;
fflush(stdin);
printf("\n Enter 'n' for break:-");
ans=getchar();
while(ans!='n')
{
node->next=(struct link *)malloc(sizeof(struct link));
node=node->next;
printf("\n Enter Coeficient value : ");
scanf("%d",&node->coef);
printf("\n Enter exponent value : ");
scanf("%d",&node->exp);
node->next=NULL;
fflush(stdin);
printf("\n Enter 'n' for break:-");
ans=getchar();
i++;
}
}
void display(struct link *node)
{
node=start->next;
while(node)
{
printf("%dX ^ %d",node->coef,node->exp);
node=node->next;
if(node!=NULL)
printf(" + ");
}
}
Similar questions
Political Science,
8 months ago
History,
8 months ago
Math,
1 year ago
Science,
1 year ago
Hindi,
1 year ago