0

一度に 1 つの要素 (各リンク リストから) がコピーされるように、2 つのリンク リストの内容を 1 つにコピーしようとしています。

したがって、私が持っている場合: 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

4 に答える 4

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;
}
于 2012-06-07T01:45:33.703 に答える
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.
于 2012-06-06T22:31:57.597 に答える
-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;
                                                                                              }
于 2012-11-04T21:56:34.013 に答える