diff --git a/DataStructure.xcodeproj/project.pbxproj b/DataStructure.xcodeproj/project.pbxproj index 8502a27..8844060 100644 --- a/DataStructure.xcodeproj/project.pbxproj +++ b/DataStructure.xcodeproj/project.pbxproj @@ -25,6 +25,9 @@ /* Begin PBXFileReference section */ 108A953A29878C0D005C28B4 /* DataStructure */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = DataStructure; sourceTree = BUILT_PRODUCTS_DIR; }; 108A953D29878C0D005C28B4 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; + 108A954429878C66005C28B4 /* DoubleLinkList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DoubleLinkList.h; sourceTree = ""; }; + 108A9546298792B3005C28B4 /* CycleLinkList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CycleLinkList.h; sourceTree = ""; }; + 108A9547298794DA005C28B4 /* CycleDoubleLinkList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CycleDoubleLinkList.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,6 +61,9 @@ isa = PBXGroup; children = ( 108A953D29878C0D005C28B4 /* main.cpp */, + 108A9547298794DA005C28B4 /* CycleDoubleLinkList.h */, + 108A9546298792B3005C28B4 /* CycleLinkList.h */, + 108A954429878C66005C28B4 /* DoubleLinkList.h */, ); path = DataStructure; sourceTree = ""; diff --git a/DataStructure/CycleDoubleLinkList.h b/DataStructure/CycleDoubleLinkList.h new file mode 100644 index 0000000..be8aa2b --- /dev/null +++ b/DataStructure/CycleDoubleLinkList.h @@ -0,0 +1,54 @@ +// +// CycleDoubleLinkList.h +// DataStructure +// 循环双链表 +// Created by Noth Amor on 2023/1/30. +// + +#include + +#ifndef CycleDoubleLinkList_h +#define CycleDoubleLinkList_h + +typedef struct DNode { + int data; + struct DNode *prior, *next; +}DNode, *DLinkList; + +// 初始化空的循环双链表 +bool InitDLinkList(DLinkList &L) { + L = (DNode *) malloc(sizeof(DNode)); + if (L == NULL) { + return false; + } + + L->prior = L; + L->next = L; + + return true; +} + +// 判断循环双链表是否为空 +bool Empty(DLinkList L) { + return L->next == L; +} + +// 循环双链表在p节点之后插入s节点 +bool InsertNextDNode(DNode *p, DNode *s) { + s->next = p->next; + p->next->prior = s; + s->prior = p; + p->next = s; + return true; +} + +// 删除p的后继节点q +bool DeleteDNode(DNode *p) { + DNode *q = p->next; + p->next = q->next; + q->next->prior = p; + free(q); + return true; +} + +#endif /* CycleDoubleLinkList_h */ diff --git a/DataStructure/CycleLinkList.h b/DataStructure/CycleLinkList.h new file mode 100644 index 0000000..050c944 --- /dev/null +++ b/DataStructure/CycleLinkList.h @@ -0,0 +1,40 @@ +// +// CycleLinkList.h +// DataStructure +// 循环单链表 +// Created by Noth Amor on 2023/1/30. +// + +#include + +#ifndef CycleLinkList_h +#define CycleLinkList_h + +typedef struct LNode{ + int data; + struct LNode *next; +}LNode, *LinkList; + +// 初始化循环单链表 +bool InitList(LinkList &L) { + L = (LNode *) malloc(sizeof(LNode)); + if (L == NULL) { + return false; + } + + L->next = L; + + return true; +} + +// 判断循环单链表是否为空 +bool Empty(LinkList L) { + return L->next == L; +} + +// 判断节点p是否为循环单链表的表尾节点 +bool IsTail(LinkList L, LNode *p) { + return p->next == L; +} + +#endif /* CycleLinkList_h */ diff --git a/DataStructure/DoubleLinkList.h b/DataStructure/DoubleLinkList.h new file mode 100644 index 0000000..6369fcb --- /dev/null +++ b/DataStructure/DoubleLinkList.h @@ -0,0 +1,84 @@ +// +// DoubleLinkList.h +// DataStructure +// 双链表 +// Created by Noth Amor on 2023/1/30. +// + +#include + +#ifndef DoubleLinkList_h +#define DoubleLinkList_h + +typedef struct DNode { + int data; + struct DNode *prior, *next; +} DNode, *DLinkList; + +// 初始化双链表 +bool InitDLinkList(DLinkList &L) { + L = (DNode *) malloc(sizeof(DNode)); + if (L == NULL) { + return false; + } + + L->prior = NULL; + L->next = NULL; + + return true; +} + +// 判断双链表是否为空 +bool LinkListEmpty(DLinkList L) { + return L->next == NULL; +} + +// 删除p节点的后继节点 +bool DeleteNextNode(DNode *p) { + if (p == NULL) { + return false; + } + + DNode *q = p->next; + + if (q == NULL) { + return false; + } + + p->next = q->next; + if (p->next != NULL) { + q->next->prior = p; + } + + free(q); + + return true; +} + +// 销毁双链表 +void DestroyDLinkList(DLinkList &L) { + while (L->next != NULL) { + DeleteNextNode(L); + } + free(L); + L = NULL; +} + +// 在p节点之后插入s节点 +bool InsertNextNode(DNode *p, DNode *s) { + if (p == NULL || s == NULL) { + return false; + } + + s->next = p->next; + if (p->next != NULL) { + p->next->prior = s; + } + + s->prior = p; + p->next = s; + + return true; +} + +#endif /* DoubleLinkList_h */ diff --git a/DataStructure/LinkList.h b/DataStructure/LinkList.h new file mode 100644 index 0000000..e306675 --- /dev/null +++ b/DataStructure/LinkList.h @@ -0,0 +1,18 @@ +// +// LinkList.h +// DataStructure +// +// Created by Noth Amor on 2023/1/30. +// + +#ifndef LinkList_h +#define LinkList_h + +typedef struct LNode{ + int data; + struct LNode *next; +}LNode, *LinkList; + + + +#endif /* LinkList_h */ diff --git a/DataStructure/main.cpp b/DataStructure/main.cpp index d78ac81..3fa8622 100644 --- a/DataStructure/main.cpp +++ b/DataStructure/main.cpp @@ -1,14 +1,16 @@ -// -// main.cpp -// DataStructure -// -// Created by Noth Amor on 2023/1/30. -// - #include +#include -int main(int argc, const char * argv[]) { - // insert code here... - std::cout << "Hello, World!\n"; +using namespace std; + +// 静态链表 +typedef struct { + int data; + int next; +} SLinkList[10]; + +int main() { + SLinkList s; + return 0; }