【问题标题】:Can't sort a linked list无法对链表进行排序
【发布时间】:2020-09-20 15:54:35
【问题描述】:

我正在尝试按升序对列表进行排序。

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


struct city {
    char ISO[3];
    char name[20];
    unsigned long inhabitants;
};
typedef struct city City;

struct listnode {
    City thecity;
    struct listnode *nextPtr;
};
typedef struct listnode ListNode;


ListNode *sortForInhabitants(ListNode *, size_t );

int main(){
    /* stuff to initialize list */


    ListNode sortedlist = *sortForInhabitants(&startPtr,num_elem);


    /* printf the output */
}


ListNode *sortForInhabitants(ListNode *sPtr, size_t num_nodes){
    ListNode *newsortedlist = NULL;
    ListNode *prevsortedPtr;
    ListNode *prevUNsortedPtr = sPtr;
    ListNode *exchangePtr;
    ListNode *currUNsortedPtr = sPtr;
    ListNode *tempPtr;
    for (size_t i=1; i<=num_nodes; ++i){
        exchangePtr = prevUNsortedPtr;
        for (size_t i=1; i<=num_nodes; ++i){
            if (exchangePtr->thecity.inhabitants > currUNsortedPtr->thecity.inhabitants){
                tempPtr = exchangePtr;
                exchangePtr = currUNsortedPtr;
                currUNsortedPtr = tempPtr;
            }
        }
        prevUNsortedPtr = prevUNsortedPtr->nextPtr;

        if (newsortedlist==NULL){
            ListNode *newsortlinkPtr = calloc(1,sizeof(ListNode));
            if (newsortlinkPtr){
                newsortlinkPtr->thecity.inhabitants = exchangePtr->thecity.inhabitants;
                newsortlinkPtr->nextPtr = NULL;
                newsortedlist = newsortlinkPtr;
                prevsortedPtr = newsortlinkPtr;
            } else {
                printf("\nNo memory.\n");
            }
        } else {
            ListNode *newsortlinkPtr = calloc(1,sizeof(ListNode));
            if (newsortlinkPtr){
                newsortlinkPtr->thecity.inhabitants = exchangePtr->thecity.inhabitants;
                newsortlinkPtr->nextPtr = NULL;
                prevsortedPtr->nextPtr = newsortlinkPtr;
                prevsortedPtr = newsortlinkPtr;
            } else {
                printf("\nNo memory.\n");
            }
        }
    }

    return newsortedlist;
}

输出:

Unsorted list:
PA Palermo 673735
ME Messina 236962
CT Catania 313396
TP Trapani 68528
AG Agrigento 59605
RG Ragusa 73500

Sorted list:
  673735
  236962
  313396
  68528
  59605
  73500

为什么?我花了很长时间,但我没有看到努力的效果。

另外,我想知道是否建议创建一个新的 ListNode(就像我在这个排序函数中所做的那样)或者只是使用指针来“调整”以前的 ListNode...什么会更好?

【问题讨论】:

  • 如何打印输出?可能是排序正常,打印错了?
  • @mkrieger1 对于未排序列表和已排序列表,打印功能是相同的,所以我猜不是 printf 造成问题。
  • 你也猜到了程序的其余部分可以正常工作,但事实并非如此。

标签: c sorting data-structures struct linked-list


【解决方案1】:

我假设您正在实施选择排序算法。您的辅助循环有一个非常明显的错误,您错过了。

ListNode *sortForInhabitants(ListNode *sPtr, size_t num_nodes){

  // initial code

  // this loop is for finding the i_th smallest element
  for (size_t i=1; i<=num_nodes; ++i){
    exchangePtr = prevUNsortedPtr;

    // your second loop is wrong. Correct access of the loop is given below.
    // as you have already found (i-1) smallest elements, you are now going
    // to search through the rest of the linked list.
    for (size_t j=i+1; j<=num_nodes; ++j){
      if (exchangePtr->thecity.inhabitants > currUNsortedPtr->thecity.inhabitants){
        tempPtr = exchangePtr;
        exchangePtr = currUNsortedPtr;
        currUNsortedPtr = tempPtr;
    }
  // rest of your code
}

【讨论】:

  • 如果您实际上解释错误是什么以及如何修复它,而不是仅仅显示代码,这个答案会更有用(我不清楚如果您显示的代码是原始代码或固定代码)。
猜你喜欢
  • 2010-10-20
  • 1970-01-01
  • 2021-04-20
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
相关资源
最近更新 更多