简体   繁体   English

C 链表分组值

[英]C linked lists grouping values

I have a problem in C linked lists.我在 C 链接列表中遇到问题。 The problem is;问题是; User will enter some inputs.用户将输入一些输入。 And we gonna add to linked list the values.我们将把值添加到链接列表中。 Values are, id,name,surname.值为 id、name、surname。 My job here grouping the same ones digit and sorting them most found.我在这里的工作是将相同的数字分组并对最常找到的数字进行排序。 Like this:像这样:

User entered:用户输入:

47 Max Clark
37 Amy Jhinar
89 Bob Lewis
25 Jackson Adams
29 Jackie Kitcher
27 Karen Robinson

The list should be like this:该列表应该是这样的:

47 Max Clark => 37 Amy Jhinar => 27 Karen Robinson => 89 Bob Lewis => 29 Jackie Kitcher => 25 Jackson Adams

I really don't know how to figure it out.我真的不知道怎么弄明白。 Can someone help me with the algorithm?有人可以帮我算法吗? Thanks a lot.非常感谢。

I am assuming this is model for your current problem.对于您当前的问题,我假设这是 model。 You have an Employee struct like this:你有一个像这样的Employee结构:

typedef struct Employee {
    int id;
    char name[100];
    char surname[100];
} Employee;

Which is a part of your LinkedList node:这是您的 LinkedList 节点的一部分:

typedef struct Node {
    Employee employee;
    struct Node *next;
} Node;

I think you should be able to do it by maintaining an array of frequencies of each Node's id % 10 .我认为您应该能够通过维护每个节点的id % 10的频率数组来做到这一点。 Then slightly customizing logic for your bubble sort method(Just need to compare frequencyMap[employee.id % 10] rather than just comparing ids.然后为您的冒泡排序方法稍微定制逻辑(只需要比较frequencyMap[employee.id % 10]而不仅仅是比较 ids。

Here is sample code:这是示例代码:

void reorderBasedOnFrequency(Node *head) {
    int frequencyMap[10] = { 0 };

    // Build array of frequencies for id mod 10`
    Node *currentNode = head;
    while (currentNode != NULL) {
        frequencyMap[currentNode->employee.id % 10]++;
        currentNode = currentNode->next;
    }

    // Perform Standard Bubble Sort
    bool swapped = true;
    Node *ptr1;
    Node *lptr = NULL;

    do {
        swapped = false;
        ptr1 = head;

        while (ptr1->next != lptr) {
            // Check for frequencies while sorting
            if (frequencyMap[(ptr1)->employee.id % 10] < frequencyMap[(ptr1->next)->employee.id % 10]) {
                // Swap
                swap(ptr1, ptr1->next);
                swapped = true;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    } while (swapped);
}

void swap(struct Node *a, struct Node *b) { 
    Employee temp = a->employee; 
    a->employee = b->employee; 
    b->employee = temp; 
} 

I used it from my main() method like this:我从我的main()方法中使用它,如下所示:

int main() {
    Employee employees[6] = { {47, "Max", "Clark"}, {37, "Amy", "Jhinar"}, {89, "Bob", "Davis"}, {25, "Jackson", "Adams"},
      {29, "Jackie", "Kitcher"},{27, "Karen", "Robinson"}};

    Node *head = NULL;
    Node *tail = NULL;
    for (int i = 0; i < 6; i++) {
        Node *newNode = (Node *)malloc(sizeof(Node));
        newNode->employee = employees[i];
        newNode->next = NULL;
        if (i == 0) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
    }

    printf("Initial List: \n");
    printList(head);
    reorderBasedOnFrequency(head);
    printf("After Sorting: \n");
    printList(head);
}

Here is a sample run based on above code:这是基于上述代码的示例运行:

src : $ gcc linkedlistsortmodulo.c 
src : $ ./a.out 
Initial List: 
47 Max Clark  => 37 Amy Jhinar  => 89 Bob Davis  => 25 Jackson Adams  => 29 Jackie Kitcher  => 27 Karen Robinson 
After Sorting: 
47 Max Clark  => 37 Amy Jhinar  => 27 Karen Robinson  => 89 Bob Davis  => 29 Jackie Kitcher  => 25 Jackson Adams 

Update:更新:

From comments by OP, I noticed that changing Node's data is not allowed.从 OP 的评论中,我注意到不允许更改节点的数据。 Here is a slight variant of above solution based on the Node Swap approach .这是基于节点交换方法的上述解决方案的轻微变体。 Here we just need to pass a Node** so that head gets updated if it's changed during sorting process:在这里我们只需要传递一个Node**以便在排序过程中头部发生变化时更新头部:

void reorderBasedOnFrequency(Node **head) {
  int frequencyMap[10] = { 0 };
    
  // Build array of frequencies for id mod 10`
  Node *currentNode = *head;
  int nCount = 0;
  while (currentNode != NULL) {
    frequencyMap[currentNode->employee.id % 10]++;
    currentNode = currentNode->next;
    nCount++;
  }

  // Perform Standard Bubble Sort
  bool swapped = true;
  Node **h;

  for (int i = 0; i <= nCount && swapped; i++) {
    h = head;
    swapped = false;
    for (int j = 0; j < nCount - i - 1; j++) {
      Node *p1 = *h;
      Node *p2 = p1->next;
      // Check for frequencies while sorting
      if (frequencyMap[p1->employee.id % 10] < frequencyMap[p2->employee.id % 10]) {
        // Swap
        *h = swap(p1, p2);
        swapped = true;
      }

      h = &(*h)->next;
    }
  }
}

Node* swap(struct Node *a, struct Node *b) { 
  Node *tmp = b->next;
  b->next = a;
  a->next = tmp;
  return b;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM