声明第二个链表将使第一个链表为NULL

时间:2018-08-23 05:14:26

标签: c list

链接列表'firstPoly'在扫描到'secondPoly'时为NULL,我已经以一种怪异的方式修复了该问题,并试图找出原因。

这使第一个Poly元素为空。

int main() {
    Polynomial *firstPoly = NULL, *secondPoly = NULL;
    createPoly(firstPoly);
    createPoly(secondPoly);

    printf("Enter first poly:\n");
    scanPoly(&firstPoly);

    printf("Enter second poly:\n");
    scanPoly(&secondPoly);

    printf("Poly1: ");
    printList(firstPoly);

    printf("\nPoly2: ");
    printList(secondPoly);

    getch();
}

但是这个效果很好。

int main() {
Polynomial *head = NULL, *head2 = NULL, *firstPoly, *secondPoly;

createPoly(&head);
createPoly(&head2);

printf("Enter first poly:\n");
scanPoly(&head);
firstPoly = head;

printf("Enter second poly:\n");
scanPoly(&head2);
secondPoly = head2;

printf("Poly1: ");
printList(firstPoly);

printf("\nPoly2: ");
printList(secondPoly);
getch();
}

扫描功能:

void scanPoly(Polynomial* head)
{
    Polynomial* scanner = head;
    int num, pow;
    scanner->next = scanner->prev = NULL;
    printf("NUM^POW: ");
    scanf("%d%d", &num, &pow);
    while (num != 0 && pow > 0)
    {
        sortedInsert(scanner, newPolyNode(num, pow));
        printf("\nNUM^POW: ");
        scanf("%d%d", &num, &pow);
    }

createPoly:

void createPoly(Polynomial **head) {
(*head) = (Polynomial*)malloc(sizeof(Polynomial));
}

0 个答案:

没有答案