From e29bed67f129fd0cccf4e8416d5e1ccce3a89e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E9=A2=96?= <3471849694@qq.com> Date: Fri, 22 Dec 2023 12:04:59 +0000 Subject: [PATCH] =?UTF-8?q?fixed=203928692=20from=20https://gitee.com/suyo?= =?UTF-8?q?oo/data=5Fstructure/pulls/1441=20=E7=AC=AC=E4=B9=9D=E7=AB=A0?= =?UTF-8?q?=EF=BC=88=E4=B8=8A=E6=9C=BA=E5=AE=9E=E9=AA=8C=E6=8C=87=E5=AF=BC?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2224020127/chapter_9/exp9-16.cpp | 70 ++++++++++++++++++++++++++++++++ 2224020127/chapter_9/exp9-2.cpp | 32 +++++++++++++++ 2224020127/chapter_9/exp9-9.cpp | 67 ++++++++++++++++++++++++++++++ 2224020127/chapter_9/seqlist.cpp | 22 ++++++++++ 4 files changed, 191 insertions(+) create mode 100644 2224020127/chapter_9/exp9-16.cpp create mode 100644 2224020127/chapter_9/exp9-2.cpp create mode 100644 2224020127/chapter_9/exp9-9.cpp create mode 100644 2224020127/chapter_9/seqlist.cpp diff --git a/2224020127/chapter_9/exp9-16.cpp b/2224020127/chapter_9/exp9-16.cpp new file mode 100644 index 00000000..e64b4151 --- /dev/null +++ b/2224020127/chapter_9/exp9-16.cpp @@ -0,0 +1,70 @@ +//文件名:exp9-16.cpp +#include +#define MaxSize 1005 +int ht[MaxSize]; //哈希表 +int a[MaxSize]; +int n; +//-----哈希表操作算法开始------- +void insertht(int x,int no) //将插入到哈希表ht中 +{ + ht[x]=no; +} +void deleteht(int x) //从哈希表ht中删除 +{ + ht[x]=-1; +} +//-----哈希表操作算法结束------- +void dispa() //输出序列 +{ + printf("a: "); + for(int i=0;i插入到哈希表ht中 + n++; +} +void deletea(int x) //删除x +{ + int no=ht[x]; //求整数x的序号no + deleteht(x); //从哈希表ht中删除x + a[no]=a[n-1]; //将z中末尾元素移到no位置 + deleteht(a[n-1]); //从哈希表ht中删除a[n-1] + insertht(a[n-1],no); //将重新插入到哈希表ht中 + n--; +} +int search(int no) //返回序号为no的整数 +{ + return a[no-1]; +} +int main() +{ + int op[]={1,5,1,4,1,3,2,4,1,7,1,6,3,2,2,5,3,1,3,2}; + int m=sizeof(op)/sizeof(op[0]); //操作次数为m/2 + for(int i=0;ik) //继续在R[low..mid-1]中查找 + high=mid-1; + else + low=mid+1; //继续在R[mid+1..high]中查找 + } + return 0; +} +int main() +{ + RecType R[MAXL]; + KeyType k=9; + int a[]={1,2,3,4,5,6,7,8,9,10},i,n=10; + CreateList(R,a,n); //建立顺序表 + printf("关键字序列:"); DispList(R,n); + printf("查找%d的比较过程如下:\n",k); + if ((i=BinSearch(R,n,k))!=0) + printf("元素%d的位置是%d\n",k,i); + else + printf("元素%d不在表中\n",k); + return 1; +} \ No newline at end of file diff --git a/2224020127/chapter_9/exp9-9.cpp b/2224020127/chapter_9/exp9-9.cpp new file mode 100644 index 00000000..c9bf76e8 --- /dev/null +++ b/2224020127/chapter_9/exp9-9.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +#define MAXWORD 100 + +typedef struct tnode { + char ch; // 字符 + int count; // 出现次数 + struct tnode *lchild, *rchild; +} BSTNode; + +void CreateBST(BSTNode *&bt, char c) // 采用递归方式构造一棵二叉排序树bt +{ + if (bt == NULL) // bt为NULL,则建立一个新节点 + { + bt = (BSTNode *)malloc(sizeof(BSTNode)); + bt->ch = c; + bt->count = 1; + bt->lchild = bt->rchild = NULL; + } else if (c == bt->ch) + bt->count++; + else if (c < bt->ch) + CreateBST(bt->lchild, c); + else + CreateBST(bt->rchild, c); +} + +void InOrder(BSTNode *bt) // 中序遍历二叉排序树bt +{ + if (bt != NULL) { + InOrder(bt->lchild); // 中序遍历左子树 + printf(" %c(%d)\n", bt->ch, bt->count); // 访问根节点 + InOrder(bt->rchild); // 中序遍历右子树 + } +} + +void DestroyBST(BSTNode *bt) // 销毁二叉排序树bt +{ + if (bt != NULL) { + DestroyBST(bt->lchild); + DestroyBST(bt->rchild); + free(bt); + } +} + +int main() +{ + BSTNode *bt = NULL; + int i = 0; + char str[MAXWORD]; + printf("输入字符串:"); + fgets(str, sizeof(str), stdin); + + while (str[i] != '\0') { + if (str[i] != '\n') { + CreateBST(bt, str[i]); + } + i++; + } + + printf("字符及出现次数:\n"); + InOrder(bt); + DestroyBST(bt); + + return 1; +} diff --git a/2224020127/chapter_9/seqlist.cpp b/2224020127/chapter_9/seqlist.cpp new file mode 100644 index 00000000..358b6c39 --- /dev/null +++ b/2224020127/chapter_9/seqlist.cpp @@ -0,0 +1,22 @@ +#include +#include +#define MAXL 100 //最大长度 +typedef int KeyType; //定义关键字类型为int +typedef char InfoType; + +typedef struct +{ KeyType key; //关键字项 + InfoType data; //其他数据项,类型为InfoType +} RecType; //查找元素的类型 + +void CreateList(RecType R[],KeyType keys[],int n) //创建顺序表 +{ + for (int i=0;i