如何编织两条链

时间:2019-01-27 04:51:41

标签: c++ doubly-linked-list

我有两条链,可以说链1的头部为1、2、3、4,而链2的头部为5、6、7、8。将其编织应产生的链1的头部为1、5、2。 ,6、3、7、4、8。我的功能当前正在编织,但仅适用于前2个,并且停止。

void Chain::weave(Chain & other) {     
 while (length_ > 0 && other.length_ > -1){
  Node * current = head_->next;
  Node * otherNode = other.head_->next;

  Node * currentNext = current->next;
  Node * currentPrev = current->prev;
  Node * otherNext = otherNode->next;
  Node * otherPrev = otherNode->prev;

  if (current != NULL && otherNode != NULL){
   current->next = otherNode;
   currentNext = otherNode->next;
   currentNext->next = otherNext;
   length_++;
   other.length_--;

}

0 个答案:

没有答案