单链表插入排序(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;
}
【华为OD机试真题】可以转到CSDN相关专栏订阅学习:https://blog.csdn.net/weixin_45541762/article/details/129903356
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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】