0
struct node* ShuffleMerge(struct node* a, struct node* b) {
struct node* result;
struct node* recur;
 if (a==NULL) return(b); // see if either list is empty
  else if (b==NULL) return(a);
  else {
  // it turns out to be convenient to do the recursive call first --
  // otherwise a->next and b->next need temporary storage.
  recur = ShuffleMerge(a->next, b->next);
  result = a; // one node from a
  a->next = b; // one from b
  return(result);
  }
}

代码不起作用,无法访问 B 之后的元素...

4

1 回答 1

0

这是一个简单的“纸笔”调试会话,使用我能想到的最简单的非平凡输入:

S(a=1->2, b=3->4)                1->2 2->N 3->4 4->N
recur := S(2, 4)
   recur := S(a=N, b=N)
       return N
   recur := N
   result:= 2
   a     := 2->4                 1->2 2->4 3->4 4->N
recur := N
result:= 1->2->4
a     := 1->3->4                 1->3 2->4 3->4 4->N
return 1->3->4

这是代码应该为这种情况做的吗?如果不是,为什么不呢?

我希望这种技术在将来有用。

于 2012-09-06T15:27:03.427 回答