数据结构第一轮复习--第七章查找(包罗课程代码)

打印 上一主题 下一主题

主题 1777|帖子 1777|积分 5331

基于数组实现次序查找代码

  1. //顺序查找的实现
  2. typedef struct{                //查找表的数据结构(顺序表)
  3.         ElemType *elem;        //指向开辟的动态数组基址 (起始地址)
  4.         int TableLen;        //表的长度
  5. }SSTable;
  6. //顺序查找
  7. int Search_Seq(SSTable ST,ElemType key){
  8.         int i;
  9.         for(i=0;i<ST.TableLen && ST.elem[i]!=key;++i);
  10.         //查找成功,则返回元素下标;查找失败,则返回-1
  11.         return i==SSTable? -1 : i;
  12. }
复制代码
基于链表实现次序查找代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 定义链表节点的数据类型
  4. typedef int ElemType;
  5. // 定义链表节点结构
  6. typedef struct Node {
  7.     ElemType data;       // 数据域,存储节点的值
  8.     struct Node* next;   // 指针域,指向下一个节点
  9. } Node;
  10. // 定义链表结构
  11. typedef struct {
  12.     Node* head;          // 指向链表的头节点
  13. } LinkedList;
  14. // 创建链表(假设链表是带头节点的单链表)
  15. LinkedList* CreateLinkedList() {
  16.     // 分配内存空间给链表结构
  17.     LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
  18.     if (list == NULL) {
  19.         printf("Memory allocation failed for LinkedList.\n");
  20.         return NULL; // 如果分配失败,返回NULL
  21.     }
  22.     // 分配内存空间给头节点
  23.     list->head = (Node*)malloc(sizeof(Node));
  24.     if (list->head == NULL) {
  25.         free(list); // 如果头节点分配失败,释放之前分配的链表结构内存
  26.         printf("Memory allocation failed for head node.\n");
  27.         return NULL;
  28.     }
  29.     // 初始化头节点,头节点不存储实际数据,其next指针初始化为NULL
  30.     list->head->next = NULL;
  31.     return list; // 返回创建好的链表
  32. }
  33. // 向链表中插入节点(在链表尾部插入)
  34. void InsertNode(LinkedList* list, ElemType value) {
  35.     // 分配内存空间给新节点
  36.     Node* newNode = (Node*)malloc(sizeof(Node));
  37.     if (newNode == NULL) {
  38.         printf("Memory allocation failed for new node.\n");
  39.         return; // 如果分配失败,直接返回
  40.     }
  41.     // 设置新节点的数据域和指针域
  42.     newNode->data = value;
  43.     newNode->next = NULL;
  44.     // 从头节点开始,找到链表的尾部节点
  45.     Node* current = list->head;
  46.     while (current->next != NULL) { // 当前节点的next不为NULL,说明不是尾部节点
  47.         current = current->next; // 移动到下一个节点
  48.     }
  49.     // 将新节点链接到链表尾部
  50.     current->next = newNode;
  51. }
  52. // 顺序查找(基于链表)
  53. int Search_Seq(LinkedList* list, ElemType key) {
  54.     // 从头节点的下一个节点开始(跳过头节点,从第一个实际节点开始)
  55.     Node* current = list->head->next;
  56.     int index = 0; // 用于记录当前节点的索引,从0开始
  57.     // 遍历链表
  58.     while (current != NULL) {
  59.         if (current->data == key) { // 如果当前节点的数据等于目标值
  60.             return index; // 返回当前节点的索引
  61.         }
  62.         current = current->next; // 移动到下一个节点
  63.         index++; // 索引加1
  64.     }
  65.     // 如果遍历完整个链表都没有找到目标值,返回-1
  66.     return -1;
  67. }
  68. // 释放链表内存
  69. void FreeLinkedList(LinkedList* list) {
  70.     // 从头节点开始释放链表内存
  71.     Node* current = list->head;
  72.     while (current != NULL) {
  73.         Node* temp = current; // 保存当前节点
  74.         current = current->next; // 移动到下一个节点
  75.         free(temp); // 释放当前节点的内存
  76.     }
  77.     free(list); // 最后释放链表结构的内存
  78. }
  79. // 测试代码
  80. int main() {
  81.     // 创建链表
  82.     LinkedList* list = CreateLinkedList();
  83.     if (list == NULL) {
  84.         return -1; // 如果链表创建失败,退出程序
  85.     }
  86.     // 向链表中插入一些数据
  87.     InsertNode(list, 10);
  88.     InsertNode(list, 20);
  89.     InsertNode(list, 30);
  90.     InsertNode(list, 40);
  91.     // 测试顺序查找
  92.     int key = 30; // 要查找的目标值
  93.     int result = Search_Seq(list, key); // 调用顺序查找函数
  94.     if (result != -1) {
  95.         printf("Element %d found at index %d.\n", key, result); // 找到目标值,输出索引
  96.     } else {
  97.         printf("Element %d not found.\n", key); // 没有找到目标值
  98.     }
  99.     // 释放链表内存
  100.     FreeLinkedList(list);
  101.     return 0; // 程序正常结束
  102. }
复制代码
 次序查找的实现(哨兵)

  1. //顺序查找(哨兵)
  2. typedef struct{                //查找表的数据结构(顺序表)
  3.         ElemType *elem;        //指向开辟的动态数组基址 (起始地址)
  4.         int TableLen;        //表的长度
  5. }SSTable;
  6. //顺序查找
  7. int Search_Seq(SSTable ST,ElemType key){
  8.         ST.elem[0]=key;                //哨兵
  9.         int i;
  10.         for(i=ST.TableLen; ST.elem[i]!=key;--i);        //从后往前找
  11.         //查找成功,则返回元素下标;查找失败,则返回0
  12.         return i;
  13. }
复制代码

折半查找的实现(二分查找)

  1. //折半查找的实现(二分查找)
  2. typedef struct{                //查找表的数据结构(顺序表)
  3.         ElemType *elem;        //指向开辟的动态数组基址 (起始地址)
  4.         int TableLen;        //表的长度
  5. }SSTable;
  6. //折半查找
  7. int Binary_Search(SSTable L,ElemType key){
  8.         int low=0,high=L.TableLen-1,mid;
  9.         while(low<=high){
  10.                 mid=(low+high)/2;        //取中间位置
  11.                 if(L.elem[mid]==key)
  12.                         return mid;                //查找成功则返回所在位置
  13.                 else if(L.elem[mid]>key)
  14.                         high=mid-1;                //从前半部分继续查找
  15.                 else
  16.                         low=mid+1;                //从后半部分继续查找
  17.         }
  18.         return -1;                                //查找失败,返回-1
  19. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

南飓风

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表