Originally Posted by
AndrewHunter
These seem to be the same problems you had in
this post. Have you implemented the changes Salem suggested there? If so, post your
REVISED code and we will see if we can help.
What we aren't going to do is:
1. Do your work for you
2. Do a complete rewrite of the code.
3. Continue to help if you do not follow the advice given <-----very important
I have rewritten the code. Last time, the error is using NULL to point to some place. But this time it seems it is not this case. So I don't know how to fix it. The new codes are in the following. I think the problem is the function insert_monomial.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
struct node /*a node denotes a P_{i,k}^{l}*/
{
int l,i,k;
};
typedef struct node node;
struct monomial_node /* a monomial denotes a monomial P_{i,k}^{l} \cdots P_{i',k'}^{l'} */
{
node y;
int coeff; /* coeff is s(m_r) */
struct monomial_node *next;
};
typedef struct monomial_node monomial_node;
typedef monomial_node *monomial;
struct polynomial_node /* a polynomial denotes a sum of some monomials */
{
monomial m;
struct polynomial_node *next;
};
typedef struct polynomial_node polynomial_node;
typedef polynomial_node *polynomial;
monomial initial_monomial(void) /* construct an empty monomial with head, return the head pointer of the monomial */
{
monomial head=malloc(sizeof(monomial_node));
head->next=NULL;
return head;
}
polynomial initial_polynomial(void) /* construct an empty polynomial with head, return the head pointer of the polynomial */
{
polynomial head=malloc(sizeof(polynomial_node));
head->m=malloc(sizeof(monomial_node));
head->next=NULL;
return head;
}
monomial insert_node(node a, monomial b) /* insert a factor a to a monomial b such that the first subscripts i's in the new monomial is in increasing order */
{
monomial head;
monomial_node *s;
monomial_node *p;
head=b;
p=head;
s=malloc(sizeof(monomial_node)); /* create new node */
s->y=a;
if(p->next == NULL)
{
s->next=NULL;
head->next=s; /* insert the node a */
}
else if(p->next->next==NULL)
{
p=p->next;
if((p->y).i > a.i || (((p->y).i == a.i) && ((p->y).l > a.l)) || (((p->y).i == a.i) && ((p->y).l == a.l) && ((p->y).k >= a.k)))
{
s->next=p;
head->next=s; /* insert the node a */
}
else
{
s->next=NULL;
p->next=s; /* insert the node a */
}
}
else
{
if((p->next->y).i > a.i || (((p->next->y).i == a.i) && ((p->next->y).l > a.l)) || (((p->next->y).i == a.i) && ((p->next->y).l == a.l) && ((p->next->y).k >= a.k)))
{
s->next=p->next;
head->next=s;
}
else
{
while((p->next->y).i <= a.i && (((p->next->y).i != a.i) || ((p->next->y).l <= a.l)) && (((p->next->y).i != a.i) || ((p->next->y).l != a.l) || ((p->next->y).k < a.k))) /* find the position */
{
p=p->next;
if(p->next==NULL)
{
break;
}
}
s->next=p->next;
p->next=s; /* insert the node a */
}
}
return head;
}