What is the error of my program?

What's wrong with this program? I use gdb to debug the program. It said that: Program received signal SIGSEGV, Segmentation fault.
0x080491c7 in simplify_polynomial (n=0x804c148) at test7.c:476
476 p=p->head->next;
I don't know what is the problem. Could you help me? Thank you.

[code] #include
#include
#include
#include
#include



typedef struct node /*a node denotes a P_{i,k}^{l}*/
{

int l,i,k;

} node;


typedef struct monomial_node /* a monomial denotes a monomial P_{i,k}^{l} cdots P_{i',k'}^{l'} */

{
node y;
int coeff; /* coeff is the coefficent of the monomial */
struct monomial_node *next;
struct monomial_node *head;
} monomial_node, *monomial;







typedef struct polynomial_node /* a polynomial denotes a sum of some monomials */

{

monomial m;

struct polynomial_node *next;

struct polynomial_node *head;

} polynomial_node, *polynomial;





monomial insert(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 p;

monomial_node *s;

monomial_node *p1;


p=b;
p1=p->head->next;

if(p1==NULL)
{

s=malloc(sizeof(monomial_node));
s->y=a;
p->head->next=s;
p->head->next->next=NULL;
p->coeff=1;
}
else
{
while((p1->y).i >= a.i) /* find the position */
{

if(p1->next==NULL)
{

break;

}
p1=p1->next;

}
}


s=malloc(sizeof(monomial_node)); /* create new node */

s->y=a;

s->next=p1->next; /* insert the node a */

p1->next=s;

p->coeff=b->coeff;
return p;

}


monomial create_monomial(int number) /* create a monomial */

{

monomial m;

monomial_node *p, *s;

int i;


m=(monomial)malloc(sizeof(monomial_node));
m->head=(monomial)malloc(sizeof(monomial_node));

if(m->head==NULL)

{
printf("No memory.
");
return NULL;

}


m->head->next=NULL;

p=malloc(sizeof(monomial_node));

m->head->next=p;

p->next=NULL;


printf("Please input the parameters of the heighest weight monomial(for example, 2, 2, 3 denotes the monomial Y^{2}_{2,3}). The first subscripts i of Y's should be in increase order.");

scanf("%d", &((p->y).l));
scanf("%d", &((p->y).i));
scanf("%d", &((p->y).k));

if(number > 1)
{
for(i=0;iy).l));
scanf("%d", &((s->y).i));
scanf("%d", &((s->y).k));




m=insert(s->y,m);


}

}

m->coeff=1;



return m;



}



polynomial create_polynomial(monomial n) /* create a polynomial with only one monomial n */

{

polynomial m;
polynomial_node *p;

m=(polynomial)malloc(sizeof(polynomial_node));
p=(polynomial)malloc(sizeof(polynomial_node));
m->head=(polynomial)malloc(sizeof(polynomial_node));

m->head->next=p;
p->m=n;
p->next=NULL;



return m;



}





void print_monomial(monomial m)

