/**链表的长度至少为2的时候,才能删除头部*/
if (nListLen >= 2)
{
pstTempNode = pstInHead->plNext;
pstInHead->plNext = pstInHead->plNext->plNext;
free(pstTempNode);
}
return 0;
}
/*****************************************
函数功能:获得链表的长度,包括头结点
函数入参:pstInHead 链表的头结点
返回值: 0 成功
1 失败
******************************************/
int getListLength(NODE* pstInHead)
{
int nLen = 0;
if (NULL == pstInHead)
{
printf("getListLength: the parameter pstInHead is NULL");
return 1;
}
while (NULL != pstInHead)
{
nLen++;
pstInHead = pstInHead->plNext;
}
return nLen;
}
/*****************************************
函数功能:删除链表的尾节点
函数入参:pstInHead 链表的头结点
返回值: 0 成功
1 失败
******************************************/
int delListTail(NODE* pstInHead)
{
if (NULL == pstInHead)
{
printf("delListTail: the parameter pstInHead is NULL");
return 1;
}
int nListLen = getListLength(pstInHead);
/**如果只有头结点直接返回*/
if (1 == nListLen)
{
return 0;
}
while (NULL != pstInHead->plNext->plNext)
{
pstInHead = pstInHead->plNext;
}
NODE* pstTempNode = pstInHead->plNext;
pstInHead->plNext = NULL;
free(pstTempNode);
return 0;
}
/*****************************************
函数功能:对整个链表进行排序,头结点不参与排序
函数入参:pstInHead 链表的头结点
nInFlag 0 降序,1升序
返回值: 0 成功
1 失败
******************************************/
int sortList(NODE* pstInHead, int nInFlag)
{
if (NULL == pstInHead)
{
printf("sortList: the parameter pstInHead is NULL");
return 1;
}
int nListLen = getListLength(pstInHead);
/**如果只有头结点,不用比较,直接返回*/
if (1 == nListLen)
{
return 0;
}
NODE* pstTempNode = NULL;
pstInHead = pstInHead->plNext;
TypeData tdTempData = 0;
/**如果是正序排列*/
if (0 == nInFlag)
{
while (NULL != pstInHead->plNext)
{
pstTempNode = pstInHead->plNext;
while (NULL != pstTempNode)
{
if (pstInHead->tdData > pstTempNode->tdData)
{
tdTempData = pstInHead->tdData;
pstInHead->tdData = pstTempNode->tdData;
pstTempNode->tdData = tdTempData;
}
pstTempNode = pstTempNode->plNext;
}
pstInHead = pstInHead->plNext;
}
}
/**如果是反序排列*/
else if (1 == nInFlag)
{
while (NULL != pstInHead->plNext)
{
pstTempNode = pstInHead->plNext;
while (NULL != pstTempNode)
{
if (pstInHead->tdData < pstTempNode->tdData)
{
tdTempData = pstInHead->tdData;
pstInHead->tdData = pstTempNode->tdData;
pstTempNode->tdData = tdTempData;
}
pstTempNode = pstTempNode->plNext;
}
pstInHead = pstInHead->plNext;
}
}
else
{
printf("You choice is wrong.Please input (0 | 1)\n");
}
C语言单向链表的实现(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://www.heiqu.com/fcc4600961ae1901c79522a2d4d6fd51.html