如何在C中加入两个链接列表?

时间:2013-06-24 07:17:39

标签: linked-list append

我想完成两个链接列表以准备考试 这是我到目前为止所拥有的 1 - 反转链表中的元素 2 - 将list2追加到列表1的末尾

我在课程中得到了关于某个人的反向功能的帮助,并试图评论每一步,以了解发生了什么,但我正在努力。如果你也可以帮助我,那将是梦幻般的

在合并功能中,我总体上感到困惑 我何时使用'&'我什么时候使用'*'?

typedef struct node *list;

typedef struct node {
    int   value;
    list  whateverNextIsCalled;
} node;

// Reverse list
list reverse (list inputList){
    list outputList = NULL;

    while (inputList != NULL) {

       /*
        nodePtr points to the first element in the inputList
        */
        node *nodePtr = inputList;

       /* 
        Make the head pointer of inputList point to the next element
        */
        inputList = inputList->whateverNextIsCalled;

       /*
        ???? help point 1
        */
        nodePtr->whateverNextIsCalled = outputList;

       /*
        ???? help point 2
        */
        outputList = nodePtr;
    }
    return outputList;
}

// Add one list to the end of another
void combine (list list1, list list2){

   /*
    Point to the first value of list1 
    */
    node *current = list1;

   /*
    Find the last node of list1
    */
    while(current->whateverNextIsCalled != NULL) {
        current = current->whateverNextIsCalled;
    }
    //connect the last node of toList and the first node of fromList
    current->whateverNextIsCalled = &list2;

    list1 = current;
}    

1 个答案:

答案 0 :(得分:1)

你可以直接到达第一个链接列表的最后一个,在下一个链接列表的下一个链接列表的开头就是null

我明白为什么你推翻了这个名单