问题是,每次调用函数addNodePos时,指针都是head(在调试器中看到了这一点),它只是创建一个节点列表,该列表指向自身,因为它是一个循环的双链接列表。当传递到NULL函数时,它显示“List is empty.”,因为list也是NULL。一直在想为什么,但还是没有结果。
下面是代码(根据SSCCE删除了多余的代码)

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

struct DoubleList
{
    int id;
    struct DoubleList *next;
    struct DoubleList *prev;
};

void addNodePos(struct DoubleList* head, int value, int position);
void printList (struct DoubleList* head);
//void clearList (struct DoubleList* head);


int main()
{
    int value, position;
    struct DoubleList *list = NULL;

    printf("\nvalue: ");
    scanf("%x", &value);
    printf("position: ");
    scanf("%d", &position);
    addNodePos(list, value, position);

    printf("\nvalue: ");
    scanf("%x", &value);
    printf("position: ");
    scanf("%d", &position);
    addNodePos(list, value, position);


    printList(list);
    //clearList(list);
    return 0;
}


void addNodePos(struct DoubleList* head, int value, int position)
{
    int i;
    struct DoubleList *node;

    if ( (node = malloc (sizeof(struct DoubleList))) != NULL ){
        node->id=value;
        if (head==NULL) {
            // points to itself as it is the only node in a list
            node->next=node;
            node->prev=node;
            head=node;
        } else {
            struct DoubleList *current=head;
            for (i = position; i > 1; i--)
                current=current->next;
            // reassign pointers -- relink nodes
            current->prev->next=node;
            node->prev=current->prev;
            node->next=current;
            current->prev=node;
        }
    }

    printf("Element has been added.\n\n");
}


void printList(struct DoubleList* head)
{
    if (head==NULL)
        printf("\nList is empty.\n\n");
    else {
        struct DoubleList *current=head;
        printf("\nThe list: ");
        do {
            printf("%d", current->id);
            current=current->next;
            if(current != head)
                printf("<->");
        } while(current!=head);
        printf("\n\n");
    }
}

最佳答案

head的地址是按值传递的,因此您的更改仅反映在函数本身中。您必须传递一个指向head地址的指针,以便可以更改该值。

int main() {
   ...
   addNodePos(&list, value, position);
   ...
}

void addNodePos(struct DoubleList** headPtr, int value, int position)
{
    struct DoubleList *head = *headPtr;
    int i;
    struct DoubleList *node;

    if ( (node = malloc (sizeof(struct DoubleList))) != NULL ){
        node->id=value;
        if (head==NULL) {
            // points to itself as it is the only node in a list
            node->next=node;
            node->prev=node;
            head=node;
        } else {
            struct DoubleList *current=head;
            for (i = position; i > 1; i--)
                current=current->next;
            // reassign pointers -- relink nodes
            current->prev->next=node;
            node->prev=current->prev;
            node->next=current;
            current->prev=node;
        }
    }

    printf("Element has been added.\n\n");
}

关于c - 指向结构的指针在每个函数调用时都为NULL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27390458/

10-09 06:38
查看更多