Skip to content

Commit f594e28

Browse files
author
Smallfly
committed
Merge github.com:wangzheng0822/algo
2 parents b57ec49 + 17bbd62 commit f594e28

File tree

126 files changed

+11807
-490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+11807
-490
lines changed

c-cpp/07_linkedlist/SingleList.cpp

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using namespace std;
66

77
class CElement;
88
/***
9-
* @brief 单链表容器
9+
* @brief 单链表容器
1010
*/
1111
class CSingleList
1212
{
@@ -15,61 +15,61 @@ class CSingleList
1515
~CSingleList();
1616

1717
/**
18-
* @brief 插入..链表末尾插入
19-
* @return 成功返回非空指针,否则失败
18+
* @brief 插入..链表末尾插入
19+
* @return 成功返回非空指针,否则失败
2020
*/
2121
CElement* Insert(void* lpData, int iDataSize);
2222
/**
23-
* @brief 插入..链表指定位置插入
24-
* @return 成功返回非空指针,否则失败
23+
* @brief 插入..链表指定位置插入
24+
* @return 成功返回非空指针,否则失败
2525
*/
2626
CElement* Insert(CElement* lpElement, void* lpData, int iDataSize);
2727
/**
28-
* @brief 删除
28+
* @brief 删除
2929
*/
3030
void Delete(CElement*);
3131

3232
/**
33-
* @brief 链首
33+
* @brief 链首
3434
*/
3535
CElement* Begin();
3636
/**
37-
* @brief 下一个元素
37+
* @brief 下一个元素
3838
*/
3939
CElement* Next();
4040
/***
41-
* @brief 链尾
41+
* @brief 链尾
4242
*/
4343
CElement* End();
4444

4545
/**
46-
* @brief 是否是空链表
47-
* @return 空返回TRUE,否则返回FALSE
46+
* @brief 是否是空链表
47+
* @return 空返回TRUE,否则返回FALSE
4848
*/
4949
bool Empty();
5050

5151
/**
52-
* @brief 反转
52+
* @brief 反转
5353
*/
5454
void Reverse();
5555

5656
/**
57-
* @brief 检测环
58-
* @return 返回TRUE时表示链表存在环,否则不存在环.
57+
* @brief 检测环
58+
* @return 返回TRUE时表示链表存在环,否则不存在环.
5959
*/
6060
bool CheckCircle();
6161

6262
/**
63-
* @brief 合并2个有序的链表
63+
* @brief 合并2个有序的链表
6464
*/
6565
void Merge(CSingleList& lst, std::function<int(void* t1, void* t2)>);
6666

6767
/**
68-
* @brief 删除倒数第K个结点
68+
* @brief 删除倒数第K个结点
6969
*/
7070
void DeleteLastKth(int k);
7171
/**
72-
* @brief 求中间节点
72+
* @brief 求中间节点
7373
*/
7474
CElement* Center();
7575
private:
@@ -80,18 +80,18 @@ class CSingleList
8080
CSingleList(CSingleList const & rhs);
8181
CSingleList& operator= (CSingleList const& rhs);
8282
private:
83-
/**头结点*/
83+
/**头结点*/
8484
CElement* m_lpHead;
85-
/**哨兵*/
85+
/**哨兵*/
8686
CElement* m_lpSentinel;
87-
/**空结点,用于End()返回 */
87+
/**空结点,用于End()返回 */
8888
CElement* m_lpNull;
89-
/**当前结点. 枚举时使用. */
89+
/**当前结点. 枚举时使用. */
9090
CElement* m_lpCur;
9191
};
9292

9393
/***
94-
* @brief 单链表结点元素.
94+
* @brief 单链表结点元素.
9595
*/
9696
class CElement
9797
{
@@ -101,19 +101,19 @@ class CElement
101101
~CElement();
102102
public:
103103
/***
104-
* @brief 获取数据指针
104+
* @brief 获取数据指针
105105
*/
106106
void* GetDataPtr();
107107
protected:
108-
/**下一个结点*/
108+
/**下一个结点*/
109109
CElement* m_lpNext;
110110
void* m_lpData;
111111
};
112112

113113

