Add files via upload

This commit is contained in:
HuaQiang 2018-10-29 15:44:49 +08:00 committed by GitHub
parent a11f3c1014
commit d4f700befa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ using namespace std;
class CElement;
/***
* @brief
* @brief
*/
class CSingleList
{
@ -15,61 +15,61 @@ public:
~CSingleList();
/**
* @brief ..
* @return ,
* @brief ..
* @return ,
*/
CElement* Insert(void* lpData, int iDataSize);
/**
* @brief ..
* @return ,
* @brief ..
* @return ,
*/
CElement* Insert(CElement* lpElement, void* lpData, int iDataSize);
/**
* @brief
* @brief
*/
void Delete(CElement*);
/**
* @brief
* @brief
*/
CElement* Begin();
/**
* @brief
* @brief
*/
CElement* Next();
/***
* @brief
* @brief
*/
CElement* End();
/**
* @brief
* @return TRUEFALSE
* @brief
* @return TRUEFALSE
*/
bool Empty();
/**
* @brief
* @brief
*/
void Reverse();
/**
* @brief
* @return TRUE时表示链表存在环,.
* @brief
* @return TRUE时表示链表存在环,.
*/
bool CheckCircle();
/**
* @brief 2
* @brief 2
*/
void Merge(CSingleList& lst, std::function<int(void* t1, void* t2)>);
/**
* @brief K个结点
* @brief K个结点
*/
void DeleteLastKth(int k);
/**
* @brief
* @brief
*/
CElement* Center();
private:
@ -80,18 +80,18 @@ private:
CSingleList(CSingleList const & rhs);
CSingleList& operator= (CSingleList const& rhs);
private:
/**头结点*/
/**头结点*/
CElement* m_lpHead;
/**哨兵*/
/**哨兵*/
CElement* m_lpSentinel;
/**空结点用于End()返回 */
/**空结点用于End()返回 */
CElement* m_lpNull;
/**当前结点. 枚举时使用. */
/**当前结点. 枚举时使用. */
CElement* m_lpCur;
};
/***
* @brief .
* @brief .
*/
class CElement
{
@ -101,11 +101,11 @@ protected:
~CElement();
public:
/***
* @brief
* @brief
*/
void* GetDataPtr();
protected:
/**下一个结点*/
/**下一个结点*/
CElement* m_lpNext;
void* m_lpData;
};
@ -113,7 +113,7 @@ protected:
void CreateList(CSingleList& lst)
{
//循环插入元素到链表尾
//循环插入元素到链表尾
for(int i=1; i<10;i++)
{
int* p = new int();
@ -134,15 +134,15 @@ void PrintList(CSingleList& lst)
int main()
{
{
/// 链表的基本操作,插入/枚举/删除
/// 链表的基本操作,插入/枚举/删除
CSingleList lst;
CElement* lpElement = NULL;
CreateList(lst);
std::cout<<"枚举链表当前的元素"<<std::endl;
std::cout<<"枚举链表当前的元素"<<std::endl;
PrintList(lst);
std::cout<<"查找指定元素,并在指定元素后面插入新元素"<<std::endl;
std::cout<<"查找指定元素,并在指定元素后面插入新元素"<<std::endl;
lpElement = lst.Begin();
while(lpElement != lst.End())
{
@ -157,10 +157,10 @@ int main()
}
}
std::cout<<"枚举链表当前的元素"<<std::endl;
std::cout<<"枚举链表当前的元素"<<std::endl;
PrintList(lst);
std::cout<<"查找指定元素(数字是7的元素),并删除指定元素"<<std::endl;
std::cout<<"查找指定元素(数字是7的元素),并删除指定元素"<<std::endl;
lpElement = lst.Begin();
while(lpElement != lst.End())
{
@ -172,52 +172,52 @@ int main()
lpElement = lst.Next();
}
}
std::cout<<"枚举链表当前的元素"<<std::endl;
std::cout<<"枚举链表当前的元素"<<std::endl;
PrintList(lst);
}
std::cout<<"--------------------------"<<std::endl;
{
/// 链表的反转
/// 链表的反转
CSingleList lst;
CElement* lpElement = NULL;
CreateList(lst);
std::cout<<"反转"<<std::endl;
std::cout<<"反转"<<std::endl;
lst.Reverse();
PrintList(lst);
}
std::cout<<"--------------------------"<<std::endl;
{
/// 检测环
/// 检测环
CSingleList lst;
CElement* lpElement = NULL;
CreateList(lst);
std::cout<<"检测环"<<std::endl;
std::cout<<"检测环"<<std::endl;
bool bRet = lst.CheckCircle();
if(bRet){
std::cout<<"存在环."<<std::endl;
std::cout<<"存在环."<<std::endl;
}else{
std::cout<<"不存在环."<<std::endl;
std::cout<<"不存在环."<<std::endl;
}
}
std::cout<<"--------------------------"<<std::endl;
{
/// 有序链表合并
/// 有序链表合并
CSingleList lst,lst2;
CElement* lpElement = NULL;
for(int i=1; i<10;i++)
for(int i=1; i<30;i++)
{
int* p = new int();
*p = i;
if(i%2){
if(i%4){
lst2.Insert(p, 4);
}else{
lst.Insert(p, 4);
}
}
std::cout<<"枚举链表当前的元素"<<std::endl;
std::cout<<"枚举链表当前的元素"<<std::endl;
PrintList(lst);
std::cout<<"......"<<std::endl;
PrintList(lst2);
@ -231,31 +231,31 @@ int main()
}
return 0;
});
std::cout<<"合并之后,打印当前链表."<<std::endl;
std::cout<<"合并之后,打印当前链表."<<std::endl;
PrintList(lst);
}
std::cout<<"--------------------------"<<std::endl;
{
/// 删除倒数第K个结点,并查看中间节点
/// 删除倒数第K个结点,并查看中间节点
CSingleList lst;
CreateList(lst);
std::cout<<"删除倒数第0个结点"<<std::endl;
std::cout<<"删除倒数第0个结点"<<std::endl;
lst.DeleteLastKth(0);
PrintList(lst);
CElement* lpCenter = lst.Center();
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
std::cout<<"删除倒数第1个结点"<<std::endl;
std::cout<<"删除倒数第1个结点"<<std::endl;
lst.DeleteLastKth(1);
PrintList(lst);
lpCenter = lst.Center();
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
std::cout<<"删除倒数第3个结点"<<std::endl;
std::cout<<"删除倒数第3个结点"<<std::endl;
lst.DeleteLastKth(3);
PrintList(lst);
lpCenter = lst.Center();
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
}
std::cin.ignore();
@ -316,10 +316,10 @@ CElement* CSingleList::Insert(CElement* lpElement, void* lpData, int iDataSize)
}
void CSingleList::Insert(CElement* lpNewElement, CElement* lpCurElement, bool bBack /*= true*/)
{
if(bBack){//插入到指定元素的后面
if(bBack){//插入到指定元素的后面
lpNewElement->m_lpNext = lpCurElement->m_lpNext;
lpCurElement->m_lpNext = lpNewElement;
}else{//插入到指定元素的前面
}else{//插入到指定元素的前面
CElement* lpIter = m_lpSentinel;
while(NULL != lpIter)
{
@ -443,11 +443,13 @@ bool CSingleList::CheckCircle()
return false;
}
/**
* 2
*/
void CSingleList::Merge(CSingleList& lst, std::function<int(void* t1, void* t2)> fnCompare)
{
CElement* lpL1 = Begin();
CElement* lpL2 = lst.Begin();
CElement* lpTail = NULL;
if(!fnCompare)
{
@ -458,7 +460,26 @@ void CSingleList::Merge(CSingleList& lst, std::function<int(void* t1, void* t2)>
{
if(lpL1 != End())
{
/**
*
*
* 1,2; 1 <- 2, 21
*
* 1212
* 1[A]2[A];
* 11
*/
iRet = fnCompare(lpL1->GetDataPtr(), lpL2->GetDataPtr());
if(iRet < 0){
lpL1 = Next();
while(lpL1 != End()){
iRet = fnCompare(lpL1->GetDataPtr(), lpL2->GetDataPtr());
if(iRet > 0){
break;
}
lpL1 = Next();
}
}
}else{
iRet = -1;
}
@ -468,17 +489,13 @@ void CSingleList::Merge(CSingleList& lst, std::function<int(void* t1, void* t2)>
lpNewElement->m_lpData = lpL2->GetDataPtr();
if(lpL1 != End())
{
Insert(lpNewElement,lpL1, iRet <= 0);
Insert(lpNewElement,lpL1, iRet < 0);
}else{
if(NULL == lpTail)
{
lpTail = Tail();
}
CElement* lpTail = Tail();
Insert(lpNewElement,lpTail);
}
}
lpL2 = lst.Next();
lpL1 = Next();
}
}