C合并两个链表

时间:2012-06-06 22:26:42

标签: c linked-list

我正在尝试复制两个链接列表的内容,以便一次复制一个元素(来自每个链接列表)。

所以,如果我有list1 = [1,2,3]list2 = [4,5,6]result = [1,4,2,5,3,6]。 如果一个列表比另一个列表短,则其余节点将附加在结果列表的末尾。

这是我的代码,它有一个小错误:它在最后创建了一个额外的节点(我不想要)。

node *list_copy(node *list1, node *list2) 
{
    node *mylist = newnode(); 
    node *head = mylist;

    while (list1 != NULL || list2 != NULL) {        

        if (list1 != NULL) {
            mylist->data = list1->data;
            mylist->next = newnode();
            mylist = mylist->next;

            list1 = list1->next;
        } 
        if (list2 != NULL) { 
            mylist->data = list2->data;
                mylist->next = newnode();
            mylist = mylist->next;

            list2 = list2->next;
        }

    }
    return head;
}   

如何修改它以使其不创建最后一个节点?

示例输入:

List1 = [1,2,3], List2 = [1,2,3,4,5,6], Result = [1,1,2,2,3,3,4,5,6,0]; 

4 个答案:

答案 0 :(得分:2)

不要在块中进行合并,这会使您的代码难以阅读。在功能上将列表副本分解为另一个函数并调用它两次。

您在循环之前执行newnode,然后在添加当前项之后执行newnode。怎么样......

node *mylist; 
node *head;

while (listItem != null) {

    if(head == null) {        
       mylist = newnode();
       head = myList;
    }
    else {
       mylist->next = newnode();
       mylist = mylist->next;
    }


    mylist->data = listItem->data;    
    listItem = listItem->next;
}

答案 1 :(得分:0)

这应该让你去。请注意,在您的示例中,似乎需要对输出进行排序。您可以通过添加一些额外的测试来处理它作为列表合并操作的一部分,但我会将其作为练习(不确定排序结果是否是您的要求的一部分)。

#include <iostream>
#include <fstream>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <cmath>
#include <complex>
#include <stack>
using namespace std;

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

node *list_copy(node *list1, node *list2) 
{
    node *mylist;
    node *head;

    mylist = NULL;
    while (list1 != NULL || list2 != NULL) {        

        if (mylist == NULL) {
            mylist = new node;
            head = mylist;
        }
        else {
            mylist->next = new node;
            mylist = mylist->next;
        }

        if (list1 != NULL) {
            mylist->data = list1->data;
            list1 = list1->next;
        } 
        else if (list2 != NULL) { 
            mylist->data = list2->data;
            list2 = list2->next;
        }

    }
    return head;
}   

node* addnode(node* list, int val) {
  if (list == NULL)
    list = new node;
  else {
    list->next = new node;
    list = list->next;
  }
  list->data = val;
  return list;
}

int main() {
  node* list1;
  node* head1;
  node* list2;
  node* head2;

  list1 = NULL;
  list2 = NULL;

  list1 = addnode(list1,1);
  head1 = list1;
  list1 = addnode(list1,2);
  list1 = addnode(list1,3);

  list2 = addnode(list2,1);
  head2 = list2;
  list2 = addnode(list2,2);
  list2 = addnode(list2,3);
  list2 = addnode(list2,4);
  list2 = addnode(list2,5);
  list2 = addnode(list2,6);

  node* m = list_copy(head1,head2);
  while(m != NULL) {
    cout<<(m->data)<<endl;
    m = m->next;
  }

  return 0;
}

输出:

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c C:\temp\temp.exe
1
2
3
1
2
3
4
5
6

> Terminated with exit code 0.

答案 2 :(得分:0)

以下是您的代码,几乎没有任何修改。希望这会对你有所帮助

node *list_copy(node *list1, node *list2) 
{
    node *mylist = NULL; 
    node *head = NULL;

    while (list1 != NULL || list2 != NULL) {        

        if (head == NULL)
        {
            mylist = newnode();
            head = mylist;
        }  
        else
        {
            mylist->next = newnode();
            mylist = mylist->next;                 
        } 

        if (list1 != NULL) {
            mylist->data = list1->data;   
            list1 = list1->next;
        } 
        if (list2 != NULL) { 
            mylist->data = list2->data;
            list2 = list2->next;
        }
        mylist->next = NULL;
    }
    return head;
}

答案 3 :(得分:-1)

更新:OP似乎想要副本列表。

#include <stdlib.h>
struct llist * llist_merge_and_dup(struct llist *one, struct llist *two, int (*cmp)(struct llist *l, struct llist *r) );
struct llist *llist_dup(struct llist *org);

struct llist *llist_dup(struct llist *org)
{
struct llist *new;
new = malloc (sizeof *new);
if (!new) return NULL;
memcpy (new, org, sizeof new);
new->next = NULL;
return new;
}

struct llist * llist_merge_and_dup(struct llist *one, struct llist *two, int (*cmp)(struct llist *l, struct llist *r) )
{
struct llist *result, **tail;

for (result=NULL, tail = &result; one && two; tail = &(*tail)->next ) {
        if (cmp(one,two) <=0) { *tail = llist_dup(one); one=one->next; }
        else { *tail = llist_dup(two); two=two->next; }
        }

for( ; one; one = one->next) {
        *tail = llist_dup(one);
        }

for( ; two; two = two->next) {
        *tail = llist_dup(two);
        }
return result;
                                                                                              }