114114
void CreateList(CSingleList& lst)
115115
{
116-
//循环插入元素到链表尾
116+
//循环插入元素到链表尾
117117
for(int i=1; i<10;i++)
118118
{
119119
int* p = new int();
@@ -134,15 +134,15 @@ void PrintList(CSingleList& lst)
134134
int main()
135135
{
136136
{
137-
/// 链表的基本操作,插入/枚举/删除
137+
/// 链表的基本操作,插入/枚举/删除
138138
CSingleList lst;
139139
CElement* lpElement = NULL;
140140
CreateList(lst);
141141

142-
std::cout<<"枚举链表当前的元素"<<std::endl;
142+
std::cout<<"枚举链表当前的元素"<<std::endl;
143143
PrintList(lst);
144144

145-
std::cout<<"查找指定元素,并在指定元素后面插入新元素"<<std::endl;
145+
std::cout<<"查找指定元素,并在指定元素后面插入新元素"<<std::endl;
146146
lpElement = lst.Begin();
147147
while(lpElement != lst.End())
148148
{
@@ -157,10 +157,10 @@ int main()
157157
}
158158
}
159159

160-
std::cout<<"枚举链表当前的元素"<<std::endl;
160+
std::cout<<"枚举链表当前的元素"<<std::endl;
161161
PrintList(lst);
162162

163-
std::cout<<"查找指定元素(数字是7的元素),并删除指定元素"<<std::endl;
163+
std::cout<<"查找指定元素(数字是7的元素),并删除指定元素"<<std::endl;
164164
lpElement = lst.Begin();
165165
while(lpElement != lst.End())
166166
{
@@ -172,52 +172,52 @@ int main()
172172
lpElement = lst.Next();
173173
}
174174
}
175-
std::cout<<"枚举链表当前的元素"<<std::endl;
175+
std::cout<<"枚举链表当前的元素"<<std::endl;
176176
PrintList(lst);
177177
}
178178

179179
std::cout<<"--------------------------"<<std::endl;
180180
{
181-
/// 链表的反转
181+
/// 链表的反转
182182
CSingleList lst;
183183
CElement* lpElement = NULL;
184184
CreateList(lst);
185-
std::cout<<"反转"<<std::endl;
185+
std::cout<<"反转"<<std::endl;
186186
lst.Reverse();
187187
PrintList(lst);
188188
}
189189

190190
std::cout<<"--------------------------"<<std::endl;
191191
{
192-
/// 检测环
192+
/// 检测环
193193
CSingleList lst;
194194
CElement* lpElement = NULL;
195195
CreateList(lst);
196-
std::cout<<"检测环"<<std::endl;
196+
std::cout<<"检测环"<<std::endl;
197197
bool bRet = lst.CheckCircle();
198198
if(bRet){
199-
std::cout<<"存在环."<<std::endl;
199+
std::cout<<"存在环."<<std::endl;
200200
}else{
201-
std::cout<<"不存在环."<<std::endl;
201+
std::cout<<"不存在环."<<std::endl;
202202
}
203203
}
204204

205205
std::cout<<"--------------------------"<<std::endl;
206206
{
207-
/// 有序链表合并
207+
/// 有序链表合并
208208
CSingleList lst,lst2;
209209
CElement* lpElement = NULL;
210-
for(int i=1; i<10;i++)
210+
for(int i=1; i<30;i++)
211211
{
212212
int* p = new int();
213213
*p = i;
214-
if(i%2){
214+
if(i%4){
215215
lst2.Insert(p, 4);
216216
}else{
217217
lst.Insert(p, 4);
218218
}
219219
}
220-
std::cout<<"枚举链表当前的元素"<<std::endl;
220+
std::cout<<"枚举链表当前的元素"<<std::endl;
221221
PrintList(lst);
222222
std::cout<<"......"<<std::endl;
223223
PrintList(lst2);
@@ -231,31 +231,31 @@ int main()
231231
}
232232
return 0;
233233
});
234-
std::cout<<"合并之后,打印当前链表."<<std::endl;
234+
std::cout<<"合并之后,打印当前链表."<<std::endl;
235235
PrintList(lst);
236236
}
237237
std::cout<<"--------------------------"<<std::endl;
238238
{
239-
/// 删除倒数第K个结点,并查看中间节点
239+
/// 删除倒数第K个结点,并查看中间节点
240240
CSingleList lst;
241241
CreateList(lst);
242-
std::cout<<"删除倒数第0个结点"<<std::endl;
242+
std::cout<<"删除倒数第0个结点"<<std::endl;
243243
lst.DeleteLastKth(0);
244244
PrintList(lst);
245245
CElement* lpCenter = lst.Center();
246-
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
246+
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
247247

248-
std::cout<<"删除倒数第1个结点"<<std::endl;
248+
std::cout<<"删除倒数第1个结点"<<std::endl;
249249
lst.DeleteLastKth(1);
250250
PrintList(lst);
251251
lpCenter = lst.Center();
252-
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
252+
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
253253

254-
std::cout<<"删除倒数第3个结点"<<std::endl;
254+
std::cout<<"删除倒数第3个结点"<<std::endl;
255255
lst.DeleteLastKth(3);
256256
PrintList(lst);
257257
lpCenter = lst.Center();
258-
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
258+
std::cout<<"中间节点:"<<*((int*)lpCenter->GetDataPtr())<<std::endl;
259259
}
260260
std::cin.ignore();
261261

