www.pudn.com > 49-LIST.zip > My_List.cpp
#include"My_List.h"
bool My_List_Create(pNODE* List)
{
//产生新节点
(*List) = new NODE;
if(!(*List))
return false;
//设置头节点的后继指向为0
(*List)->_next = 0;
return true;
}
bool My_List_Insert(pNODE* List, unsigned int Postion, MY_TYPE Data)
{
//参数判断
if(Postion < 1 || Postion > My_List_Length(List) + 1)
return false;
//找到需要插入位置的前一个节点
pNODE Tmp = (*List);
for(unsigned int i = 0; i < Postion - 1; ++i)
Tmp = Tmp->_next;
//生成新节点
pNODE NewNode = new NODE;
if(NewNode == 0)
return false;
NewNode->_data = Data;
//插入
if(My_List_Length(List) + 1 == Postion)
{
Tmp->_next = NewNode;
NewNode->_next = 0;
}
else
{
NewNode->_next = Tmp->_next;
Tmp->_next = NewNode;
}
return true;
}
bool My_List_Push(pNODE* List, MY_TYPE Data)
{
return My_List_Insert(List, My_List_Length(List) + 1, Data);
}
bool My_List_Erase(pNODE* List, unsigned int Postion)
{
//参数判断
if(Postion < 1 || Postion > My_List_Length(List))
return false;
//找到需要删除位置的前一个节点
pNODE Tmp = (*List);
for(unsigned int i = 0; i < Postion - 1; ++i)
Tmp = Tmp->_next;
//找到需要删除的节点
pNODE EraseNode = Tmp->_next;
//删除节点
Tmp->_next = EraseNode->_next;
delete EraseNode;
return true;
}
void My_List_Clear(pNODE* List)
{
//得到第一个节点
pNODE Tmp1 = (*List)->_next;
pNODE Tmp2;
while(Tmp1->_next)
{
Tmp2 = Tmp1->_next;
delete Tmp1;
Tmp1 = Tmp2;
}
if(Tmp1)
delete Tmp1;
(*List)->_next = 0;
}
void My_List_Destory(pNODE* List)
{
My_List_Clear(List);
delete (*List);
(*List) = 0;
}
bool My_List_Search(pNODE* List, unsigned int Postion, MY_TYPE* pData)
{
//参数判断
if(Postion < 1 || Postion > My_List_Length(List))
return false;
//找到需要查找位置的节点
pNODE Tmp = (*List);
for(unsigned int i = 0; i < Postion; ++i)
Tmp = Tmp->_next;
//获取数据
*pData = Tmp->_data;
return true;
}
bool My_List_Locate(
pNODE* List,
unsigned int* pPostion,
MY_TYPE Data,
bool (* Equal)(MY_TYPE data1, MY_TYPE data2))
{
bool bResult = false;
pNODE Tmp = (*List);
for(unsigned int i = 0; i < My_List_Length(List); ++i)
{
Tmp = Tmp->_next;
if(Equal(Tmp->_data, Data))
{
*pPostion = i + 1;
bResult = true;
break;
}
}
return bResult;
}
void My_List_Sort(
pNODE* List,
bool (* Bigger)(MY_TYPE data1, MY_TYPE data2),
bool Increase)
{
//定义指向头节点的指针
pNODE Tmp = (*List);
pNODE pNode1, pNode2;
//得到总循环次数
unsigned int uiLength = My_List_Length(List) - 1;
unsigned int uiNum;
MY_TYPE DataTmp;
if(Increase)
{
while(uiLength != 0)
{
//得到每次循环的次数
uiNum = uiLength--;
pNode1 = (*List)->_next;
for(unsigned int i = 0; i < uiNum; ++i)
{
pNode2 = pNode1->_next;
if(Bigger(pNode1->_data, pNode2->_data))
{
DataTmp = pNode1->_data;
pNode1->_data = pNode2->_data;
pNode2->_data = DataTmp;
}
pNode1 = pNode1->_next;
}
}
}
else
{
while(uiLength != 0)
{
//得到每次循环的次数
uiNum = uiLength--;
pNode1 = (*List)->_next;
for(unsigned int i = 0; i < uiNum; ++i)
{
pNode2 = pNode1->_next;
if(Bigger(pNode2->_data, pNode1->_data))
{
DataTmp = pNode1->_data;
pNode1->_data = pNode2->_data;
pNode2->_data = DataTmp;
}
pNode1 = pNode1->_next;
}
}
}
}
unsigned int My_List_Length(pNODE* List)
{
unsigned int uiLength = 0;
//定义指向头节点的指针
pNODE Tmp = (*List);
//获取节点个数
while(Tmp->_next != 0)
{
Tmp = Tmp->_next;
uiLength++;
}
//返回链表长度(节点个数)
return uiLength;
}