Linked list 如何在C中连接两个链表?

Linked list 如何在C中连接两个链表?,linked-list,append,Linked List,Append,我想完成两个链表,为考试做准备 这是我到目前为止所拥有的 1-反转链接列表中的元素 2-将列表2附加到列表1的末尾 在我的课程中,有人向我提供了反向功能方面的帮助,我试图对每个步骤进行注释,以了解正在发生的事情,但我正在努力。如果你也能帮我解决这个问题,那就太棒了 在联合收割机功能中,我只是总体上感到困惑 我什么时候使用“&”和“*” typedef struct node *list; typedef struct node { int value; list what

我想完成两个链表,为考试做准备 这是我到目前为止所拥有的 1-反转链接列表中的元素 2-将列表2附加到列表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;
}    

您只需到达第一个链表的最后一个,在下一个链表中,如果出现空值,只需将下一个链表的开头放进去


我不明白你为什么要颠倒这个列表

我有一个prac考试,我们会被告知写一个反转/追加/添加函数。。。所以我只是在练习,我该怎么写呢?我真的不理解topicsee每个列表如果它不是一个循环列表,那么它的结束节点下一个地址必须为NULL,所以如果你想添加任何列表,那么只需要做一些事情,比如,假设我运行第一个列表的循环,确保你像while(p->next)not(while(p)那样运行循环,在第一种情况下,它将在最后一个节点停止,您可以说p->next=secondList(第二个列表的起始地址),现在如果您像while(p)一样运行循环,然后它将在NULL上停止,所以您将无法在NULL中存储地址,因为它不是一个节点,它将只是一个NULL。同样,如果您要反转,则执行相同的操作,如果它的双链接列表,意味着如果它包含下一个地址以及上一个地址,那么您可以在结束节点处到达,并通过prev addr返回ess,但如果是单链接列表,则必须采用交换逻辑,如果要追加,则必须在追加之前停止一点,并在停止的节点中插入新地址,然后在新节点中添加停止的下一个节点的地址:-)