简体   繁体   English

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

[英]Having trouble with Insert Sort for Linked List. Only prints first value of list when I try to sort the list in ascending order

I am writing a program for one of my classes and am stuck on the section that asks to sort a linked list in ascending order.我正在为我的一个班级编写一个程序,并被困在要求按升序对链表进行排序的部分。 I have tried to sort the list but when I run the function it only prints the first value.我试图对列表进行排序,但是当我运行该函数时,它只打印第一个值。 I know the print function works because I can run it without Insert Sort and it prints fine.我知道打印功能有效,因为我可以在没有插入排序的情况下运行它,并且打印效果很好。

My code is as follows:我的代码如下:

#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;

}

And the linkedList.h file is here:而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

And the test file is here (Although there shouldn't be any error here since this was provided to all students in the class by our instructor):测试文件在这里(虽然这里不应该有任何错误,因为这是我们的老师提供给班上所有学生的):

#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

}

Does anyone have any idea what the problem is here?有谁知道这里的问题是什么? The error is somewhere in listInsertionSort(struct Link* head) but I have tried multiple different combinations with no success.错误出现在 listInsertionSort(struct Link* head) 中,但我尝试了多种不同的组合但没有成功。

The current output is : LL Sort 0-9 mod 4: 1 When it should be: LL Sort 0-9 mod 4: 0 0 0 1 1 1 2 2 3 3当前输出是:LL Sort 0-9 mod 4:1 什么时候应该是:LL Sort 0-9 mod 4: 0 0 0 1 1 1 2 2 3 3

I found the solution for the problem I was having.我找到了解决我遇到的问题的方法。

Instead of returning cur, I was supposed to return head.而不是返回 cur ,我应该返回 head 。 I also changed the swap function so that it did not include swapping values of the links in the list because that was unnecessary and looked messy.我还更改了交换函数,使其不包括列表中链接的交换值,因为这是不必要的并且看起来很混乱。

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

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