交换双向链接列表

时间:2017-04-25 04:55:10

标签: c linked-list swap doubly-linked-list

我的节目越来越难受。请帮帮我:

int areTheyNeighbours(struct student_record_node* A,struct student_record_node* B) {
    return ( A->next_ == B && B->prev_ == A ) || ( A->prev_ == B && B->next_ == A );
}
void refreshOuterPointers(struct student_record_node*A) {
    if (A->prev_ != NULL)
        A->prev_->next_ = A;

    if (A->next_ != NULL)
        A->next_->prev_ = A;
}
/*Swap function accpects value from sort function and preforms the actual swap*/
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
/*
  student_record *tmp;

  tmp = (*node1)->record_;
  (*node1)->record_ = (*node2)->record_;
  (*node2)->record_ = tmp;*/


  struct student_record_node* A;
  struct student_record_node* B;
  struct student_record_node* temp;
  struct student_record_node* swapperVector[4];
  A = *node1;
  B = *node2;
  if (A == B)
  {return;}
  if (B->next_=A){
    temp=A;
    A=B;
    B=temp;
  }
   swapperVector[0] = A->prev_;
   swapperVector[1] = B->prev_;
   swapperVector[2] = A->next_;
   swapperVector[3] = B->next_;

   if (areTheyNeighbours(A,B)) {
       A->prev_ = swapperVector[2];
       B->prev_ = swapperVector[0];
       A->next_ = swapperVector[3];
       B->next_ = swapperVector[1];
   } else {
       A->prev_ = swapperVector[1];
       B->prev_ = swapperVector[0];
       A->next_ = swapperVector[3];
       B->next_ = swapperVector[2];
   }

   refreshOuterPointers(A);
   refreshOuterPointers(B);


}

我正在尝试交换节点的位置,而不是值。请帮忙。它是一个双重链表。我不确定我哪里出错了。这是一个大学家庭作业。我是新手,请帮忙。

0 个答案:

没有答案