我正在编写一个 C 程序来表示链表中的多项式。这是我到目前为止所做的。
# include <stdio.h>
# include <stdlib.h>
struct poly
{
float coef;
int exp;
struct poly* next;
};
void make(struct poly**, float, int);
void display(struct poly*);
void add(struct poly*, struct poly*, struct poly**);
int main()
{
struct poly *first, *second, *final;
int expa, expb, i;
float data;
first = second = final = NULL;
printf("Enter maximum exponent for polynomial A ");
scanf("%d", &expa);
printf("Enter data for polynomial A\n");
for(i=0;i<=expa;i++)
{
printf("Enter coefficient for exponent %d ", expa - i);
scanf("%f", &data);
make(&first, data, expa - i);
}
printf("Displaying polynomial A ");
display(first);
printf("Enter maximum exponent for polynomial B ");
scanf("%d", &expb);
printf("Enter data for polynomial B\n");
for(i=0;i<=expb;i++)
{
printf("Enter coefficient for exponent %d ", expb - i);
scanf("%f", &data);
make(&second, data, expb - i);
}
printf("Displaying polynomial B ");
display(second);
printf("Now adding polynomials A and B \n");
add(first, second, &final);
display(final);
return 1;
}
void make(struct poly**head, float coef, int exp)
{
struct poly *new, *temp;
new = (struct poly*)malloc(sizeof(struct poly));
new->coef = coef;
new->exp = exp;
new->next = NULL;
temp = *head;
if(temp == NULL)
{
*head = new;
return;
}
while(temp->next)
temp = temp->next;
temp->next = new;
}
void display(struct poly*head)
{
struct poly*temp = head;
while(temp)
{
printf("%.1fx^%d ", temp->coef, temp->exp);
temp = temp->next;
}
printf("\nExiting display\n");
}
void add(struct poly*first, struct poly*second, struct poly**sum)
{
struct poly* new;
printf("Currently in add");
if(first == NULL && second == NULL)
return;
while(first&&second)
{
if((*sum)==NULL)
{
new = (struct poly*)malloc(sizeof(struct poly));
*sum = new;
}
else
{
new->next = (struct poly*)malloc(sizeof(struct poly));
new = new->next;
}
if(first->exp == second->exp)
{
new->exp = first->exp;
new->coef = first->coef + second->coef;
first = first->next;
second = second ->next;
}
if(first->exp > second->exp)
{
new->exp = first->exp;
new->coef = first->coef;
first = first->next;
}
if(first->exp < second->exp)
{
new->exp = second->exp;
new->coef = second->coef;
second = second->next;
}
new->next = NULL;
}
while(first)
{
new->next = (struct poly*)malloc(sizeof(struct poly));
new->coef = first->coef;
new->exp = first->exp;
new->next = NULL;
first = first->next;
}
while(second)
{
new->next = (struct poly*)malloc(sizeof(struct poly));
new->coef = second->coef;
new->exp = second->exp;
new->next = NULL;
second= second->next;
}
}
I am receiving output:
./PolynomialAdditionLinkedList.out
Enter maximum exponent for polynomial A 2
Enter data for polynomial A
Enter coefficient for exponent 2 1
Enter coefficient for exponent 1 2
Enter coefficient for exponent 0 1
Displaying polynomial A 1.0x^2 2.0x^1 1.0x^0
Exiting display
Enter maximum exponent for polynomial B 2
Enter data for polynomial B
Enter coefficient for exponent 2 1
Enter coefficient for exponent 1 6
Enter coefficient for exponent 0 9
Displaying polynomial B 1.0x^2 6.0x^1 9.0x^0
Exiting display
Now adding polynomials A and B
Segmentation fault (core dumped)
从输出来看,我在下一行中似乎有错误。
add(first, second, &final);
As the output doesn't prints
当前在 add` 中,在它之前发生错误。我相信我没有以任何非法方式修改第一或第二的值?我在哪里犯错了?