使用链表排序

发布于 2024-10-07 23:00:19 字数 1091 浏览 3 评论 0原文

我有两个字段,即 ID 和名称。在链表中插入节点后,我想按 ID 降序对其进行排序。假设不同的人有可能拥有相同的ID。例如,

1001 CHARICE -> 1001 JUSTIN -> 1001 ANNA -> 1000 CHYNA -> 888 MIKEY -> NULL

最终列表应如下所示:

1001 ANNA -> 1001 CHARICE -> 1001 JUSTINE -> 1000 CHYNA -> 888 MIKEY -> NULL

我按升序对具有相同 ID 的名称进行排序,而 ID 按降序排序。这是我的代码:

NODE* insert_std(NODE *head, NODE* std){
     NODE *prev, *cur;
     if(head==NULL) return std; 
     cur = head;
     while (cur != NULL &&  std->ID < cur->ID){  
         prev = cur;
         cur = cur->next;
     }
     if(std->ID == cur->ID){
         while (cur != NULL &&  strcmp(std->name, cur->name)>=0){
             prev = cur;
             cur = cur->next;
         }
     }    
     if (head==cur){
         if(std->ID >= head->ID) {
         std->next = head;
         head = std;
         }
     } else {
         std->next = cur;
         prev->next = std;
     }
     return head;
}

但是没有按照我想要的方式排序。我做错了什么?

I have two fields namely ID and name. Upon inserting a node in a linked list, I would like to sort it by ID in descending order. Assuming that it is possible that different persons can have the same ID. For Example

1001 CHARICE -> 1001 JUSTIN -> 1001 ANNA -> 1000 CHYNA -> 888 MIKEY -> NULL

The final list should look like this:

1001 ANNA -> 1001 CHARICE -> 1001 JUSTINE -> 1000 CHYNA -> 888 MIKEY -> NULL

I sort the names with the same ID in ascending order while the IDs are sorted in descending order. Here is my code:

NODE* insert_std(NODE *head, NODE* std){
     NODE *prev, *cur;
     if(head==NULL) return std; 
     cur = head;
     while (cur != NULL &&  std->ID < cur->ID){  
         prev = cur;
         cur = cur->next;
     }
     if(std->ID == cur->ID){
         while (cur != NULL &&  strcmp(std->name, cur->name)>=0){
             prev = cur;
             cur = cur->next;
         }
     }    
     if (head==cur){
         if(std->ID >= head->ID) {
         std->next = head;
         head = std;
         }
     } else {
         std->next = cur;
         prev->next = std;
     }
     return head;
}

But is is not being sorted the way I want it. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吝吻 2024-10-14 23:00:19

在插入函数中,每当比较两个节点时,首先比较两个 ID。如果一个比另一个小,这会告诉您哪个节点应该排在第一位。如果ID相同,则需要比较名称来决定哪个节点在前。

这相当于修改 std->ID <= cur->IDstd->ID > 。头->ID。如果我是你,我会编写一个辅助函数,它需要两个指向 NODE 的指针(将它们称为 A 和 B),以上述方式比较它们并返回 true如果节点 A 在节点 B 之前。将此函数合并到您的 insert_std 中就很简单了。

In your insertion function, whenever you compare two nodes, compare the two IDs first. If one is smaller than the other, this tells you which node should come first. If the IDs are the same, you need to compare the names to decide which node comes first.

This amounts to modifying std->ID <= cur->ID and std->ID > head->ID. If I were you, I would write a helper function that would take two pointers to NODE (call them A and B), compare them in the manner described above and return true if node A comes before node B. Incorporating this function into your insert_std is then trivial.

草莓味的萝莉 2024-10-14 23:00:19

只需替换 ID 值的比较,并在 ID 值相同时也考虑名称(例如使用 strcmp)。最简单的方法是编写一个单独的函数来比较执行此任务的两个记录。

Simply replace your comparisons on ID values, and take into account the name as well (for example using strcmp) when the ID values are the same. The easiest way is to write a separate function for comparing two records which does this job.

水波映月 2024-10-14 23:00:19

添加 && while 循环中的 std->ID == cur->ID

 while (cur != NULL &&  std->ID == cur->ID && strcmp(std->name, cur->name)>=0)

Add && std->ID == cur->ID on the while loop

 while (cur != NULL &&  std->ID == cur->ID && strcmp(std->name, cur->name)>=0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文