[英]Bubble sort with Function Comparator in C
我正在嘗試使用函數比較器來實現冒泡排序。 我沒有收到任何錯誤,但沒有得到正確的輸出。 這是我的比較器功能
int ageComparator( struct student_record_node* node1, struct student_record_node* node2 )
{
struct student_record_node *ptr1;
struct student_record_node *lptr;
ptr1=node1;
lptr=node2->next_;
int a=1;
if (ptr1->record_->student_age_>lptr->record_->student_age_&&ptr1->next_ != NULL)
{
//swap(&ptr1, &ptr1->next_);
return a;
}
return 0;
}
這是我的排序功能:
void sort(struct student_record_node **recordsHead, int (*compare_fcn)(struct student_record_node*, struct student_record_node*))
{
int swapped, i;
struct student_record_node *ptr1;
struct student_record_node *lptr = NULL;
do
{
swapped = 0;
ptr1 = *recordsHead;
while (ptr1->next_ != lptr)
{
if (compare_fcn(ptr1,ptr1)>0)
{
swap(&ptr1, &ptr1->next_);
swapped = 1;
}
ptr1 = ptr1->next_;
}
lptr = ptr1;
}
while (swapped);
}
我的交換功能:
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
student_record_node *tmp;
student_record_node *n1=NULL;
student_record_node *n2=NULL;
n1 = *node1;
n2 = *node2;
*tmp->record_= *n1->record_;
*n1->record_= *n2->record_;
*n2->record_ = *tmp->record_;
}
我的輸出:排序之前...
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421758
student age: 15
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421759
student age: 19
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421756
student age: 20
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421758
student age: 16
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421759
student age: 11
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421756
student age: 22
Sorting by age...
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421759
student age: 11
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421759
student age: 11
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421759
student age: 11
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421759
student age: 11
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421759
student age: 11
struct student_record_node:
student first name: Name
student second name: Last Name
student id: 1001421756
student age: 22
通過排序功能后,除我的最后一個以外的所有值都相同。
student_record_node *tmp;
...
*tmp->record_= *n1->record_;
tmp
尚未初始化,因此對*tmp
訪問是未定義的行為。
您的交換功能有錯誤,可以減少。 嘗試這個
這個想法是在喜歡節點的地方交換
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
student_record_node *tmp;
tmp = *node2->next;
*node2->next = *node1->next;
*node1->next = (tmp) ? tmp->next : NULL; // protection for last element
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.