@@ -316,10 +316,10 @@ CElement* CSingleList::Insert(CElement* lpElement, void* lpData, int iDataSize)
316316
}
317317
void CSingleList::Insert(CElement* lpNewElement, CElement* lpCurElement, bool bBack /*= true*/)
318318
{
319-
if(bBack){//插入到指定元素的后面
319+
if(bBack){//插入到指定元素的后面
320320
lpNewElement->m_lpNext = lpCurElement->m_lpNext;
321321
lpCurElement->m_lpNext = lpNewElement;
322-
}else{//插入到指定元素的前面
322+
}else{//插入到指定元素的前面
323323
CElement* lpIter = m_lpSentinel;
324324
while(NULL != lpIter)
325325
{
@@ -443,11 +443,13 @@ bool CSingleList::CheckCircle()
443443
return false;
444444
}
445445

446+
/**
447+
* 合并的2个链表必须是有序的
448+
*/
446449
void CSingleList::Merge(CSingleList& lst, std::function<int(void* t1, void* t2)> fnCompare)
447450
{
448451
CElement* lpL1 = Begin();
449452
CElement* lpL2 = lst.Begin();
450-
CElement* lpTail = NULL;
451453

452454
if(!fnCompare)
453455
{
@@ -458,7 +460,26 @@ void CSingleList::Merge(CSingleList& lst, std::function<int(void* t1, void* t2)>
458460
{
459461
if(lpL1 != End())
460462
{
463+
/**
464+
* 查找需要插入的正确位置
465+
*
466+
* 链表1,链表2; 链表1 <- 链表2, 链表2被合并到链表1中
467+
*
468+
* 如果链表1的元素小于链表2中的元素,则循环查找链表1中大于链表2中的当前元素的元素
469+
* 如果在链表1中找到满足上面条件的的元素位置[A]时,则把链表2中的当前元素插入到元素位置[A]的前面;
470+
* 如果在链表1中不存在这个位置则在链表1的末位插入元素
471+
*/
461472
iRet = fnCompare(lpL1->GetDataPtr(), lpL2->GetDataPtr());
473+
if(iRet < 0){
474+
lpL1 = Next();
475+
while(lpL1 != End()){
476+
iRet = fnCompare(lpL1->GetDataPtr(), lpL2->GetDataPtr());
477+
if(iRet > 0){
478+
break;
479+
}
480+
lpL1 = Next();
481+
}
482+
}
462483
}else{
463484
iRet = -1;
464485
}
@@ -468,17 +489,13 @@ void CSingleList::Merge(CSingleList& lst, std::function<int(void* t1, void* t2)>
468489
lpNewElement->m_lpData = lpL2->GetDataPtr();
469490
if(lpL1 != End())
470491
{
471-
Insert(lpNewElement,lpL1, iRet <= 0);
492+
Insert(lpNewElement,lpL1, iRet < 0);
472493
}else{
473-
if(NULL == lpTail)
474-
{
475-
lpTail = Tail();
476-
}
494+
CElement* lpTail = Tail();
477495
Insert(lpNewElement,lpTail);
478496
}
479497
}
480498
lpL2 = lst.Next();
481-
lpL1 = Next();
482499
}
483500
}
484501

0 commit comments

Comments
 (0)