C 链表:将节点从一个列表移动到另一个列表

C 链表:将节点从一个列表移动到另一个列表,c,pointers,linked-list,C,Pointers,Linked List,有两个列表source={3,2,1}和dest={4,5,6,7},其中链表的头指针分别位于3和4中。源中的头节点被删除,数据3被移动到dest列表,并在dest列表中成为新的头节点 所以在第一轮source={2,1}dest={3,4,5,6,7}之后,其中source中的head指向2,dest中的head指向3。最后,我必须使source=NULL和Dest={1,2,3,4,5,6,7}head=>1。我可以通过每次调用下面的move node函数来实现这一点。但当我在循环中运行时,

有两个列表
source={3,2,1}
dest={4,5,6,7}
,其中链表的头指针分别位于3和4中。源中的头节点被删除,数据3被移动到dest列表,并在dest列表中成为新的头节点

所以在第一轮
source={2,1}dest={3,4,5,6,7}
之后,其中source中的head指向2,dest中的head指向3。最后,我必须使
source=NULL和Dest={1,2,3,4,5,6,7}head=>1
。我可以通过每次调用下面的move node函数来实现这一点。但当我在循环中运行时,它会不断循环。这是错误的代码。请告诉我为什么会出现循环问题

     typedef struct node{
int data;
struct node* next;
}Node;

    void push(Node** headRef, int data){
Node* newNode = (Node*) malloc(sizeof(newNode));
newNode->data = data;
newNode->next = *headRef;
*headRef = newNode;
    }
    Node* pushtop(){
Node* head = NULL;
int i;
for(i = 1; i<=3; i++){
push(&head,i);
}
return head; 
    }

    Node* pushbottom(){
Node* head = NULL;
int i;
for(i=7; i>=4; i--){
push(&head,i);
}
return head;
    }

    void moveNode(Node** source,Node** dest){
Node* ptr = *source;
Node* current = NULL;
while(ptr!=NULL){    // here the continuous looping occurs 
    current=ptr;
    current->next = *dest
    *dest = current;    
    *source = ptr->next;
    ptr = ptr->next;
    }
    Node* test = *dest;
    printf("\nthe then moved list is\n\n");
    while(test!=NULL){
        printf("%d\n",test->data);
        test = test->next;
        }
      } 
    int main(){
Node* headA = pushtop();
Node* headB = pushbottom();
moveNode(&headA, &headB);
    return 0;
}
typedef结构节点{
int数据;
结构节点*下一步;
}节点;
无效推送(节点**headRef,int数据){
Node*newNode=(Node*)malloc(sizeof(newNode));
新建节点->数据=数据;
新建节点->下一步=*headRef;
*headRef=newNode;
}
节点*pushtop(){
Node*head=NULL;
int i;
对于(i=1;i=4;i--){
推压(头部,i);
}
回流头;
}
无效移动节点(节点**源,节点**目的){
节点*ptr=*源;
节点*当前=空;
而(ptr!=NULL){//这里会发生连续循环
电流=ptr;
当前->下一步=*dest
*dest=电流;
*source=ptr->next;
ptr=ptr->next;
}
节点*test=*dest;
printf(“\n然后移动的列表是\n\n”);
while(test!=NULL){
printf(“%d\n”,测试->数据);
测试=测试->下一步;
}
} 
int main(){
节点*headA=pushtop();
节点*headB=pushbottom();
移动节点(头A和头B);
返回0;
}

请检查循环部分时移动节点。

答案与@zavg的答案有点不同。稍微调整一下就可以了

Node* ptr = NULL;
Node* current = *source;
while(current != NULL) {    // here the continuous looping occurs 
    ptr = current->next;
    current->next = dest;
    dest = current;     
    current = ptr;  
}
正如我所说的,源指针也应该更改。所以我将粘贴代码。谢谢@zavg的帮助

     while(ptr != NULL) {    // here is the correct code. 
         current = ptr->next;
         ptr->next = *dest;
        *dest = ptr;     
         ptr = current;
        *source = ptr;  
       }

        printf("%p\n",*source);   //it will print Nil.

        Node* test = *dest;
        printf("\nthe then moved list is\n\n");

        while(test!=NULL){
            printf("%d\n",test->data);
            test = test->next;
        }

从逻辑上讲,我的代码必须工作。我错过了可视化导致循环的东西。它在源端和目标端之间循环!你确定那不会崩溃吗?我在这里看到的第一件事是current=NULL;ptr=电流;ptr->next=*dest;----->分割错误费尔南多编辑的伙计!抱歉弄错了,你是怎么把缩进弄得那么糟糕的?在发布代码之前,请正确格式化它。我被分段代码甩了!而且源指针也应该被移动。请帮帮我。谢谢我能给出正确的答案吗。它的工作与调整位。完美!谢谢你,伙计!我意识到我的错误。我使用ptr指针连接目标指针,而不是当前指针!工作如我所料。非常感谢!:)如果你喜欢它,把它标记为答案,并给予它应有的信任:-“@ BKS4LY谢谢你这样的情感反馈:”啊,我没有认为 Dist和源> /Cord>是<代码>节点** /代码>。我刚刚发布了一般的算法思想:)