两个单向有序链表的归并算法

2014-11-23 17:56:39 · 作者: · 浏览: 36

  //递归算法


  NodePtr merge_lists(NodePtr& one_head,NodePtr& two_head)


  {


  NodePtr p1;


  p1=NULL;


  if(one_head == NULL && two_head == NULL)


  return p1;


  else if(one_head == NULL && two_head != NULL)


  return two_head;


  else if(one_head != NULL && two_head == NULL)


  return one_head;


  else


  {


  if(one_head->data <= two_head->data)


  {


  p1=one_head;


  p1->link = merge_lists(one_head->link,two_head);


  }


  else


  {


  p1=two_head;


  p1->link = merge_lists(one_head,two_head->link);


  }


  return p1;


  }


  }


  //迭代算法