0
Follow
0
View

C language linked list sort exchange data

yuki1201 注册会员
2023-02-25 23:11

Part of the answer references GPT, GPT_Pro better problem solving
C language list sort exchange data:
To sort char[20] and nt data in a linked list, we first need to define the structure:

{
  char[20] c;
  int n;
}

Then, we can use the bubble sort method to implement the exchange data sort, the specific code is as follows:

void sort(struct data *head) { struct data *p; struct data *q; for(p=head; p->next!=NULL; p=p->next) { for(q=head; q->next!=NULL; q=q->next) { if (q->n > q->next->n) { char temp_c[20]; int temp_n; strcpy(temp_c, q->c); temp_n = q->n; strcpy(q->c, q->next->c); q->n = q->next->n; strcpy(q->next->c, temp_c); q->next->n = temp_n; } } } }
Finally, we can sort the char[20] and nt data in the list by passing the sort function to the header node.
If the answer is helpful, please accept it.

About the Author

Question Info

Publish Time
2023-02-25 23:11
Update Time
2023-02-25 23:11