【问题标题】:Having trouble with Insert Sort for Linked List. Only prints first value of list when I try to sort the list in ascending order为链接列表插入排序时遇到问题。当我尝试按升序对列表进行排序时,仅打印列表的第一个值
【发布时间】:2017-02-22 04:44:14
【问题描述】:

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

我的代码如下:

#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

【问题讨论】:

  • 我不认为你的交换功能会起作用,我不知道它怎么会起作用。请阅读指针
  • struct Link* temp = first; temp-&gt;next = first-&gt;next; temp-&gt;value = first-&gt;value; 考虑到 temp = first,你认为这会发生什么?
  • temp-&gt;next = first-&gt;next; 这就像 a = a as temp = first
  • 当你给一个指针赋值时,它们指向同一个“对象”。
  • 测试你的 swap() 实现;它坏了(通常交换需要制作其中一个实体的完整临时副本;您只需复制指向它们的指针)。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    相关资源
    最近更新 更多