单链表插入排序(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;

}
posted @   笑着的程序员  阅读(78)  评论(0编辑  收藏  举报  
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示