From d4f700beface6100f58c8a098f092c3e94682316 Mon Sep 17 00:00:00 2001 From: HuaQiang Date: Mon, 29 Oct 2018 15:44:49 +0800 Subject: [PATCH] Add files via upload --- c-cpp/07_linkedlist/SingleList.cpp | 133 ++++++++++++++++------------- 1 file changed, 75 insertions(+), 58 deletions(-) diff --git a/c-cpp/07_linkedlist/SingleList.cpp b/c-cpp/07_linkedlist/SingleList.cpp index a43079c..5f07863 100644 --- a/c-cpp/07_linkedlist/SingleList.cpp +++ b/c-cpp/07_linkedlist/SingleList.cpp @@ -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 空返回TRUE,否则返回FALSE + * @brief Ƿǿ + * @return շTRUE򷵻FALSE */ bool Empty(); /** - * @brief 反转 + * @brief ת */ void Reverse(); /** - * @brief 检测环 - * @return 返回TRUE时表示链表存在环,否则不存在环. + * @brief ⻷ + * @return TRUEʱʾڻ,򲻴ڻ. */ bool CheckCircle(); /** - * @brief 合并2个有序的链表 + * @brief ϲ2 */ void Merge(CSingleList& lst, std::function); /** - * @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<<"枚举链表当前的元素"<GetDataPtr())<GetDataPtr())<GetDataPtr())<GetDataPtr())<GetDataPtr())<GetDataPtr())<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 fnCompare) { CElement* lpL1 = Begin(); CElement* lpL2 = lst.Begin(); - CElement* lpTail = NULL; if(!fnCompare) { @@ -458,7 +460,26 @@ void CSingleList::Merge(CSingleList& lst, std::function { if(lpL1 != End()) { + /** + * Ҫȷλ + * + * 1,2; 1 <- 2, 2ϲ1 + * + * 1ԪС2еԪأѭ1д2еĵǰԪصԪ + * 1ҵĵԪλ[A]ʱ2еĵǰԪز뵽Ԫλ[A]ǰ; + * 1вλ1ĩλԪ + */ 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 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(); } }