링크 된 목록에서 노드의 위치를 바꿔서 이상을 정렬 함수로 정렬하려고합니다. 이 함수 중 하나에서 논리적 오류가 발생합니다. 프로그램을 실행하면 무한 루프가됩니다.이중 연결 목록을 사용하여 C 스와핑
업데이트 코드
int adjuctNodes(struct student_record_node** n1, struct student_record_node** n2)
{
struct student_record_node* prev_;
struct student_record_node* next_;
return((*n1)->next_==(*n2) && (*n2)->prev_ == (*n1))||
((*n1)->prev_ ==(*n2) && (*n2)->next_ == (*n1));
}
void updateOuterPointer(struct student_record_node** n)
{
struct student_record_node* next_;
struct student_record_node* prev_;
if((*n)->next_!=NULL)
(*n)->prev_->next_=(*n);
if((*n)->next_ !=NULL)
(*n)->next_->prev_=(*n);
}
/*Swaping */
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
struct student_recod_node* prev_;
struct student_recod_node* next_;
struct student_record_node* ptr=(*node1)->next_;
if(adjucntNodes((node1),(node2)))
{
(node1)->prev_=pnode2;
(node2)->prev_=pnode0;
(node1)->next_=pnode3;
(node2)->next_=pnode1;
}else{
(node1)->prev_=pnode1;
(node2)->prev_=pnode0;
(node1)->next_=pnode3;
(node2)->next_=pnode2;
}
updateOuterPointer((node1));
updateOuterPointer((node2));
}
/*Sorting linked list*/
void sort(struct student_record_node**recordsHead,int(*compare_fcn)(struct
student_record_node*,struct student_record_node*))
{
int swapped;
struct student_record_node *ptr1=*recordsHead;
struct student_record_node *lptr = NULL;
if (ptr1 == NULL)
return;
do
{
swapped = 0;
ptr1 = *recordsHead;
while (ptr1->next_ != lptr)
{
if (compare_fcn(ptr1,ptr1->next_))
{
printf("swapping\n");
swap(&ptr1,&ptr1->next_);
if(ptr1==*recordsHead)
{
(*recordsHead)=ptr1->next_;
}
swapped=1;
}
else ptr1 = ptr1->next_;
}
lptr = ptr1;
;
}
while (swapped);
}
추가하지 마십시오 관련이없는 태그를! – Olaf
디버거를 사용해 보셨습니까? –
서식을 수정할 수 있습니까? 게시하기 전에 미리보기가 있습니다. – Angew