如何在C中对具有多个元素的双向链表进行排序?

拉科

我需要对包含多个元素的双向链表进行排序,我的结构如下:

typedef struct node {
   int code;
   char name[40];
   char job[40];
   int salary;
   struct nodo *next;
   struct nodo *prev;
} node;

详细地说,我必须创建一个接收a的函数,*Head并按其元素salary(从低到高)对元素进行排序,例如

node1->salary = 1000;
node2->salary = 500;
node3->salary = 1500;

该功能必须排序,因此该顺序设置为node2,node1,node3。这是我到目前为止尝试过的:

void sort(node *head) 
{ 
    int swapped, i; 
    node *ptr1; 
    node *lptr = NULL; 

    if (top == NULL) 
        return; 

    do
    { 
        swapped = 0; 
        ptr1 = head; 

        while (head->next != lptr) 
        { 
            if (ptr1->salary > ptr1->next->salary) 
            {  
                // This part actually only changes the positions of the salary
                // I need to change the position of all the elements
                node *temp;
                temp->salary= ptr1->salary;
                ptr1->salary= ptr1->next->salary;
                ptr1->next->salary= temp->salary;
                swapped = 1; 
            } 
            ptr1 = ptr1->next; 
        } 
        lptr = ptr1; 
    } 
    while (swapped); 
} 
SaymoinSam

这是一个示例,同时按升序和降序排序

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

typedef struct node {
   int code;
   char name[40];
   char job[40];
   int salary;
   struct node *next;
   struct node *prev;
} node;

void swapNodes(node *a, node *b) {
  node tmp = *a;
  a->code = b->code;
  strcpy(a->name, b->name);
  strcpy(a->job, b->job);
  a->salary = b->salary;
  b->code = tmp.code;
  strcpy(b->name, tmp.name);
  strcpy(b->job, tmp.job);
  b->salary = tmp.salary;
}

//////////////////////////////////////////////////////////////////////////////////////
// \brief  Sort the linked list in ascending or descending order
// \param  head        The head node of the list
//
// \param  isReversed  Whether to sort in asc or desc, false for asc and true for desc
//////////////////////////////////////////////////////////////////////////////////////
void sort(node *head, bool isReversed) {
  // store the current and the previous nodes
  node *currentNode, *previousNode;
  // store if there still a difference in the list means that we have some nodes not sorted
  bool difference;
  loopAgain:
    // looping again and assuming no more difference
    difference = false;
    currentNode = previousNode = head;
    // loop over the list
    while(currentNode != NULL) {
      currentNode = currentNode->next;
      // check for salary if the current salary is less than the previous and in ascending mode
      // then swap the nodes and the opposite in descending mode
      if(currentNode != NULL && (isReversed ? previousNode->salary < currentNode->salary :
          previousNode->salary > currentNode->salary)) {
        swapNodes(previousNode, currentNode);
        difference = true;
      }
      previousNode = currentNode;
    }
  // go to loop again since there still maybe no sorted nodes yet
  if(difference) {
    goto loopAgain;
  }
}

int main() {
  node n1 = {0, "a", "-", 3500, NULL, NULL},
    n2 = {0, "b", "-", 500, NULL, &n1},
    n3 = {0, "c", "-", 1500, NULL, &n2};
  n1.next = &n2;
  n2.next = &n3;
  printf("Before sort:\n%s)%d\n%s)%d\n%s)%d\n", n1.name, n1.salary, n2.name, n2.salary, n3.name, n3.salary);
  sort(&n1, false);
  printf("After ascending sort:\n%s)%d\n%s)%d\n%s)%d\n", n1.name, n1.salary, n2.name, n2.salary, n3.name, n3.salary);
  sort(&n1, true);
  printf("After descending sort:\n%s)%d\n%s)%d\n%s)%d", n1.name, n1.salary, n2.name, n2.salary, n3.name, n3.salary);
  return 0;
}

输出量

Before sort:
a)3500
b)500
c)1500
After ascending sort:
b)500
c)1500
a)3500
After descending sort:
a)3500
c)1500
b)500

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在C中实现具有多个节点的链表?

c++ - 如何在不交换数据的情况下对C ++中的双向链表进行排序,仅传输(条目)节点

链表:如何对双向链表进行排序?

如何在c中反转双向链表

对具有多个元素的数组进行排序

C中的双向链表排序函数

如何在Spark Scala中对具有5个元素的元组的RDD进行排序?

如何在JavaScript或jQuery中对每个数组记录具有多个对象的数组进行排序

如何在一行中对具有多个数据集的数据进行排序?

如何在双向链表中删除 - Python?

如何在已排序的双向链表中插入字符串数据?

如何在C / C ++中对具有多个相似值的枚举进行switch()?

如何在VBA中对具有多个小数位的数字字符串进行排序

如何在Python中构造具有多个特征的元素

如何在不重复的情况下在双向链表中插入元素?

如何为双向链表编写堆排序?C++

计算有多少“元素”出现在双向链表中

合并链表的排序代码仅对C中的一半元素进行排序

如何在C中的双向链表中搜索名称和删除节点?

如何在C#中的XmlDocument中对元素的子元素进行排序?

如何在C中的链表中找到可能有重复项的最小元素

冒泡对C中的链表进行排序

在C中按升序对链表进行排序

如何在SQL中实现双向排序?

如何对具有不同类型的RecyclerView元素进行排序?

如何对具有相同值的元素进行排序

如何在双向链表中实现擦除方法

如何在 Scala 中创建不可变的双向链表?

如何在双向链表c ++之前添加节点