Saturday, 15 February 2014

Representation of polynomials using linked list

Aim: To Represent polynomials using linked list in cprogram

program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void display();

void insert();

struct poly

{

int coef,exp; 

struct poly *next;

};

typedef struct poly node;

node *header,*ptr,*temp;

void main()

{

int ch;

clrscr(); 

        header->coef=NULL;

header->exp=NULL;

header->next=NULL;

while(1)

{ 

printf("1.insert poly\t 2.display\t 3.exit\n"); 

printf("enter your choice");  

                scanf("%d",&ch);

switch(ch)

{  

case 1: insert();  

                                        break;  

case 2:  

display(); 

break;  

default:  

exit(0); 

}

}

void insert()

{ 

       int coef,exp; 

       printf("enter coefficients,exponen for term ");

scanf("%d%d",&coef,&exp);

ptr=(node *)malloc(sizeof(node));

ptr->coef=coef; ptr->exp=exp; 

       ptr->next=NULL; 

       if(header->next==NULL) '

header->next=ptr;

else

{  

               temp=header;  

               while(temp->next!=NULL) 

temp=temp->next; 

temp->next=ptr;

}

}

void display()

{

printf("header->"); 

      ptr=header->next;

while(ptr!=NULL)

{ 

printf("[%d][%d]->",ptr->coef,ptr->exp); 

ptr=ptr->next; 

       }

printf("NULL\n");
}

out put:





No comments:

Post a Comment