Skip to content

Commit bdb7440

Browse files
单循环链表的实现
1 parent 935ace5 commit bdb7440

File tree

8 files changed

+513
-11
lines changed

8 files changed

+513
-11
lines changed

da-hua-shu-ju-jie-gou/03-list/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,39 @@ int StaticLinkListLength(StaticLinkList space) {
475475

476476

477477

478+
## 循环链表
479+
**将单链表中终端节点的指针端由空指针改为指向头结点, 就使整个单链表形成一个环, 这种头尾相接的单链表称为循环连链表(circular linked list)**
478480

481+
### 循环链表的合并
482+
```c
483+
/**
484+
合并两个单循环链表
485+
486+
@param list 主单循环链表
487+
@param anotherList 从单循环链表
488+
@return 操作是否成功
489+
*/
490+
Status UnionCircularLinkList(CircularLinkList *list, CircularLinkList *anotherList) {
491+
492+
CircularLinkList rearA = (*list);
493+
CircularLinkList rearB = (*anotherList);
494+
495+
while (rearA->next != *list) {
496+
rearA = rearA->next;
497+
}
498+
while (rearB->next != *anotherList) {
499+
rearB = rearB->next;
500+
}
501+
502+
rearA->next = (*anotherList)->next;
503+
rearB->next = (*list);
504+
505+
free((*anotherList));
506+
*anotherList = NULL;
507+
508+
return OK;
509+
}
510+
```
479511
480512
481513

da-hua-shu-ju-jie-gou/03-list/list/list.xcodeproj/project.pbxproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
DA48D10821EE11D6006A240B /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = DA48D10721EE11D6006A240B /* main.c */; };
1212
DA48D11021EE193A006A240B /* SqList.c in Sources */ = {isa = PBXBuildFile; fileRef = DA48D10F21EE193A006A240B /* SqList.c */; };
1313
DA67B4AF21EF7AFA002D3947 /* LinkList.c in Sources */ = {isa = PBXBuildFile; fileRef = DA67B4AE21EF7AFA002D3947 /* LinkList.c */; };
14+
DA8512712220DE23007A250C /* CircularList.c in Sources */ = {isa = PBXBuildFile; fileRef = DA8512702220DE23007A250C /* CircularList.c */; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXCopyFilesBuildPhase section */
@@ -35,6 +36,8 @@
3536
DA67B4AB21EF75A4002D3947 /* List.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = List.h; sourceTree = "<group>"; };
3637
DA67B4AD21EF7AFA002D3947 /* LinkList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LinkList.h; sourceTree = "<group>"; };
3738
DA67B4AE21EF7AFA002D3947 /* LinkList.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = LinkList.c; sourceTree = "<group>"; };
39+
DA85126F2220DE23007A250C /* CircularList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CircularList.h; sourceTree = "<group>"; };
40+
DA8512702220DE23007A250C /* CircularList.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = CircularList.c; sourceTree = "<group>"; };
3841
/* End PBXFileReference section */
3942

4043
/* Begin PBXFrameworksBuildPhase section */
@@ -80,6 +83,7 @@
8083
DA48D11121EE1956006A240B /* sqList */,
8184
DA67B4AC21EF7AEE002D3947 /* linkList */,
8285
DA054119221AD6BC004519F5 /* staticList */,
86+
DA85126E2220DE08007A250C /* cicularList */,
8387
DA48D10721EE11D6006A240B /* main.c */,
8488
);
8589
path = list;
@@ -103,6 +107,15 @@
103107
path = linkList;
104108
sourceTree = "<group>";
105109
};
110+
DA85126E2220DE08007A250C /* cicularList */ = {
111+
isa = PBXGroup;
112+
children = (
113+
DA85126F2220DE23007A250C /* CircularList.h */,
114+
DA8512702220DE23007A250C /* CircularList.c */,
115+
);
116+
path = cicularList;
117+
sourceTree = "<group>";
118+
};
106119
/* End PBXGroup section */
107120

108121
/* Begin PBXNativeTarget section */
@@ -160,6 +173,7 @@
160173
buildActionMask = 2147483647;
161174
files = (
162175
DA48D10821EE11D6006A240B /* main.c in Sources */,
176+
DA8512712220DE23007A250C /* CircularList.c in Sources */,
163177
DA05411C221AD6DC004519F5 /* StaticList.c in Sources */,
164178
DA67B4AF21EF7AFA002D3947 /* LinkList.c in Sources */,
165179
DA48D11021EE193A006A240B /* SqList.c in Sources */,

da-hua-shu-ju-jie-gou/03-list/list/list/List.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,15 @@ typedef int Status; // Status 是函数的类型,其值是函数结
4141
// 线性表存储的数据结构定义
4242
typedef int ElemType; // ElemType类型根据实际情况而定, 这里假设为 int
4343

44+
/**
45+
线性表的单链表、单循环链表的存储结构
46+
47+
结点由存放数据元素的数据域和存放后继结点地址的指针域组成
48+
*/
49+
typedef struct Node {
50+
ElemType data;
51+
struct Node *next;
52+
} Node;
53+
4454

4555
#endif /* List_h */

0 commit comments

Comments
 (0)