Polynomial representation using Linked List
The linked list can be used to represent a polynomial of any degree. Simply the information field is changed according to the number of variables used in the polynomial. If a single variable is used in the polynomial the information field of the node contains two parts: one for coefficient of variable and the other for degree of variable. Let us consider an example to represent a polynomial using linked list as follows:
#inculde<stdio.h>
#inculde<stdlib.h>
#inculde<conio.h>
struct node
{
int coeft;
int degree;
struct node *link;
};
typedef struct node poly;
main()
{
poly *root,*temp,*new;
int hdegree, coeft;
root=NULL;
clrscr();
printf(βEnter the highest degree of polynomial:β)
scanf(β%dβ,&hdegree);
while(hdegree>=0)
{
printf(βEnter coefficient of variable with degree %dβ,hdegree);
scanf(β%dβ,&coeft);
if(coeft!=0)
{
new=(poly*)malloc(sizeof(poly));
if(new= =NULL)
{
printf(βMemory allocation errorβ¦β); exit(0);
}
new->coeft=coeft;
new->degree=hdegree;
new->link=NULL;
if(root= =NULL)
{
root=new;
temp=root;
}
else
{
temp->link=new;
temp=new;
}
}
hdegree--;
}
clrscr();
printf(β\n The Polynomial is:\n\nβ);
temp=root;
while(temp!=NULL)
{
if(temp->coeft>0)
printf(β+%dx%dβ,temp->coeft,temp->degree);
else
printf(β%dx%dβ,temp->coeft,temp->degree);
temp=temp->link;
}
getch();
}
Labels: Data Structure, DS Unit-1
Discussion & Queries (<$I18NNumComments$>):
<$CommentPager$>
- <$I18NAtCommentTimeWithPermalink$>, <$I18NCommentAuthorSaid$>
<$CommentPager$>
<$BlogCommentBody$>
<$BlogCommentDeleteIcon$>