2

我有以下 C 代码:

typedef struct DListNode_ {
    void    *data;
    struct DListNode_ *prev;
    struct DListNode_ *next;
} DListNode;


typedef struct DList_ {
    int size;
    DListNode  *tail;
    DListNode  *head;
} DList;

void insert(DList * list, DListNode * element, int data) {
    DListNode * new_element = (DListNode *)malloc(sizeof(DListNode));
    new_element->data = &data;
    if (list->head==NULL) {
        list->head=list->tail=new_element;
        list->size++;
        return;
    }
    if(element == NULL) {
        // handle size==0?
        new_element->next=list->head;
        list->head->prev=new_element;
        list->head=new_element;
        list->size++;
    } else {
        printf("Not yet implemented!\n");
    }
}

void printNodes(DList *list) {
    DListNode * pointer = list->head;
    if (pointer!=NULL) {
        int v= *((int*)pointer->data);
        printf("Node has value: %d\n", v);
        while (pointer->next != NULL) {
            v = *((int*)pointer->data);
            printf("Node has value: %d\n", v);
            pointer=pointer->next;
        }
    }
}

int main(int argc, const char * argv[])
{

    int e0 = 23;
    int e1 = 7;
    int e2 = 11;
    DList *list = (DList *)malloc(sizeof(DList));
    initList(list);
    assert(count(list)==0);
    insert(list, NULL, e0);
    assert(count(list)==1);

    insert(list,NULL, e1);
    assert(count(list)==2);

    insert(list,NULL, e2);
    assert(count(list)==3);
    printNodes(list);

    return 0;
}

我有几个问题:

  1. DListNode * new_element = (DListNode *)malloc(sizeof(DListNode)); 还为数据、上一个、下一个指针分配空间,还是我需要在每个指针上手动调用 malloc?
  2. 当我在每个节点中打印数据指针的内容时,它们都具有值3,即使我插入 23、7 和 11 并将数据指针设置为 int 的地址:** new_element->data = &data;** .

(已订购 C 入门教材)

编辑:

insert 现在需要一个指向数据的 void 指针:

// Insert data as the new head
void insert(DList *list, DListNode *element, void *data) {
    DListNode *new_element = malloc(sizeof(DListNode));
    new_element->data = data;
    if (list->head==NULL) {
        list->head=list->tail=new_element;
        list->size++;
        return;
    }
    if(element == NULL) {
        new_element->next=list->head;
        list->head->prev=new_element;
        list->head=new_element;
        list->size++;
    } else {
        printf("Not yet implemented!\n");
    }
}

我主要做:

int main(int argc, const char * argv[])
{
    int i0=7;
    int *ip0 = malloc(sizeof(int));
    ip0 = &i0;

    int i1=8;
    int *ip1 = malloc(sizeof(int));
    ip1 = &i1;

    int *ip2 = malloc(sizeof(int));
    int i2=44;
    ip2 = &i2;

    DList *list = malloc(sizeof(DList));
    initList(list);
    // create some nodes
    assert(count(list)==0);
    insert(list, NULL, ip0);
    assert(count(list)==1);

    insert(list,NULL, ip1);
    assert(count(list)==2);

    insert(list,NULL, ip2);
    assert(count(list)==3);
    printNodes(list);

    return 0;
}

输出:

Node has value: 44
Node has value: 44
Node has value: 8

但应该是:

Node has value: 44
Node has value: 8
Node has value: 7
4

2 回答 2

3
  1. malloc(sizeof(DListNode))为 one 分配空间DListNode,根据定义,它由 avoid*和两个DListNode指针组成。但是,它不会初始化这些指针。

  2. 您正在将data参数的地址分配给insert. 这是一个指向临时的指针,一旦insert返回就失效。程序的行为是未定义的。简单的解决方案是替换void *dataint data.

于 2013-05-14T18:22:59.797 回答
0
  1. 您需要手动将这些指针设置为使用 malloc 指向的位置。没有它,它们将指向一个不是 DListNode 大小的空间。

  2. 不要使数据成为指针。只需将数据设为 int (它会自动分配),然后只需设置 data = data (传递给插入的数据)。

于 2013-05-14T18:21:50.907 回答