问题 B 实验11_10_链表排序

问题 B 实验11_10_链表排序

题目描述
已知一个正整数组成的无序序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后将这个链表进行排序,使得排序后的链表为递增序列。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。在排序的过程中,你可以自己选择排序算法(冒泡排序、选择排序等),但必须是通过修改结点的指针域来进行排序,而不是对结点的数据域进行修改。程序结束后要释放所有节点占据的空间。
输入
一个元素个数未知的正整数序列,以输入“-1”结束,输入“-1”前至少输入一个正整数。
输出
经过排序后的链表,每个元素后有一个空格,注意最后一个元素后只有换行符。
数据最多的测试用例节点数在1000这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
样例输入 Copy

49 38 65 97 76 13 27 49 -1

样例输出 Copy

The new list is:13 27 38 49 49 65 76 97
/* creat the linked list and sort the linked list
@author: CangCheng
@date: 13/3/2021
*/

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

struct node {
    int data;
    int *nextPtr;
};

struct node *creatLinkedList(struct node *headPtr);

void delLinkedList(struct node *headPtr);

void sortLinkedList(struct node *headPtr);

int getNodeNum(struct node *headPtr);

void printList(struct node *headPtr);

int main() {
    struct node *headPtr = NULL;
    headPtr = creatLinkedList(headPtr);
    sortLinkedList(headPtr);
    printList(headPtr);
    delLinkedList(headPtr);
    return 0;
}

//创建有空节点的链表
struct node *creatLinkedList(struct node *headPtr) {
    struct node *newPtr, *currentPtr;
    int num;
    newPtr = malloc(sizeof(struct node));
    newPtr->nextPtr = NULL;
    headPtr = newPtr;
    scanf("%d", &num);
    while (num != -1) {
        newPtr = malloc(sizeof(struct node));
        newPtr->data = num;
        newPtr->nextPtr = NULL;
        if (headPtr->nextPtr == NULL) {
            currentPtr = newPtr;
            headPtr->nextPtr = currentPtr;
        } else {
            currentPtr->nextPtr = newPtr;
            currentPtr = newPtr;
        }
        scanf("%d", &num);
    }
    return headPtr;
}

//删除链表
void delLinkedList(struct node *headPtr) {
    struct node *thisPtr, *thatPtr;
    thisPtr = headPtr;
    while (thisPtr != NULL) {
        thatPtr = thisPtr->nextPtr;
        free(thisPtr);
        thisPtr = thatPtr;
    }
}

//升序排序
void sortLinkedList(struct node *headPtr) {
    struct node *thisPtr, *thatPtr, *lastPtr;
    int nodeNum, flag;
    nodeNum = getNodeNum(headPtr);
    for (int i = 1; i <= nodeNum - 2; i++) {
        lastPtr = headPtr;
        thisPtr = lastPtr->nextPtr;
        thatPtr = thisPtr->nextPtr;
        for (int j = 1; j <= nodeNum - 2; j++) {
            if (thisPtr->data > thatPtr->data) {
                thisPtr->nextPtr = thatPtr->nextPtr;
                thatPtr->nextPtr = thisPtr;
                lastPtr->nextPtr = thatPtr;
            }
            lastPtr = lastPtr->nextPtr;
            thisPtr = lastPtr->nextPtr;
            thatPtr = thisPtr->nextPtr;
        }
    }
}

//得到节点数
int getNodeNum(struct node *headPtr) {
    int num = 0;
    struct node *thisPtr;
    thisPtr = headPtr;
    while (thisPtr != NULL) {
        thisPtr = thisPtr->nextPtr;
        num++;
    }
    return num;
}

//打印排序结果
void printList(struct node *headPtr) {
    struct node *thisPtr;
    thisPtr = headPtr->nextPtr;
    printf("The new list is:");
    while (thisPtr != NULL) {
        if (thisPtr->nextPtr != NULL) {
            printf("%d ", thisPtr->data);
        } else if (thisPtr->nextPtr == NULL) {
            printf("%d\n", thisPtr->data);
        }
        thisPtr = thisPtr->nextPtr;
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值