#include <stdio.h>
#include <windows.h>
typedef int TypeData;
#define NODE_LENGTH sizeof(NODE)
/**定义链表的结构体*/
typedef struct tagNode
{
TypeData tdData;
struct tagNode *plNext;
}NODE;
/*******函数声明****************************/
NODE* createList(TypeData tdInData);
int forEachList(NODE* pstInHead);
int insertListTail(NODE* pstInHead, TypeData tdInData);
int insertListHead(NODE* pstInHead, TypeData tdInData);
int getListLength(NODE* pstInHead);
int delListHead(NODE* pstInHead);
int delListTail(NODE* pstInHead);
int sortList(NODE* pstInHead, int nInFlag);
int clearList(NODE* pstInHead);
/*****************************************
函数功能:创建一个新的链表,带头节点
函数入参:tdInData 头结点的值
返回值: 非NULL 成功
NULL 失败
******************************************/
NODE* createList(TypeData tdInData)
{
NODE* pstNewNode = (NODE*)malloc(NODE_LENGTH);
if (NULL == pstNewNode)
{
printf("createList: malloc failed");
return NULL;
}
pstNewNode->plNext = NULL;
pstNewNode->tdData = tdInData;
return pstNewNode;
}
/*****************************************
函数功能:遍历整个链表,把链表的值都显示出来,
函数入参:pstInHead 链表的头结点
返回值:0 成功
1 失败
******************************************/
int forEachList(NODE* pstInHead)
{
if (NULL == pstInHead)
{
printf("forEachList: the parameter pstInHead is NULL");
return 1;
}
while (NULL != pstInHead->plNext)
{
printf("%d-->", pstInHead->tdData);
pstInHead = pstInHead->plNext;
}
printf("%d\n", pstInHead->tdData);
return 0;
}
/*****************************************
函数功能:在链表的尾部插入数据
函数入参:pstInHead 链表的头结点
tdInData 在尾部插入数据的值
返回值: 0 成功
1 失败
******************************************/
int insertListTail(NODE* pstInHead, TypeData tdInData)
{
if (NULL == pstInHead)
{
printf("insertListTail: the parameter pstInHead is NULL");
return 1;
}
while (NULL != pstInHead->plNext)
{
pstInHead = pstInHead->plNext;
}
NODE* pstNewNode = (NODE*)malloc(NODE_LENGTH);
if (NULL == pstNewNode)
{
printf("insertListTail: malloc pstNewNode failed");
return 1;
}
pstNewNode->plNext = NULL;
pstNewNode->tdData = tdInData;
pstInHead->plNext = pstNewNode;
return 0;
}
/*****************************************
函数功能:在链表的首部插入数据,就是在头结点的后面插入数据
函数入参:pstInHead 链表的头结点
tdInData 在尾部插入数据的值
返回值: 0 成功
1 失败
******************************************/
int insertListHead(NODE* pstInHead, TypeData tdInData)
{
if (NULL == pstInHead)
{
printf("insertListHead: the parameter pstInHead is NULL");
return 1;
}
NODE* pstNewNode = (NODE*)malloc(NODE_LENGTH);
if (NULL == pstNewNode)
{
printf("insertListTail: malloc pstNewNode failed");
return 1;
}
pstNewNode->tdData = tdInData;
/**将该节点插入头部*/
pstNewNode->plNext = pstInHead->plNext;
pstInHead->plNext = pstNewNode;
return 0;
}
/*****************************************
函数功能:删除头结点
函数入参:pstInHead 链表的头结点
返回值: 0 成功
1 失败
******************************************/
int delListHead(NODE* pstInHead)
{
if (NULL == pstInHead)
{
printf("delListHead: the parameter pstInHead is NULL");
return 1;
}
int nListLen = getListLength(pstInHead);
NODE* pstTempNode = NULL;