{

monomial p;


p=m->head->next;

if(p==NULL)

{

printf("No element.
");

}

if(m->coeff == -1)
{
printf("-");
}
else if(m->coeff != 1)
{
printf("%d", m->coeff);
}

while(p!=NULL)

{

printf("Y^{%d}_{%d,%d}",(p->y).l,(p->y).i,(p->y).k);

p=p->next;

}

}



void print_polynomial(polynomial n)

{

polynomial p;

int i=0;

p=n->head->next;

if(p==NULL)

{

printf("No element.
");

}

while(p!=NULL)

{

if(i !=0)

{

printf("+");

}

i++;

print_monomial(p->m);

p=p->next;

}

printf("
");

}



int length_monomial(monomial a)
{
int len=0;
monomial_node *b;

b=a->head->next;
while(b != NULL)
{
len++;
b=b->next;
}
return len;
}


int length_polynomial(polynomial a)
{
int len=0;
polynomial_node *b;

b=a->head->next;
while(b != NULL)
{
len++;
b=b->next;
}
return len;
}


int compare_nodes(node a, node b)
{
int result;
if(a.l==b.l && a.i==b.i && a.k==b.k)
{
result=1;
}
else
{
result=0;
}
return result;
}


monomial delete_node(monomial a, node b)
{

monomial p;
monomial_node *p1;


p=a;
p1=p->head->next;
p=p->head;

while(p1 != NULL)
{
if(compare_nodes(p1->y, b)==1)
{
p->next = p1->next;
break;
}
p=p->next;
p1=p1->next;
}

return p;

}

monomial simplify_monomial(monomial n)
{
monomial p;
monomial_node *p1, *s;

p=n;


p1=p->head->next;
p=p->head->next;
s=p;

while(p!=NULL) /* merge the terms with the same monomials */
{
while(p1!=NULL)
{
if(p->y.i==p1->y.i && p->y.k==p1->y.k)
{
p->y.l=p1->y.l + p->y.l;
p=delete_node(p, p1->y);
p1=s;
p=s->next;
}
p1=p1->next;
}
p=p->next;
s=p; /* s records the position of p */
p1=p;
}


p1=p->head->next;
while(p1 != NULL)
{
if(p1->y.l==0)
{
p=delete_node(p, p1->y);
}
p1=p1->next;
}

return p;

}




int compare_monomials(monomial m, monomial n) /* do not compare the coefficients of m and n */
{
int result=1;
monomial_node *a;
monomial_node *b;

a=simplify_monomial(m);
b=simplify_monomial(n);


a=a->head->next;
b=b->head->next;



while(a != NULL && b != NULL)
{
if(compare_nodes(a->y, b->y)==0)
{
result=0;
return 0;
break;
}
a=a->next;
b=b->next;
}
if((a==NULL && b != NULL) || (a != NULL && b == NULL))
{
return 0;
}
return result;
}




polynomial delete_monomial(polynomial a, monomial b)
{

polynomial p;
polynomial_node *p1;


p=a;
p1=p->head->next;
p=p->head;

while(p1 != NULL)
{
if(compare_monomials(p1->m, b)==1)
{
p->next = p1->next;
free(b);
break;
}
p=p->next;
p1=p1->next;
}

return p;

}



polynomial simplify_polynomial(polynomial n)
{
polynomial p;
polynomial_node *p1;
polynomial_node *s;

p=n;
p=(n->head)->next;


while(p!=NULL)
{
p->m=simplify_monomial(p->m);
p=p->next;
}

p1=p->head->next;
p=p->head->next;
s=p;

while(p!=NULL) /* merge the terms with the same monomials */
{
while(p1!=NULL)
{
if(compare_monomials(p->m, p1->m)==1)
{
p->m->coeff=p1->m->coeff + p->m->coeff;
p=delete_monomial(p, p1->m);
p1=s;

p=s->next;
}
p1=p1->next;
}
p=p->next;
s=p; /* s records the position of p */
p1=p;
}


p1=p->head->next;
while(p1 != NULL)
{
if(p1->m->coeff==0)
{
p=delete_monomial(p, p1->m);
}
p1=p1->next;
}

return p;

}



int coefficient(polynomial a, monomial b) /* coefficient of a monomial in a polynomial */
{
int coeff=0;
polynomial_node *c;

c=a->head->next;

while(c != NULL)
{
if(compare_monomials(c->m, b)==1)
{
coeff=coeff+(b->coeff);
}

c=c->next;
}
return coeff;
}

int is_dominant(monomial m, int i) /* check that whether a monomial is i-dominat or not*/
{
int result=1;
monomial_node *n;

n=m->head->next;

while(n != NULL)
{
if((n->y).i==i && (n->y).l<0)
{
result=0;
break;
}
n=n->next;
}
return result;
}

monomial reduce(monomial m, int i) /* reduce an i-dominant monomial */
{
monomial n;
monomial_node *a;

a=m->head->next;

n=malloc(sizeof(monomial_node));
n->head=malloc(sizeof(monomial_node));
n->head->next=NULL;


while(a != NULL)
{
if((a->y).i == i)
{
n=insert(a->y, n);
}
a=a->next;
}

return n;
}



monomial minus_monomial(monomial a)
{
monomial b;

b=a;
b->coeff=-(a->coeff);
return b;
}

polynomial minus_polynomial(polynomial a)
{
polynomial b;

b=a;
b=b->head->next;
while(b!=NULL)
{
b->m=minus_monomial(b->m);
b=b->next;
}
return b;
}


polynomial add_polynomials(polynomial a, polynomial b)
{
polynomial c;

c=a;
c=c->head;
while(c->next != NULL)
{
c=c->next;
}
c->next=b->head->next;
c=simplify_polynomial(c);
return c;
}



polynomial subtract_polynomials(polynomial a, polynomial b) /* a-b */
{
polynomial c,d;


c=a;
d=minus_polynomial(b);
c=add_polynomials(c, d);

return c;
}




polynomial subtract_polynomial_monomial(polynomial a, monomial b) /* a-b */
{
polynomial c, d;


c=a;
d=create_polynomial(b);
c=subtract_polynomials(c,d);


return c;
}



monomial multiply_monomials(monomial a, monomial b) /* product of two monomials */
{
monomial n;
monomial_node *c;


n=a;


c=b->head->next;

while(c != NULL)
{
n=insert(c->y, n);
c=c->next;
}

n->coeff = (a->coeff)*(b->coeff);

n=simplify_monomial(n);

return n;
}

polynomial multiply_monomial_polynomial(monomial a, polynomial b) /* product of a monomial a polynomial */
{
polynomial n;
polynomial_node *c;


n=b;
c=n->head->next;

while(c != NULL)
{
c->m=multiply_monomials(a, c->m);
c=c->next;
}
return n;
}


polynomial multiply_polynomials(polynomial a, polynomial b) /* product of two polynomails */
{
polynomial n;
polynomial_node *c;

c=a->head->next;
n=multiply_monomial_polynomial(c->m, b);
c=c->next;


while(c != NULL)
{
n=add_polynomials(n, multiply_monomial_polynomial(c->m, b));
c=c->next;
}

n=simplify_polynomial(n);

return n;
}



int main(int argc, char *argv[])

{



node p;

polynomial FM;

polynomial new_monomials;

polynomial phi[100][1000];


monomial s;

monomial m;

monomial mm[1000];


int type;

int rank;

int l;

int i,j;

int number;

int a;







printf("Please input the number of factors.
");

scanf("%d", &number);



m=create_monomial(number);
printf("m is
");
print_monomial(m);
printf("
");

s=create_monomial(2);
printf("s is
");
print_monomial(s);
printf("
");



FM=create_polynomial(m);
new_monomials=create_polynomial(s);
new_monomials=add_polynomials(new_monomials,FM);

printf("FM is
");
print_polynomial(FM);
printf("
");

printf("new_monomials = s + FM is
");
print_polynomial(new_monomials);
printf("
");




printf("FM - s is
");
print_polynomial(subtract_polynomial_monomial(FM, s));
printf("
");

printf("FM - new_monomials = FM - (s+FM) = s is
");
print_polynomial(subtract_polynomials(FM, new_monomials));
printf("
");


/*
l=0;

while(1)
{
new_monomials=subtract_polynomials(Dm(FM, type, rank, Cartan_matrix),FM);
FM=Dm(FM, type, rank, Cartan_matrix);

while(new_monomials != NULL)
{
for(i=0; i<rank; i++)
{
if(is_dominant(new_monomials, i)==1);
{
l=1;
break;
}
}

if(is_dominant(new_monomials, i)==1);
{
l=1;
break;
}

}

if(l==0);
{

break;
}

}

*/








return 0;

} [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories