c - 链接列表的插入排序遇到问题。当我尝试按升序对列表进行排序时,仅打印列表的第一个值

标签 c sorting linked-list singly-linked-list insertion-sort

我正在为我的一门类(class)编写一个程序,但被困在询问的部分 按升序对链接列表进行排序。我尝试过对列表进行排序,但是当 我运行该函数,它只打印第一个值。我知道打印功能有效 因为我可以在没有插入排序的情况下运行它并且打印得很好。

我的代码如下:

#include "linkedList.h"
#include <stdlib.h>
#include <stdio.h>


void swap(struct Link *first, struct Link *second){
  struct Link* temp = first;
  temp->next = first->next;
  temp->value = first->value;
  first = second;
  first->next = second->next;
  first->value = second->value;
  second = temp;
  second->next = temp->next;
  second->value = temp->value;
}

struct Link* listInsertionSort(struct Link* head) {

  /*
   * This function should perform an insertion sort on the list whose head is
   * provided as the function's argument, so that the values in the list are
   * sorted in ascending order, starting at the head.
   *
   * The sort should be done without allocating any new Link structs or any
   * other auxiliary data structures.
   *
   * Return a pointer to the new head of the list.
   */

struct Link* cur = head;
cur->next = head->next;
struct Link* count;
for(;cur->next != NULL; cur = cur->next){
  for(count = cur->next; count != NULL; count = count->next){
      if(cur->value < count->value){
        swap(cur, count);
      }
  }
}


return cur;

}

linkedList.h 文件在这里:

#ifndef __LINKEDLIST_H
#define __LINKEDLIST_H

#define TYPE int

/* Single link structure */
struct Link {
  TYPE value;
  struct Link* next;
};

struct Link* listInsertionSort(struct Link* head);
struct Link* reverseList(struct Link* head);
struct Link* reverseListRecursive(struct Link* head);

#endif

测试文件在这里(虽然这里不应该有任何错误 因为这是我们的老师向类所有学生提供的):

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <float.h>

#include "linkedList.h"

struct Link* buildLink(int n, int rev, int mod) {
  struct Link* head = (struct Link*)malloc(sizeof(struct Link));
  struct Link* cur = head;

  for (int i = 0; i < n; i++) {
    if (rev)
      cur->value = n - i - 1; //If rev is 1, creates list from high value to low value
    else
      cur->value = i; //If rev is 0, creates list from low value to high value

    if (mod)
      cur->value = cur->value % mod; //Modifies list so that it increments up to value of mod

    if (i + 1 < n)
      cur->next = (struct Link*)malloc(sizeof(struct Link)); //Creates next link in the array
    else
      cur->next  = 0; //If less than the cap it sets next character to NULL, ending the list
    cur = cur->next; //Sets current link to next link to continue for loop
  }

  return head;
}

void printLL(struct Link* l,char* s) {
  printf("LL %s: ",s);
  while (l != 0) {
    printf("%d ", l->value);
    l = l->next;
  }
  printf("\n");
}

int main() {
  // We aren't practicing good memory management
  //    here....
  struct Link* l = buildLink(10, 0, 4);
  struct Link* r = listInsertionSort(l);
  printLL(r, "Sort 0-9 mod 4"); //This should print 0 0 0 1 1 1 2 2 3 3

}

有谁知道这里出了什么问题吗?错误位于 listInsertionSort(struct Link* head) 中的某个位置,但我尝试了多种不同的组合,但没有成功。

当前输出为:LL Sort 0-9 mod 4: 1 何时应该是:LL Sort 0-9 mod 4: 0 0 0 1 1 1 2 2 3 3

最佳答案

我找到了解决我遇到的问题的方法。

我应该返回 head,而不是返回 cur。我还更改了交换函数,使其不包括列表中链接的交换值,因为这是不必要的并且看起来很困惑。

关于c - 链接列表的插入排序遇到问题。当我尝试按升序对列表进行排序时,仅打印列表的第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40014558/

相关文章:

sorting - Powershell排序非常大的对象集合

c++ - 双向链表复制构造函数和程序崩溃

java - Java中编辑打开的.txt中的文件并替换为新数据的过程

c - TCP 缓冲死锁,如何断开未接收数据的用户?

c - C 中结构的 Malloc

c - 调试 syslinux 看到的引导文件系统环境?

java - 按所有语言的字母升序对国家/地区进行排序

c++ - C++合并排序可视化器

c++ - 插入到链表后,value head value 是最后输入的值。为什么?

c - 第一个字符串输出未显示