链表排序之后输出混乱

问题是:已知一个正整数组成的无序序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后将这个链表进行排序,使得排序后的链表为递增序列。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。在排序的过程中,你可以自己选择排序算法(冒泡排序、选择排序等),但必须是通过修改结点的指针域来进行排序,而不是对结点的数据域进行修改。程序结束后要释放所有节点占据的空间。
输入:49 38 65 97 76 15 -1
自己写的代码输出:The new list is:15 38 76 6952224 6947152 6971424
请问为什么会有这种情况

```c
#include 
#include 
struct node
{
   int num;
   struct node *next;
};
int main()
{
   int n,t=0;
   struct node *head = NULL, *cur = NULL, *last = NULL;
   struct node *newnode = (struct node *)malloc(sizeof(struct node *));
   newnode->next == head;
   scanf("%d", &n);
   while (n != -1)
   {
      cur = (struct node *)malloc(sizeof(struct node));
      cur->num = n;
      if (head == NULL)
      {
         head = cur;
         last = cur;
      }
      else
      {
         last->next = cur;
         last = cur;
      }
      scanf("%d", &n);
      t++;
   }
   cur->next = NULL;
   last = head;
   cur = head->next;
   while (cur)
   {
      if (cur->num >= last->num)
      {
         last->next = cur;
         last = cur;
      }
      else
      {
         struct node *pre = newnode;
         while (pre->next->num < cur->num)
         {
            pre = pre->next;
         }
         last->next = cur->next;
         cur->next = pre->next;
         pre->next = cur;
      }
      cur = last->next;
   }
   printf("The new list is:%d",newnode->next->num);
   cur=newnode->next->next;
   for(int i=0;i1;i++)
   {
      printf(" %d",cur->num);
      cur=cur->next;
   }
   return 0;
}

```


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

struct node {
    int num;
    struct node *next;
};

int main() {
    int n, t = 0;
    struct node *head = NULL, *cur = NULL, *last = NULL;
    struct node *newnode = (struct node *)malloc(sizeof(struct node));
    newnode->next = head;
    scanf("%d", &n);
    while (n != -1) {
        cur = (struct node *)malloc(sizeof(struct node));
        cur->num = n;
        if (head == NULL) {
            head = cur;
            last = cur;
        } else {
            last->next = cur;
            last = cur;
        }
        scanf("%d", &n);
        t++;
    }
    cur->next = NULL;

    last = head;
    cur = head->next;
    while (cur) {
        if (cur->num >= last->num) {
            last->next = cur;
            last = cur;
        } else {
            struct node *pre = newnode;
            while (pre->next && pre->next->num < cur->num) {
                pre = pre->next;
            }
            last->next = cur->next;
            cur->next = pre->next;
            pre->next = cur;
        }
        cur = last->next;
    }

    printf("The new list is: %d", newnode->next->num);
    cur = newnode->next->next;
    for (int i = 0; i < t - 1; i++) {
        printf(" %d", cur->num);
        cur = cur->next;
    }
    printf("\n");

    cur = head;
    while (cur) {
        struct node *tmp = cur;
        cur = cur->next;
        free(tmp);
    }
    free(newnode);

    return 0;
}

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/730989
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:数据处理之异常值处理
  • 除此之外, 这篇博客: 冒泡法排序之过程中的 输出格式: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 在每一行中输出排序过程中对应步骤的中间结果,即每一步后a[0]〜 a[n−1]的值,相邻数字间有一个空格,行末不得有多余空格。