top of page
Writer's pictureApurba Paul

Display a Polynomial using Singly Linked List

#include<stdio.h> typedef struct mynode { int coeff,expo; struct mynode *Link; }Node; Node *start=NULL,*start2=NULL; //Display Function void display() { Node *tptr=start; printf("\nPolynomial:\n"); while(tptr) { if(tptr->coeff>0) { if(tptr->expo==1) { printf("%dx+",tptr->coeff); } else if(tptr->expo==0) { printf("%d",tptr->coeff); } else { printf("%dx^%d+",tptr->coeff,tptr->expo); } tptr=tptr->Link; } else { if(tptr->expo==1) { printf("%dx",tptr->coeff); } else if(tptr->expo==0) { printf("%d",tptr->coeff); } else { printf("%dx^%d",tptr->coeff,tptr->expo); } tptr=tptr->Link; } } } //Insertion at the End function void add_node(int v1,int v2) { Node *newp=NULL,*tptr=NULL; newp=(Node*)malloc(sizeof(Node)); newp->coeff=v1; newp->expo=v2; newp->Link=NULL; if(start==NULL) { start=newp; } else { tptr=start; while(tptr->Link!=NULL) { tptr=tptr->Link; } tptr->Link=newp; } } //Main int main() { int v1,v2; char choice; while(v2!=0) {   printf("\nEnter the co-eff:");   scanf("%d",&v1);   fflush(stdin);   printf("\nEnter the expo:");   scanf("%d",&v2);   fflush(stdin);   add_node(v1,v2);   fflush(stdin); } display(); return 0; }

10 views0 comments

Recent Posts

See All

Comments


bottom of page