By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

Linked list

P: 5
struct node *insert(struct node *p);
void *add (struct node *a,struct node *b);
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int cof,ex,ey,ez;
struct node *link;

};
void main ()
{
struct node *a,*b,*q;
int i,ter1,ter2;
clrscr();
printf("\n\nNo of terms you want to enter for first polynomial");
scanf("%d",&ter1);
printf("\n\nNo of terms you want to enter for second polynomial");
scanf("%d",&ter2);
a=NULL;
b=NULL;
for(i=0;i<ter1;i++)
{
a=insert(a);
}
for(i=0;i<ter2;i++)
{
b=insert(b);
}
printf("\n\nFor first polynomial");
q=a;
for(i=0;i<ter1;i++)
{
printf("\n\nEnter the coffecient & exponent of x,y,z of %d term :",i+1);
scanf("%d%d%d%d",&q->cof,&q->ex,&q->ey,&q->ez);
q=q->link;
}
printf("\n\nFor second polynomial");
q=b;
for(i=0;i<ter2;i++)
{
printf("\n\nEnter the coffecient & exponent of x,y,z of %d term :",i+1);
scanf("%d%d%d%d",&q->cof,&q->ex,&q->ey,&q->ez);
q=q->link;
}

printf("\n\nFirst polynomial is :\n\n");
q=a;
for(i=0;i<ter1;i++)
{
printf("%d(X^%dY^%dZ^%d) + ",q->cof,q->ex,q->ey,q->ez);
q=q->link;
}

printf("\n\nSecond polynomial is :\n\n");
q=b;
for(i=0;i<ter2;i++)
{
printf("%d(X^%dY^%dZ^%d) + ",q->cof,q->ex,q->ey,q->ez);
q=q->link;
}

add(a,b);
getch();
}

struct node *insert(struct node *p)
{
struct node *temp;
if(p==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
p=temp;
temp->link=NULL;
}
else
{
temp=p;
temp->link=(struct node*)malloc(sizeof(struct node));
temp=temp->link;
temp->link=NULL;
}
return(p);
}

void *add(struct node *a,struct node *b)
{
struct node *q,*p,*c,*temp;
int i=0,j;
p=a;
q=b;
c=NULL;
while(p->link!=NULL && q->link!=NULL)
{
if(p->ex==q->ex && p->ey==q->ey && p->ez==q->ez)
{
c=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof + q->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
temp->link=NULL;
i=i+1;
p=p->link;
q=q->link;
}
else
{
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
c=insert(c);
temp->link=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=q->cof;
temp->ex=q->ex;
temp->ey=q->ey;
temp->ez=q->ez;
i=i+2;
}
}
if(p->link==NULL && q->link!=NULL)
{
if(p->ex==q->ex && p->ey==q->ey && p->ez==q->ez)
{
c=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=insert(c);
temp->cof=p->cof + q->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
temp->link=NULL;
i=i+1;
q=q->link;
}
else
{
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=insert(c);
temp=temp->link;
temp->cof=p->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
c=insert(c);
temp->link=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=q->cof;
temp->ex=q->ex;
temp->ey=q->ey;
temp->ez=q->ez;
i=i+2;
q=q->link;
}
do
{
temp->link=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=q->cof;
temp->ex=q->ex;
temp->ey=q->ey;
temp->ez=q->ez;
i=i+1;
q=q->link;
}while(q->link!=NULL);
}
if(q->link==NULL)
{
if(p->ex==q->ex && p->ey==q->ey && p->ez==q->ez)
{
c=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof + q->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
temp->link=NULL;
i=i+1;
p=p->link;
}
else
{
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=q->cof;
temp->ex=q->ex;
temp->ey=q->ey;
temp->ez=q->ez;
i=i+2;
p=p->link;
}
do
{
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
i=i+1;
}while(p->link!=NULL);
}
printf("\n\n");
temp=c;
for(j=0;j<i;j++)
{
printf("%d(X^%dY^%dZ^%d)+",temp->cof,temp->ex,temp->ey,temp->ez);
temp=temp->link;
}

}

Help me to solve this question
Sep 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: 61
I think you are storing each term of the polynomial in a new node in a linked list. By using the below function you are creating the nodes for storing the terms of the polynomial.

struct node *insert(struct node *p)
{
struct node *temp;
if(p==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
p=temp;
temp->link=NULL;
}
else
{
temp=p;
temp->link=(struct node*)malloc(sizeof(struct node));
temp=temp->link;
temp->link=NULL;
}
return(p);
}

When you call this function for first two times, it works properly. But third time onwards, it will not work.

Let us assume that you require 4 nodes to store the terms of the polynomial and list is created upto the second node. When the above function is called for creating the third node it does the following...

1. The execution control will go the else portion and assign the head of the list to temp variable.

2. Now you are creating another node in the link portion where already a node exists. So the new node will overwrite this portion and you will ultimately get two nodes irrespective of the number of calls you made to this function.

So change the function to..........

struct node *insert(struct node *p)
{
if(p==NULL)
{
p=(struct node*)malloc(sizeof(struct node));
p->link=NULL;
}
else
{
p->link=insert(p->link);
}
return(p);
}


OR


struct node *insert(struct node *p)
{
struct node *temp;
if(p==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
p=temp;
temp->link=NULL;
}
else
{
temp=p;

while(temp->link != NULL) /* This loop will take you to the last node */
temp=temp->link;

temp->link=(struct node*)malloc(sizeof(struct node));
temp=temp->link;
temp->link=NULL;
}
return(p);
}
Sep 16 '06 #2

P: 5
Thank you for pointing out the error
I want to multiply these two polynomial
Code for multipliction is

void *multiply(struct node *a,struct node *b,int ter1,int ter2)
{
struct node *p,*q,*c,*temp;
int i,j;
c=NULL;
p=a;
q=b;
for(i=0;i<ter1*ter2;i++)
{
c=insert(c);

}
temp=c;
for(i=0;i<ter1;i++)
{
for(j=0;j<ter2;j++)
{
temp->cof=p->cof*q->cof;
temp->ex=p->ex+q->ex;
temp->ey=p->ey+q->ey;
temp->ez=p->ez+q->ez;
temp=temp->link;
q=q->link;
}
q=b;
temp=temp->link;
p=p->link;
}
printf("\n\n ");
temp=c;
for(i=0;i<(ter1*ter2);i++)
{
printf("%d(X^%dY^%dZ^%d)+",temp->cof,temp->ex,temp->ey,temp->ez);
temp=temp->link;
}
}
please help to solve this.
Sep 20 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.