简体   繁体   English

这个链表代码是一个好习惯吗?

[英]is this linkedlist code a good practice?

Hi everyone I am a newbie in C and trying to learn it. 大家好,我是C的新手并试图学习它。 I have a simple query regarding this linkedlist implementation which I found at many places: 我有一个关于这个链表实现的简单查询,我在很多地方发现:

void addNode(node **listhead, int data, int pos){
        if(pos<=0 || pos > length(*listhead)+1){
                printf("Invalid position provided, there are currently %d nodes in the list \n", length(*listhead));
                return;
        }else{
                node *current = *listhead;
                node *newNode = (node*)malloc(sizeof(node));
                if(newNode == NULL){
                        printf("Memory allocation error\n");
                        return;
                }
                newNode->data = data;
                newNode->next = NULL;
                if (current == NULL){
                        *listhead = newNode;
                        return;
                }else{
                        int i = 0;
                        while(current->next != NULL && i < pos-1){
                                ++i;
                                current = current->next;
                        }
                        if(current->next == NULL){
                                current->next = newNode;
                        }
                        if(i == pos-1){
                                newNode->next = current->next;
                                current->next = newNode;
                        }
                }
        }
}




int main(){
        node *head = NULL;
        node **headref = &head;
        addNode(headref, 1, 1);
        addNode(headref, 2, 2);
        addNode(headref, 3, 3);
        printList(head);
        return 0;
    }

my query is here we are creating a pointer to a pointer which is pointing to NULL. 我的查询在这里,我们正在创建一个指针指向NULL的指针。 This code works, however I wanted to know if this is a good practice. 这段代码有效,但我想知道这是不是一个好习惯。 If it is not, how should I create my head pointer and pass its reference to the addNode function. 如果不是,我应该如何创建我的头指针并将其引用传递给addNode函数。

Suggested alternative: 建议的替代方案:

int main() {
  node *head = addNode(NULL, 1, 1);
  node *current = head;
  current = addNode(current, 2, 2);
  current = addNode(current, 3, 3);
  printList(head);
  return 0;
}

In other words: 换一种说法:

1) addNode() becomes a function that takes the current value as a parameter (so it doesn't have to traverse the entire list just to add a new element)... 1)addNode()成为一个函数,它将当前值作为参数(因此它不必遍历整个列表只是为了添加一个新元素)...

2) ... and it returns a pointer to the new node. 2)...并返回指向新节点的指针。

3) This means at ANY point in the program you can access ANY of a) the list head, b) the previous pointer (before "add") and/or c) the next pointer (after add). 3)这意味着在程序的任何一点你都可以访问以下任何一个:a)列表头,b)前一个指针(在“添加”之前)和/或c)下一个指针(在添加之后)。

We pass a double pointer to addNode() for cases, where the headref pointer needs to be updated. 我们将一个双指针传递给addNode()用于需要更新headref指针的情况。 For such cases, with "addNode(headref, 1, 1);", the addNode would most likely be storing the address of the malloced element inside addNode() in the headref. 对于这种情况,使用“addNode(headref,1,1);”,addNode很可能将malloced元素的地址存储在headref中的addNode()内。 If you were to pass headref as a pointer, then after the call headref would continue to point to the address in the main and you would lose the malloced address. 如果你将headref作为指针传递,那么在调用后headref将继续指向main中的地址,你将丢失malloced地址。

For single linked list, it is actually a good practice, which simplifies the addNode implementation. 对于单链表,它实际上是一种很好的做法,它简化了addNode实现。 I guess the call to addNode(node_x, 1, 1) will add a node before node_x . 我想对addNode(node_x, 1, 1)的调用将在node_x之前添加一个节点。 If you pass only the pointer to node_x . 如果只传递指向node_x的指针。 Then the function will need to loop over the entire list and find the node before node_x and modify its pointer to the new constructed node. 然后,函数将需要遍历整个列表并在node_x之前找到节点并修改其指向新构造节点的指针。 While if you pass a pointer to pointer, let's say node** p then the function only needs to assign the address of the new node to *p . 如果你将一个指针传递给指针,让我们说node** p那么该函数只需要将新节点的地址分配给*p

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

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