具有双向链表的多项式 - 指针问题

问题描述

我用双向链表做了一些多项式代码。例如,如果 你写1和2,那么1是度数,2是系数。 1x^2 插入 到双向链表。问题是当我检查我的代码时,节点 head->degree 正在改变。如果我写 1x^2 那么 head->degree 接下来是 1, 我写 2x^1 然后 head-> degree 应该保持 1 但 head-> degree 改成2我觉得头指针有问题。

#include <stdio.h>
#include <stdlib.h>

// struct 
struct Node {
    int degree;
    int coefficient;
    struct Node* next;
    struct Node* prev;
};


// global variables
int de; // degree
int co; // coefficient
int flag;
Node** head = (Node**)malloc(sizeof(Node)); // 
Node** head1 = (Node**)malloc(sizeof(Node)); // 
Node** head2 = (Node**)malloc(sizeof(Node)); // 
Node** head3 = (Node**)malloc(sizeof(Node)); // 
Node* newNode = (Node*)malloc(sizeof(Node)); //


// function
Node* inputpoly(void);
void printNode(Node* inp);
Node* multiply(Node* a,Node* b);

// main
int main() {
    // head null
    (*head1) = NULL;
    (*head2) = NULL;
    (*head3) = NULL;


    while (1) {
        printf("Input (degree) (coefficient) : ");
        scanf_s("%d %d",&de,&co);
            
        if (de * co < 0) { continue; }
        if (de < 0 && co < 0) {
            printf("Done!\n");
            break;
        }
        *head = inputpoly();
    }
    printNode(*head);
        
    //multiply(*head1,*head2);

    free(head1);
    free(head2);
    free(head3);
    free(newNode);
    free(head);
}


Node* inputpoly(void) {
    // create Node
    newNode->degree = de;
    newNode->coefficient = co;
    newNode->next = NULL;
    newNode->prev = NULL;
    
    // case1 
    if (flag == 0) {
        // list 
        if ((*head1) == NULL) {
            *head1 = newNode;
        }
        // list x
        else {
            Node* horse = (*head1);
            // front of head
            //  ------------------There is some problem
            printf("%d\n",1);
            printf("--%d\n",newNode->degree);
            printf("--%d\n",horse->degree);
            if (horse->degree > newNode->degree) {
                newNode->next = horse;
                horse->prev = newNode;
                *head1 = newNode;
            }
            // barward of head
            else {
                int num = 0;
                while (horse->next != NULL) {
                    
                    horse = horse->next;
                    if (horse->degree > newNode->degree) {
                        horse->prev->next = newNode;
                        newNode->next = horse;
                        newNode->prev = horse->prev;
                        horse->prev = newNode;
                        num = 1;
                        break;
                    }
                }
                // behind tail
                if (num == 0) {
                    horse->next = newNode;
                    newNode->prev = horse;
                }
            }
        }   
        return *head1;
    }

}

void printNode(Node* inp) {
    Node* horse = inp;
    if (horse == NULL) 
    { 
        return;
    }

    while (horse != NULL) {
        if (horse->prev == NULL) {
            if (horse->degree == 1) {
                printf("%d",horse->coefficient);
            }
            else {
                printf("%d x^%d",horse->coefficient,horse->degree);
            }
        }
        else {
            if (horse->degree == 1) {
                printf(" + %d",horse->coefficient);
            }
            else {
                printf(" + %d x^%d",horse->degree);
            }
        }
    }
    printf("\n");
}

解决方法

“我认为存在一些头指针问题,如果我修复了它,我可以解决这个问题。所以我想尽可能地维护这段代码。我想要一些 对我的头部指针的建议或解决方案"

您的示例中发布的代码无法编译:

enter image description here

在修复头指针问题之前,必须先编译代码。此错误列表详细说明了 2 件事:

首先,不能在函数外调用函数,例如:

Node** head = (Node**)malloc(sizeof(Node)); // 
Node** head1 = (Node**)malloc(sizeof(Node)); // 
Node** head2 = (Node**)malloc(sizeof(Node)); // 
Node** head3 = (Node**)malloc(sizeof(Node)); // 
Node* newNode = (Node*)malloc(sizeof(Node)); //

应该从 main(void){...} 或其他一些函数中调用。

其次,每次出现 Node 都应以 struct 开头。例如:

struct Node** head = malloc(sizeof(struct Node *));

(还删除了强制转换,并修改了您为其创建内存的大小,即指针)

而不是解决这些和其他问题,这里有一个双向链表的例子,它可以演示一个简单的工作程序的结构。您可以调整以下内容以满足您的需求:

 struct Node {
    int deg;
    int coef;
    struct Node* next; // Pointer to next node in DLL
    struct Node* prev; // Pointer to previous node in DLL
};

void inputpoly(struct Node** head_ref,int deg,int coef)
{
    //allocate node
    struct Node *new_node = malloc(sizeof(*new_node));
 
    //assign data
    new_node->deg = deg;
    new_node->coef = coef;  
 
    //set next as new head and prev to null
    new_node->next = (*head_ref);
    new_node->prev = NULL;
 
    //change prev of head  to new */
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
 
    //point head to the new node */
    (*head_ref) = new_node;
}

void printList(struct Node* node)
{
    struct Node* last;
    printf("\nread forward\n");
    while (node != NULL) {
        printf(" %d,%d ",node->deg,node->coef);
        last = node;
        node = node->next;
    }
 
    printf("\nread reverse\n");
    while (last != NULL) {
        printf(" %d,last->deg,last->coef);
        last = last->prev;
    }   
}

int main(void)
{
    //start with empty list
    struct Node* head = NULL;
    //create and populate new nodes
    inputpoly(&head,7,2);
    inputpoly(&head,1,4);
    inputpoly(&head,4,6);
    //ouput list

    printList(head);
    getchar();
    return 0;
}

请注意,此代码是作为创建双向链表的基本演示提供的,并说明如何遍历两个方向。由于它不会释放分配的内存,因此不建议将其用于任何生产目的而不解决该遗漏问题。