簡體   English   中英

具有雙向鏈表的多項式 - 指針問題

[英]Polynominal with doubly linked list - pointer problem

我用雙向鏈表做了一些多項式代碼。 例如,如果你寫 1 和 2,那么 1 是度數,2 是系數。 1x^2 插入到雙向鏈表。 問題是當我檢查我的代碼時,Node head->degree 正在改變。 如果我寫 1x^2 然后 head->degree 接下來是 1,我寫 2x^1 然后 head-> degree 應該保持 1 但 head-> degree 變為 2 我認為 head 指針有問題。

#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->coefficient, horse->degree);
            }
        }
    }
    printf("\n");
}

“我認為有一些頭指針問題,如果我解決了這個問題,我可以解決這個問題。所以我想盡可能地維護這段代碼。我想要一些關於頭指針的建議或解決方案”

您的示例中發布的代碼無法編譯:

在此處輸入圖像描述

在修復頭指針問題之前,必須編譯代碼。 此錯誤列表詳細說明了兩件事:

首先,不能在 function 之外調用函數,例如:

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){...}或其他一些 function 中調用。

其次,每次出現的Node都應該在前面加上struct 例如:

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

(還刪除了演員表,並修改了您正在創建 memory 的大小,即指針)

而不是解決這些和其他問題,這里是一個雙向鏈表的例子,它可以展示一個簡單的工作程序的結構。 您可以調整以下內容以滿足您的需求:

 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,%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;
}

請注意,此代碼是作為創建雙向鏈表的基本演示提供的,並說明了如何遍歷兩個方向 因為它不會釋放分配的 memory,所以不建議在不解決該遺漏的情況下將其用於任何生產目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM