2018-10-07 17:33:05 +08:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <functional>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CElement;
|
|
|
|
|
/***
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
class CSingleList
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CSingleList();
|
|
|
|
|
~CSingleList();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>..<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĩβ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>طǿ<EFBFBD>ָ<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
CElement* Insert(void* lpData, int iDataSize);
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>..<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>λ<EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>طǿ<EFBFBD>ָ<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
CElement* Insert(CElement* lpElement, void* lpData, int iDataSize);
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief ɾ<EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
void Delete(CElement*);
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
CElement* Begin();
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
CElement* Next();
|
|
|
|
|
/***
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD>β
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
CElement* End();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD>Ƿ<EFBFBD><EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return <EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD>TRUE<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FALSE
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
bool Empty();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD>ת
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
void Reverse();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>TRUEʱ<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>.
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
bool CheckCircle();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD>ϲ<EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
void Merge(CSingleList& lst, std::function<int(void* t1, void* t2)>);
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>K<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
void DeleteLastKth(int k);
|
|
|
|
|
/**
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD>ڵ<EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
CElement* Center();
|
|
|
|
|
private:
|
|
|
|
|
void Insert(CElement* lpNewElement, CElement* lpCurElement, bool bBack = true);
|
|
|
|
|
void Insert(CElement* lpNewElement);
|
|
|
|
|
CElement* Tail();
|
|
|
|
|
|
|
|
|
|
CSingleList(CSingleList const & rhs);
|
|
|
|
|
CSingleList& operator= (CSingleList const& rhs);
|
|
|
|
|
private:
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/**ͷ<><CDB7><EFBFBD><EFBFBD>*/
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CElement* m_lpHead;
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/**<2A>ڱ<EFBFBD>*/
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CElement* m_lpSentinel;
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/**<2A>ս<EFBFBD><D5BD>㣬<EFBFBD><E3A3AC><EFBFBD><EFBFBD>End()<29><><EFBFBD><EFBFBD> */
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CElement* m_lpNull;
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/**<2A><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>. ö<><C3B6>ʱʹ<CAB1><CAB9>. */
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CElement* m_lpCur;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/***
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>.
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
class CElement
|
|
|
|
|
{
|
|
|
|
|
friend class CSingleList;
|
|
|
|
|
protected:
|
|
|
|
|
CElement();
|
|
|
|
|
~CElement();
|
|
|
|
|
public:
|
|
|
|
|
/***
|
2018-10-29 15:44:49 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
*/
|
|
|
|
|
void* GetDataPtr();
|
|
|
|
|
protected:
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/**<2A><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CElement* m_lpNext;
|
|
|
|
|
void* m_lpData;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CreateList(CSingleList& lst)
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
//ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>β
|
2018-10-07 17:33:05 +08:00
|
|
|
|
for(int i=1; i<10;i++)
|
|
|
|
|
{
|
|
|
|
|
int* p = new int();
|
|
|
|
|
*p = i;
|
|
|
|
|
lst.Insert(p, 4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void PrintList(CSingleList& lst)
|
|
|
|
|
{
|
|
|
|
|
CElement* lpElement = lst.Begin();
|
|
|
|
|
while(lpElement != lst.End())
|
|
|
|
|
{
|
|
|
|
|
std::cout<<*((int*)lpElement->GetDataPtr())<<std::endl;
|
|
|
|
|
lpElement = lst.Next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/ö<><C3B6>/ɾ<><C9BE>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CSingleList lst;
|
|
|
|
|
CElement* lpElement = NULL;
|
|
|
|
|
CreateList(lst);
|
|
|
|
|
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
PrintList(lst);
|
|
|
|
|
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>Ԫ<EFBFBD>غ<EFBFBD><D8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
lpElement = lst.Begin();
|
|
|
|
|
while(lpElement != lst.End())
|
|
|
|
|
{
|
|
|
|
|
if(*((int*)lpElement->GetDataPtr()) == 5)
|
|
|
|
|
{
|
|
|
|
|
int* p = new int();
|
|
|
|
|
*p = 55;
|
|
|
|
|
lst.Insert(lpElement,p, 4);
|
|
|
|
|
break;
|
|
|
|
|
}else{
|
|
|
|
|
lpElement = lst.Next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
PrintList(lst);
|
|
|
|
|
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>7<EFBFBD><37>Ԫ<EFBFBD><D4AA>),<2C><>ɾ<EFBFBD><C9BE>ָ<EFBFBD><D6B8>Ԫ<EFBFBD><D4AA>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
lpElement = lst.Begin();
|
|
|
|
|
while(lpElement != lst.End())
|
|
|
|
|
{
|
|
|
|
|
if(*((int*)lpElement->GetDataPtr()) == 7)
|
|
|
|
|
{
|
|
|
|
|
lst.Delete(lpElement);
|
|
|
|
|
break;
|
|
|
|
|
}else{
|
|
|
|
|
lpElement = lst.Next();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
PrintList(lst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout<<"--------------------------"<<std::endl;
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>ת
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CSingleList lst;
|
|
|
|
|
CElement* lpElement = NULL;
|
|
|
|
|
CreateList(lst);
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD><EFBFBD>ת"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
lst.Reverse();
|
|
|
|
|
PrintList(lst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout<<"--------------------------"<<std::endl;
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/// <20><><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CSingleList lst;
|
|
|
|
|
CElement* lpElement = NULL;
|
|
|
|
|
CreateList(lst);
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD><EFBFBD><EFBFBD>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
bool bRet = lst.CheckCircle();
|
|
|
|
|
if(bRet){
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>."<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
}else{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>."<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout<<"--------------------------"<<std::endl;
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CSingleList lst,lst2;
|
|
|
|
|
CElement* lpElement = NULL;
|
2018-10-29 15:44:49 +08:00
|
|
|
|
for(int i=1; i<30;i++)
|
2018-10-07 17:33:05 +08:00
|
|
|
|
{
|
|
|
|
|
int* p = new int();
|
|
|
|
|
*p = i;
|
2018-10-29 15:44:49 +08:00
|
|
|
|
if(i%4){
|
2018-10-07 17:33:05 +08:00
|
|
|
|
lst2.Insert(p, 4);
|
|
|
|
|
}else{
|
|
|
|
|
lst.Insert(p, 4);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
PrintList(lst);
|
|
|
|
|
std::cout<<"......"<<std::endl;
|
|
|
|
|
PrintList(lst2);
|
|
|
|
|
lst.Merge(lst2,[](void* lpT1, void* lpT2) -> int{
|
|
|
|
|
if(*((int*)lpT1) < *((int*)lpT2)){
|
|
|
|
|
return -1;
|
|
|
|
|
}else if(*((int*)lpT1) == *((int*)lpT2)){
|
|
|
|
|
return 0;
|
|
|
|
|
}else if(*((int*)lpT1) > *((int*)lpT2)){
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD>ϲ<EFBFBD>֮<EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>."<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
PrintList(lst);
|
|
|
|
|
}
|
|
|
|
|
std::cout<<"--------------------------"<<std::endl;
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>K<EFBFBD><4B><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>鿴<EFBFBD>м<EFBFBD><D0BC>ڵ<EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CSingleList lst;
|
|
|
|
|
CreateList(lst);
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
lst.DeleteLastKth(0);
|
|
|
|
|
PrintList(lst);
|
|
|
|
|
CElement* lpCenter = lst.Center();
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD>м<EFBFBD><EFBFBD>ڵ<EFBFBD>:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
lst.DeleteLastKth(1);
|
|
|
|
|
PrintList(lst);
|
|
|
|
|
lpCenter = lst.Center();
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD>м<EFBFBD><EFBFBD>ڵ<EFBFBD>:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
lst.DeleteLastKth(3);
|
|
|
|
|
PrintList(lst);
|
|
|
|
|
lpCenter = lst.Center();
|
2018-10-29 15:44:49 +08:00
|
|
|
|
std::cout<<"<EFBFBD>м<EFBFBD><EFBFBD>ڵ<EFBFBD>:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
|
2018-10-07 17:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
std::cin.ignore();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSingleList::CSingleList()
|
|
|
|
|
{
|
|
|
|
|
m_lpHead = new CElement();
|
|
|
|
|
m_lpSentinel = new CElement();
|
|
|
|
|
m_lpNull = new CElement();
|
|
|
|
|
m_lpCur = NULL;
|
|
|
|
|
m_lpHead->m_lpNext = m_lpSentinel;
|
|
|
|
|
}
|
|
|
|
|
CSingleList::~CSingleList()
|
|
|
|
|
{
|
|
|
|
|
if(NULL != m_lpSentinel)
|
|
|
|
|
{
|
|
|
|
|
delete m_lpSentinel;
|
|
|
|
|
m_lpSentinel = NULL;
|
|
|
|
|
}
|
|
|
|
|
if(NULL != m_lpNull)
|
|
|
|
|
{
|
|
|
|
|
delete m_lpNull;
|
|
|
|
|
m_lpNull = NULL;
|
|
|
|
|
}
|
|
|
|
|
if(NULL != m_lpHead)
|
|
|
|
|
{
|
|
|
|
|
delete m_lpHead;
|
|
|
|
|
m_lpHead = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CElement* CSingleList::Insert(void* lpData, int iDataSize)
|
|
|
|
|
{
|
|
|
|
|
CElement* lpNewElement = new CElement();
|
|
|
|
|
if(NULL == lpNewElement)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
lpNewElement->m_lpData = lpData;
|
|
|
|
|
Insert(lpNewElement, Tail());
|
|
|
|
|
return lpNewElement;
|
|
|
|
|
}
|
|
|
|
|
CElement* CSingleList::Insert(CElement* lpElement, void* lpData, int iDataSize)
|
|
|
|
|
{
|
|
|
|
|
if((NULL == lpElement) || (End() == lpElement))
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
CElement* lpNewElement = new CElement();
|
|
|
|
|
if(NULL == lpNewElement)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
lpNewElement->m_lpData = lpData;
|
|
|
|
|
Insert(lpNewElement, lpElement);
|
|
|
|
|
return lpNewElement;
|
|
|
|
|
}
|
|
|
|
|
void CSingleList::Insert(CElement* lpNewElement, CElement* lpCurElement, bool bBack /*= true*/)
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
if(bBack){//<2F><><EFBFBD>뵽ָ<EBB5BD><D6B8>Ԫ<EFBFBD>صĺ<D8B5><C4BA><EFBFBD>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
lpNewElement->m_lpNext = lpCurElement->m_lpNext;
|
|
|
|
|
lpCurElement->m_lpNext = lpNewElement;
|
2018-10-29 15:44:49 +08:00
|
|
|
|
}else{//<2F><><EFBFBD>뵽ָ<EBB5BD><D6B8>Ԫ<EFBFBD>ص<EFBFBD>ǰ<EFBFBD><C7B0>
|
2018-10-07 17:33:05 +08:00
|
|
|
|
CElement* lpIter = m_lpSentinel;
|
|
|
|
|
while(NULL != lpIter)
|
|
|
|
|
{
|
|
|
|
|
if(lpIter->m_lpNext == lpCurElement)
|
|
|
|
|
{
|
|
|
|
|
lpNewElement->m_lpNext = lpIter->m_lpNext;
|
|
|
|
|
lpIter->m_lpNext = lpNewElement;
|
|
|
|
|
break;
|
|
|
|
|
}else{
|
|
|
|
|
lpIter = lpIter->m_lpNext;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSingleList::Delete(CElement* lpElement)
|
|
|
|
|
{
|
|
|
|
|
if((NULL == lpElement) || (End() == lpElement))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CElement* lpCurElement = m_lpHead->m_lpNext;
|
|
|
|
|
while(NULL != lpCurElement->m_lpNext)
|
|
|
|
|
{
|
|
|
|
|
if(lpCurElement->m_lpNext == lpElement)
|
|
|
|
|
{
|
|
|
|
|
lpCurElement->m_lpNext = lpCurElement->m_lpNext->m_lpNext;
|
|
|
|
|
break;
|
|
|
|
|
}else{
|
|
|
|
|
lpCurElement = lpCurElement->m_lpNext;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CElement* CSingleList::Tail()
|
|
|
|
|
{
|
|
|
|
|
CElement* lpCurElement = m_lpHead->m_lpNext;
|
|
|
|
|
while(NULL != lpCurElement->m_lpNext)
|
|
|
|
|
{
|
|
|
|
|
lpCurElement = lpCurElement->m_lpNext;
|
|
|
|
|
}
|
|
|
|
|
return lpCurElement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CElement* CSingleList::Begin()
|
|
|
|
|
{
|
|
|
|
|
m_lpCur = NULL;
|
|
|
|
|
if(NULL == m_lpHead->m_lpNext->m_lpNext)
|
|
|
|
|
{
|
|
|
|
|
m_lpCur = End();
|
|
|
|
|
}else{
|
|
|
|
|
m_lpCur = m_lpHead->m_lpNext->m_lpNext;
|
|
|
|
|
}
|
|
|
|
|
return m_lpCur;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CElement* CSingleList::Next()
|
|
|
|
|
{
|
|
|
|
|
if((NULL == m_lpCur) || (End() == m_lpCur))
|
|
|
|
|
{
|
|
|
|
|
return m_lpCur;
|
|
|
|
|
}
|
|
|
|
|
m_lpCur = m_lpCur->m_lpNext;
|
|
|
|
|
if(NULL == m_lpCur)
|
|
|
|
|
{
|
|
|
|
|
m_lpCur = End();
|
|
|
|
|
}
|
|
|
|
|
return m_lpCur;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CElement* CSingleList::End()
|
|
|
|
|
{
|
|
|
|
|
return m_lpNull;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CSingleList::Empty()
|
|
|
|
|
{
|
|
|
|
|
return Begin() == End();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSingleList::Reverse()
|
|
|
|
|
{
|
|
|
|
|
if(Empty())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CElement* lpPre = NULL;
|
|
|
|
|
CElement* lpTmp = NULL;
|
|
|
|
|
CElement* lpCurElement = m_lpSentinel->m_lpNext;
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
lpTmp = lpCurElement->m_lpNext;
|
|
|
|
|
lpCurElement->m_lpNext = lpPre;
|
|
|
|
|
if(NULL == lpTmp)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
lpPre = lpCurElement;
|
|
|
|
|
lpCurElement = lpTmp;
|
|
|
|
|
}
|
|
|
|
|
m_lpSentinel->m_lpNext = lpCurElement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CSingleList::CheckCircle()
|
|
|
|
|
{
|
|
|
|
|
if(Empty())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
CElement* lpFast = m_lpSentinel->m_lpNext;
|
|
|
|
|
CElement* lpSlow = m_lpSentinel->m_lpNext;
|
|
|
|
|
while ((NULL != lpFast) && (NULL != lpFast->m_lpNext))
|
|
|
|
|
{
|
|
|
|
|
lpFast = lpFast->m_lpNext->m_lpNext;
|
|
|
|
|
lpSlow = lpSlow->m_lpNext;
|
|
|
|
|
if (lpFast == lpSlow)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>ϲ<EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2018-10-07 17:33:05 +08:00
|
|
|
|
void CSingleList::Merge(CSingleList& lst, std::function<int(void* t1, void* t2)> fnCompare)
|
|
|
|
|
{
|
|
|
|
|
CElement* lpL1 = Begin();
|
|
|
|
|
CElement* lpL2 = lst.Begin();
|
|
|
|
|
|
|
|
|
|
if(!fnCompare)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int iRet = 0;
|
|
|
|
|
while((lpL2 != lst.End()))
|
|
|
|
|
{
|
|
|
|
|
if(lpL1 != End())
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷλ<EFBFBD><EFBFBD>
|
|
|
|
|
*
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>1,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>1 <- <EFBFBD><EFBFBD><EFBFBD><EFBFBD>2, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>
|
|
|
|
|
*
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD>е<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD>еĵ<EFBFBD>ǰԪ<EFBFBD>ص<EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>Ԫ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>[A]ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD>еĵ<EFBFBD>ǰԪ<EFBFBD>ز<EFBFBD><EFBFBD>뵽Ԫ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>[A]<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>;
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ĩλ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2018-10-07 17:33:05 +08:00
|
|
|
|
iRet = fnCompare(lpL1->GetDataPtr(), lpL2->GetDataPtr());
|
2018-10-29 15:44:49 +08:00
|
|
|
|
if(iRet < 0){
|
|
|
|
|
lpL1 = Next();
|
|
|
|
|
while(lpL1 != End()){
|
|
|
|
|
iRet = fnCompare(lpL1->GetDataPtr(), lpL2->GetDataPtr());
|
|
|
|
|
if(iRet > 0){
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
lpL1 = Next();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-07 17:33:05 +08:00
|
|
|
|
}else{
|
|
|
|
|
iRet = -1;
|
|
|
|
|
}
|
|
|
|
|
CElement* lpNewElement = new CElement();
|
|
|
|
|
if(NULL != lpNewElement)
|
|
|
|
|
{
|
|
|
|
|
lpNewElement->m_lpData = lpL2->GetDataPtr();
|
|
|
|
|
if(lpL1 != End())
|
|
|
|
|
{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
Insert(lpNewElement,lpL1, iRet < 0);
|
2018-10-07 17:33:05 +08:00
|
|
|
|
}else{
|
2018-10-29 15:44:49 +08:00
|
|
|
|
CElement* lpTail = Tail();
|
2018-10-07 17:33:05 +08:00
|
|
|
|
Insert(lpNewElement,lpTail);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lpL2 = lst.Next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSingleList::DeleteLastKth(int k)
|
|
|
|
|
{
|
|
|
|
|
int i = 1;
|
|
|
|
|
if(k <= 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CElement* lpFast = Begin();
|
|
|
|
|
while((lpFast != End()) && (i < k))
|
|
|
|
|
{
|
|
|
|
|
lpFast = Next();
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
if (lpFast == End())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CElement* lpSlow = Begin();
|
|
|
|
|
CElement* lpPrev = NULL;
|
|
|
|
|
while (NULL != lpFast->m_lpNext)
|
|
|
|
|
{
|
|
|
|
|
lpFast = lpFast->m_lpNext;
|
|
|
|
|
lpPrev = lpSlow;
|
|
|
|
|
lpSlow = Next();
|
|
|
|
|
}
|
|
|
|
|
if(NULL != lpPrev)
|
|
|
|
|
{
|
|
|
|
|
lpPrev->m_lpNext = lpPrev->m_lpNext->m_lpNext;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CElement* CSingleList::Center()
|
|
|
|
|
{
|
|
|
|
|
CElement* lpFast = Begin();
|
|
|
|
|
CElement* lpSlow = lpFast;
|
|
|
|
|
while((NULL != lpFast->m_lpNext) && (NULL != lpFast->m_lpNext->m_lpNext))
|
|
|
|
|
{
|
|
|
|
|
lpFast = lpFast->m_lpNext->m_lpNext;
|
|
|
|
|
lpSlow = lpSlow->m_lpNext;
|
|
|
|
|
}
|
|
|
|
|
return lpSlow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CElement::CElement()
|
|
|
|
|
{
|
|
|
|
|
m_lpNext = NULL;
|
|
|
|
|
m_lpData = NULL;
|
|
|
|
|
}
|
|
|
|
|
CElement::~CElement()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* CElement::GetDataPtr()
|
|
|
|
|
{
|
|
|
|
|
return m_lpData;
|
|
|
|
|
}
|