# Basic data structures implementation **Repository Path**: YoHen-Fu/basic-data-structures-implementation ## Basic Information - **Project Name**: Basic data structures implementation - **Description**: 基本数据结构的C++实现 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-24 - **Last Updated**: 2025-02-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 基本数据结构的C++实现 ## 1、数组(Array) **public functions:** | type | function name | | ----:|:------------- | | | | | | | | | | | | | | | | | | | ## 2、[链表(Liked List)](./docs/LinkedList.md) **public functions:** | type | function name | | --------------------:|:---------------------------------- | | | `LinkedList()` | | | `~LinkedList()` | | `void` | `Insert(LValueType X, Position i)` | | `void` | `Delete(Position i)` | | `LNode*` | `Findith(Position i)` | | `LNode*` | `Find(LValueType X)` | | `int` | `getLength()` | | `LNode*` | `getValue()` | **test** ```c #include #include #include "DSFactory.h" #include "DSInterface.h" int main(){ ILinkedList* LN = DSFactory::createLinkedList(); std::vector nums1{1, 2, 3, 4, 5, -1, -1, 8}; // LinkedList *LN = new LinkedList(); for(auto i = 0; i < nums1.size(); i++){ LN->Insert(nums1[i], LN->getLength()); //遍历给链表赋值 } //链表的长度 std::cout<<"链表的长度为:"<getLength()<* LNode_tmp; LNode_tmp = LN->getValue(); std::cout<<"链表中的值为:"; while(LNode_tmp){ std::cout<value<<" "; LNode_tmp = LNode_tmp->next; } LN->Delete(2); std::cout<getValue(); std::cout<<"删除位置2处的元素后,链表中的值为:"; while(LNode_tmp){ std::cout<value<<" "; LNode_tmp = LNode_tmp->next; } std::cout<Find(2)){ std::cout<<"链表中存在值为2的元素"<Find(12)){ std::cout<Find(12)->value<Findith(3)){ std::cout<<"链表中位置3处的值为"<Findith(3)->value<Findith(12)){ std::cout<<"链表中位置3处的值为"<Findith(12)->value< #include #include "DSFactory.h" #include "DSInterface.h" int main(){ std::vector nums1{12, 2, 6, 8, 1, 10, 22, 25}; IStack* S = DSFactory::createStack(); for(auto i = 0; i < nums1.size(); i++){ S->Push(nums1[i]); } std::cout<<"堆栈中元素为:"; while(!S->IsEmpty()){ std::cout<Pop()<<" "; } try{ S->Pop(); }catch(const char* e){ std::cout< #include #include "DSFactory.h" #include "DSInterface.h" int main(){ IQueue* Qe = DSFactory::createQueue(); Qe->AddQ(12); Qe->AddQ(5); int res; while(!Qe->IsEmpty()){ std::cout<DeleteQ()<<" "; } delete Qe; Qe = nullptr; } ``` **output** ```c 12 5 ``` ## 5、树(Tree) ### [基本二叉树(BinTree)](./docs/BinTree.md) **public functions:** | type | function name | | ----------------------:|:--------------------------------------------- | | | `BinTree()` | | | `BinTree(std::vector TreeData)` | | | `~BinTree()` | | `void` | `setBinTree(std::vector nums)` | | `TNode*` | `getBinTree()` | | `int` | `getHeight` | | `void` | `getLeaves` | | `void` | `getPreOrderT()` | | `void` | `getInOrderT()` | | `void` | `getPostOrderT()` | | `void` | `getLevelOrderT()` | | `bool` | `IsIsomorphic(BinTree* BT2)` | **private function** | type | function name | | ----------------------:|:-------------------------------------------------------------------------------- | | `TNode*` | `SetBinTree(TNode* Tree, std::vector nums, int pos)` | | `int` | `GetHeight(TNode* Tree)` | | `void` | `PreOrderPrintLeaves(TNode* Tree)` | | `bool` | `Isomorphic(TNode* Tree1, TNode* Tree2)` | | `void` | `PreOrderTraversal(TNode* Tree)` | | `void` | `InOrderTraversal(TNode* Tree)` | | `void` | `PostOrderTraversal(TNode* Tree)` | **test** ```c #include #include #include "DSFactory.h" #include "DSInterface.h" int main(){ std::vector nums{'a', 'b', 'c', 'd', 'e', '0', '0', 'f'}; IBinTree* BT = DSFactory::createBinTree(); BT->setBinTree(nums); std::cout<<"二叉树的前序遍历结果为:"; BT->getPreOrderT(); std::cout<<"二叉树的中序遍历结果为:"; BT->getInOrderT(); std::cout<<"二叉树的后序遍历结果为:"; BT->getPostOrderT(); // std::cout<<"二叉树的层次遍历结果为:"; // BT->getLevelOrderT(); std::cout<<"二叉树的叶子结点为:"; BT->getLeaves(); std::cout<getHeight(); } ``` **output** ```c 二叉树的前序遍历结果为:a b d f e c 0 0 二叉树的中序遍历结果为:f d b e a 0 c 0 二叉树的后序遍历结果为:f d e b 0 0 c a 二叉树的层次遍历结果为:a b c d e 0 0 f 二叉树的叶子结点为:f e 0 0 二叉树的高度为:4 ``` ### 二叉搜索树 | type | function name | | -------------------------: | :---------------------------------------------- | | `BSTNode*` | `setBinSTree(std::vector nums)` | | `bool` | `IsEmpty()` | | `BSTNode*` | `Find(BSTElementType X)` | | `BSTNode*` | `FindMax()` | | `BSTNode*` | `FindMin` | | `BSTNode*` | `Insert(BSTElementType X)` | | `BSTNode*` | `Delete(BSTElementType X)` | **test** ```cpp #include #include #include "DSFactory.h" #include "DSInterface.h" int main(){ std::vector nums{10, 8, 11, 6, 9, -1, 12}; IBinSearchTree* Tree = DSFactory::createBinSearchTree(); Tree->setBinSTree(nums); auto tmp{Tree->Find(11)}; if(tmp){ std::cout<value<FindMax()}; if(max){ std::cout<value<FindMin()}; if(min){ std::cout<value<Insert(13); Tree->Insert(5); Tree->Insert(7); Tree->Delete(10); std::cout<<"程序执行完毕"; } ``` **output** ```c 11 12 -1 程序执行完毕 ``` ## 6、散列表(Hash) **public functions:** | type | function name | | ----:|:------------- | | | | | | | | | | | | | | | | | | | ## 7、 堆(Heap) ### 最大堆 **public functions:** | type | function name | | ----:|:------------- | | | | | | | | | | | | | | | | | | | ```cpp #include #include #include "DSFactory.h" #include "DSInterface.h" int main(){ std::vector nums{1,3,2}; int maxsize = 100; int maxdata = 100; IMaxHeap* H = DSFactory::createIMaxHeap(); H->CreateMaxHeap(maxsize, maxdata); // H->setSize(nums.size()); for(int i = 0; i < nums.size(); i++){ H->Insert(nums[i]); } for(auto i = 0; igetHeap()->Size; i++){ std::cout<getHeap()->Elements[i+1]<DeleteMax()<getHeap()->Size; i++){ std::cout<getHeap()->Elements[i+1]<