📄 list.c
字号:
#include "assert.h"
#define VOS_NULL_PTR 0
typedef struct NODE
{
int data;
struct NODE * next;
}NODE_STRU;
int m_iListLen = 0;
NODE_STRU * m_pstNodeList = VOS_NULL_PTR;
int CreateList(NODE_STRU ** ppstNodeHead, int iListLen)
{
NODE_STRU * pstNode = VOS_NULL_PTR;
NODE_STRU * p = VOS_NULL_PTR;
int iIndex = 0;
int iFreeIndex = 0;
assert(VOS_NULL_PTR != ppstNodeHead && iListLen > 0);
pstNode = (NODE_STRU*)malloc(sizeof(NODE_STRU));
if (VOS_NULL_PTR== pstNode)
{
free(pstNode);
return -1;
}
pstNode->data = 0;
*ppstNodeHead = pstNode;
p = pstNode;
for (iIndex = 1; iIndex < iListLen; iIndex++)
{
pstNode = (NODE_STRU*)malloc(sizeof(NODE_STRU));
if (VOS_NULL_PTR == pstNode)
{
for (iFreeIndex = iIndex - 1 ; iFreeIndex >= 0; iFreeIndex--)
{
(void)DeleteNode(iFreeIndex);
}
return -1;
}
pstNode->data = iIndex;
pstNode->next = VOS_NULL_PTR;
p->next = pstNode;
p = pstNode;
}
return 0;
}
int InsertNode(int iIndex, int data)
{
NODE_STRU * pstNode = VOS_NULL_PTR;
NODE_STRU * pstNextNode = VOS_NULL_PTR;
NODE_STRU * pstNewNode;
int i = 0;
if (VOS_NULL_PTR ==m_pstNodeList || iIndex > m_iListLen- 1)
{
return -1;
}
pstNode = m_pstNodeList;
pstNewNode = (NODE_STRU*)malloc(sizeof(NODE_STRU));
if (VOS_NULL_PTR== pstNewNode)
{
return -1;
}
pstNewNode->data = data;
while (VOS_NULL_PTR != pstNode && i < iIndex)
{
pstNode = pstNode->next;
i++;
}
assert(VOS_NULL_PTR != pstNode);
/* 如果是在尾部节点上插入 */
if (VOS_NULL_PTR == pstNode->next)
{
pstNode->next = pstNewNode;
pstNewNode->next = VOS_NULL_PTR;
m_iListLen++;
return 0;
}
pstNextNode = pstNode->next;
pstNode->next = pstNewNode;
pstNewNode->next = pstNextNode;
m_iListLen++;
return 0;
}
int DeleteNode(int Index)
{
NODE_STRU * pstNode = VOS_NULL_PTR;
NODE_STRU * pstNextNode = VOS_NULL_PTR;
NODE_STRU * pstCureNode = VOS_NULL_PTR;
int i = 0;
if (VOS_NULL_PTR ==m_pstNodeList|| Index > m_iListLen- 1)
{
return -1;
}
pstNode = m_pstNodeList;
/* 找到删除节点前一个节点*/
while (VOS_NULL_PTR != pstNode && i < Index)
{
pstNode = pstNode->next;
i++;
}
assert(VOS_NULL_PTR != pstNode);
pstCureNode = pstNode;
pstNextNode = pstNode->next;
/*没有该节点*/
if (VOS_NULL_PTR == pstNextNode)
{
return -1;
}
pstCureNode->next = pstNextNode->next;
free(pstNextNode);
m_iListLen--;
return 0;
}
void DispNode(void)
{
NODE_STRU * pstNode = VOS_NULL_PTR;
if (VOS_NULL_PTR ==m_pstNodeList)
{
return -1;
}
pstNode = m_pstNodeList;
printf("\r\n LIST:");
while (VOS_NULL_PTR != pstNode)
{
printf("-%d",pstNode->data);
pstNode = pstNode->next;
}
return;
}
void Sort(void)
{
int iI;
int iJ;
int iSwp;
NODE_STRU * pstNode = VOS_NULL_PTR;
NODE_STRU * pstNodeMove = VOS_NULL_PTR;
if (m_iListLen< 2)
{
return;
}
pstNode = m_pstNodeList;
pstNodeMove = pstNode->next;
printf("\r\n m_iListLen:%d\r\n",m_iListLen);
for (iI = 0; iI < m_iListLen - 1; iI++)
{
pstNodeMove = pstNode->next;
for (iJ = iI + 1; iJ < m_iListLen; iJ++)
{
if (pstNode->data < pstNodeMove->data)
{
iSwp = pstNode->data;
pstNode->data = pstNodeMove->data;
pstNodeMove->data = iSwp;
}
pstNodeMove = pstNodeMove->next;
}
pstNode = pstNode->next;
}
return;
}
int main(void)
{
int iRet;
m_iListLen = 10;
if (2 >= 2)
{
printf("xxxxxxxxx\r\n");
}
iRet = CreateList(&m_pstNodeList, m_iListLen);
if (0 != iRet)
{
return iRet;
}
Sort();
DispNode();
/* insert after thered node */
iRet = InsertNode(2, 300);
if (0 != iRet)
{
printf("\r\n LINE: %ld",__LINE__);
}
Sort();
DispNode();
/* insert after head node */
iRet = InsertNode(0, 100);
if (0 != iRet)
{
printf("\r\n LINE: %ld",__LINE__);
}
Sort();
DispNode();
/* insert after rail node */
iRet = InsertNode(11, 1200);
if (0 != iRet)
{
printf("\r\n LINE: %ld",__LINE__);
}
Sort();
DispNode();
/* Delete rail node */
iRet = DeleteNode(11);
if (0 != iRet)
{
printf("\r\n LINE: %ld",__LINE__);
}
Sort();
DispNode();
/* Delete forth node */
iRet = DeleteNode(3);
if (0 != iRet)
{
printf("\r\n LINE: %ld",__LINE__);
}
Sort();
DispNode();
/* Delete one node */
iRet = DeleteNode(0);
if (0 != iRet)
{
printf("\r\n LINE: %ld",__LINE__);
}
Sort();
DispNode();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -