单链表插入排序(C++)

单链表插入排序(C++)

本题的难点在于,要给链表创建一个不赋值而只是为了方便插入的头节点
插入函数

ListNode* InsertSort(const int* numbers, int length)
{
    if (numbers == nullptr || length <= 0)
        return nullptr;
    ListNode* pNode = new ListNode();//一个不赋值的链表头
    ListNode* pHead = new ListNode();
    pHead->m_Value = numbers[0];//将数列的第一个值先存储到链表头节点的下一个节点
    pHead->p_Next = nullptr;
    pNode->p_Next = pHead;
    
    for (int i = 1; i < length; ++i)//使用for循环逐个插入
    {
        ListNode* insert = new ListNode();
        insert->m_Value = numbers[i];
        insert->p_Next = nullptr;
        ListNode* pPre = pNode;
        ListNode* pCurrent = pNode->p_Next;//刚开始这里赋值成了pHead,导致对于逆序数列,排序后只输出开头和结尾
        while(insert->m_Value > pCurrent->m_Value  && pCurrent->p_Next != nullptr)
        {
            pPre = pCurrent; 
            pCurrent= pCurrent->p_Next;
                
        }
        if (insert->m_Value <= pCurrent->m_Value)
        {
            
            insert->p_Next = pCurrent;
            pPre->p_Next = insert;
            // std::cout << pCurrent->m_Value << '\n';
        }
        else
        {
            pCurrent->p_Next = insert;
        }
    }
    return pNode;
}

完整代码

#include <iostream>

struct ListNode
{
    int m_Value;
    ListNode* p_Next;
};

ListNode* InsertSort(const int* numbers, int length)
{
    if (numbers == nullptr || length <= 0)
        return nullptr;
    ListNode* pNode = new ListNode();//一个不赋值的链表头
    ListNode* pHead = new ListNode();
    pHead->m_Value = numbers[0];//将数列的第一个值先存储到链表头节点的下一个节点
    pHead->p_Next = nullptr;
    pNode->p_Next = pHead;
    
    for (int i = 1; i < length; ++i)//使用for循环逐个插入
    {
        ListNode* insert = new ListNode();
        insert->m_Value = numbers[i];
        insert->p_Next = nullptr;
        ListNode* pPre = pNode;
        ListNode* pCurrent = pNode->p_Next;//刚开始这里赋值成了pHead,导致对于逆序数列,排序后只输出开头和结尾
        while(insert->m_Value > pCurrent->m_Value  && pCurrent->p_Next != nullptr)
        {
            pPre = pCurrent; 
            pCurrent= pCurrent->p_Next;
                
        }
        if (insert->m_Value <= pCurrent->m_Value)
        {
            
            insert->p_Next = pCurrent;
            pPre->p_Next = insert;
            // std::cout << pCurrent->m_Value << '\n';
        }
        else
        {
            pCurrent->p_Next = insert;
        }
    }
    return pNode;
}

void DispList(ListNode* phead)
{
    ListNode* pNode = phead;
    while(pNode)
    {
        std::cout << pNode->m_Value << " ";
        pNode = pNode->p_Next;
    }
    std::cout << std::endl;
}

void DestroyList(ListNode* phead)
{
    ListNode* pNode = phead;
    while (pNode)
    {
        phead = phead->p_Next;
        delete pNode;
        pNode = phead;
    }
}
void Test(const char* testName, const int* number, int length)
{
    if (testName != nullptr)
        std::cout << testName << " begins:\n";
    ListNode* head = InsertSort(number, length);
    if (head)
    {
        DispList(head->p_Next);//因为头节点没有存储数据,所以从下一个节点开始打印
        DestroyList(head);
    }    
    else
        std::cout << "It is nullptr!\n"; 
}

void Test1()
{
    int num[] {2,6,3,1,8};
    Test("Test1",num,sizeof(num)/sizeof(int));
}

void Test2()
{
    Test("Test2",nullptr,3);
}
void Test3()
{
    int num[] {9,7,5,3,1};
    Test("Test3",num,sizeof(num)/sizeof(int));
}
void Test4()
{
    int num[] {8};
    Test("Test4",num,sizeof(num)/sizeof(int));
}

void Test5()
{
    int num[] {1,3,5,7,9};
    Test("Test5",num,sizeof(num)/sizeof(int));
}

void Test6()
{
    int num[] {1,7,5,7,9};
    Test("Test6",num,sizeof(num)/sizeof(int));
}

void Test7()
{
    int num[] {9,7,5};
    Test("Test7",num,sizeof(num)/sizeof(int));
}
int main()
{
    Test1();
    Test2();   
    Test3();   
    Test4();   
    Test5();   
    Test6();   
    Test7();   
    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑着的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值