繁体   English   中英

C中实现LinkedList

[英]Implementing LinkedList in C

我为我的学校作业实现了一个链表,但是当我尝试打印出一个节点的值时,第一个节点总是打印出 memory 地址,我不明白为什么会这样。

我尝试调试,当我将列表的尾部分配给新节点时,节点的值被打印为 memory 地址。

为什么会这样?

int AppendLinkedList(LinkedListPtr list, int value) {
    LinkedListNodePtr newNode = CreateLinkedListNode(value);
    if (list->head == NULL) {
        list->head = newNode;
        list->tail = newNode;
        
        return 0;
    }
    LinkedListNodePtr tailCopy = list->tail;
    newNode->prev = tailCopy;
    tailCopy->next = newNode;
    list->tail = newNode;
    return 0;
}

LinkedListNode *CreateLinkedListNode(int data) {
    LinkedListNodePtr newNode;
    newNode = (LinkedListNodePtr)malloc(sizeof(LinkedListNode));
    newNode->data = data;
    printf("%d\n", data);
    return newNode;
}

int main() {
    LinkedListPtr list = CreateLinkedList();
    int data = 5;
    AppendLinkedList(list, data);
}

typedef struct ll_node {
    int           data;       // Data this node holds 
    struct ll_node *next;     // next node in list, or NULL
    struct ll_node *prev;     // prev node in list, or NULL
} LinkedListNode, *LinkedListNodePtr;

typedef struct ll_head {
    uint64_t          num_elements;  //  # elements in the list
    LinkedListNodePtr head;  // head of linked list, or NULL if empty
    LinkedListNodePtr tail;  // tail of linked list, or NULL if empty
} *LinkedListPtr;

LinkedListPtr CreateLinkedList() {
    LinkedListPtr list;
    list = (LinkedListPtr)malloc(sizeof(LinkedListPtr));
    if (list == NULL) {
        return NULL;
    }
    return list;
} 

您的代码中存在多个问题:

  • 您没有在CreateLinkedListNode()中将prevnext成员初始化为NULL

  • CreateLinkedList()中的分配大小不正确:您应该使用:

    链表 *list = malloc(sizeof(*list));

并且您应该将成员num_elements初始化为0并将headtail初始化为NULL

  • 定义顺序不对,header文件丢失。

  • AppendLinkedList()不更新num_elements

更一般地说,将指针隐藏在 typedef 后面很容易出错。 使用显式指针语法,代码更具可读性。

这是修改后的版本:

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

typedef struct ll_node {
    int data;                // Data this node holds 
    struct ll_node *next;    // next node in list, or NULL
    struct ll_node *prev;    // prev node in list, or NULL
} LinkedListNode;

typedef struct ll_head {
    uint64_t num_elements;   // # elements in the list
    LinkedListNode *head;    // head of linked list, or NULL if empty
    LinkedListNode *tail;    // tail of linked list, or NULL if empty
} LinkedList;

LinkedList *CreateLinkedList() {
    LinkedList *list = malloc(sizeof(*list));
    if (list != NULL) {
        list->num_elements = 0;
        list->head = NULL;
        list->tail = NULL;
    }
    return list;
} 

LinkedListNode *CreateLinkedListNode(int data) {
    LinkedListNode *newNode = malloc(sizeof(*newNode));
    if (newNode != NULL) {
        newNode->data = data;
        newNode->prev = NULL;
        newNode->next = NULL;
    }
    return newNode;
}

int AppendLinkedList(LinkedList *list, int value) {
    if (list == NULL)
        return -1;

    LinkedListNode *newNode = CreateLinkedListNode(value);
    if (newNode == NULL)
        return -1;

    if (list->head == NULL) {
        list->head = newNode;
    } else {
        LinkedListNode *tail = list->tail;
        newNode->prev = tail;
        tail->next = newNode;
    }
    list->tail = newNode;
    list->num_elements += 1;
    return 0;
}

int main() {
    LinkedList *list = CreateLinkedList();
    int data = 5;
    AppendLinkedList(list, data);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM