当前位置: 首页 > wzjs >正文

网站开发ckplayer加载失败10大品牌网

网站开发ckplayer加载失败,10大品牌网,国内服务器租用,如何提高网站的安全性在数据结构算法中,有一种算法犹如“时空穿梭机”,能在瞬间跨越层层障碍,直击目标——它就是跳表算法。下面,就让我们一起揭开跳表算法的神秘面纱,通过实例探究其高效与魅力。 目录 一、跳表算法是什么? …

        在数据结构算法中,有一种算法犹如“时空穿梭机”,能在瞬间跨越层层障碍,直击目标——它就是跳表算法。下面,就让我们一起揭开跳表算法的神秘面纱,通过实例探究其高效与魅力。

目录

一、跳表算法是什么?

二、实例解析

1、跳表的构建

2、查找过程

三、跳表算法的优点

四、跳表——C语言实现

1、常规链表查找

2、跳表查找

一、跳表算法是什么?

        跳表,顾名思义,是一种能够“跳过”某些节点进行搜索的链表。它通过再链表的基础上增加多级索引,实现了对数据的快速定位、插入与删除。想象一下,再一条长长的队伍中,你能直接跳到接近目标的位置,是不是能缩短搜索该目标的时间,从而大大提高代码运行效率。

二、实例解析

        假设有一个有序的链表,存储了数字1到10。现在,需要查找数字7.在没有跳表的情况下,可能需要从头开始,一步步遍历到7。但是有了跳表,一切都将变得不同。

1、跳表的构建

        首先,要为这个链表构建一个跳表。跳表分为多层,每一层都是下面一层的部分节点建立的索引。比如:

层3:4,8

层2:2,4,6,8,10

层1:1,2,3,4,5,6,7,8,9,10

2、查找过程

        现在,开始查找数据7:

        从层3开始查找,首先比较4和7。由于4比7小,就继续向右查找,直至比较到8,仍未找到7,于是便下降到层2。

        在层2比较6和7。6仍然小于7,但接近查找目标。继续向右查找,发现8大于7,于是再次下降一层,到达层1。

        在层1,直接定位到6,便可轻松查找到值7。

        通过这个实例可以看到,跳表通过多级索引,实现了对数据的快速定位,大大减少了查找的时间复杂度。但代价是占用更多的空间。典型的空间换时间

三、跳表算法的优点

高效性:跳表的查找、插入和删除操作的平均时间复杂度都是O(log n),媲美平衡树结构。

简单性:相对于红黑树等复杂的数据结构,跳表的实现更为简单,易于理解和维护。

随机性:跳表的随机性保证了其性能的稳定性,避免了极端情况下的性能下降。

四、跳表——C语言实现

1、常规链表查找

#include <stdio.h>
#include <stdlib.h>// 跳表节点定义
typedef struct SkipListNode {int value;struct SkipListNode *next;struct SkipListNode *skip;
} SkipListNode;// 创建跳表节点
SkipListNode* createSkipListNode(int value) {SkipListNode* node = (SkipListNode*)malloc(sizeof(SkipListNode));node->value = value;node->next = NULL;node->skip = NULL;return node;
}// 插入节点到跳表
void insertSkipList(SkipListNode** head, int value) {SkipListNode* newNode = createSkipListNode(value);if (*head == NULL) {*head = newNode;return;}SkipListNode* current = *head;SkipListNode* skipPrev = NULL;// 寻找插入位置while (current != NULL && current->value < value) {skipPrev = current;current = current->next;}// 插入节点newNode->next = current;if (skipPrev != NULL) {skipPrev->next = newNode;} else {*head = newNode;}// 更新跳过指针if (skipPrev != NULL && skipPrev->skip != NULL && skipPrev->skip->value < value) {newNode->skip = skipPrev->skip->next;skipPrev->skip->next = newNode;}
}// 查找节点
SkipListNode* findSkipList(SkipListNode* head, int value) {SkipListNode* current = head;while (current != NULL) {if (current->value == value) {return current;} else if (current->skip != NULL && current->skip->value <= value) {current = current->skip;} else {current = current->next;}}return NULL;
}// 打印跳表
void printSkipList(SkipListNode* head) {SkipListNode* current = head;while (current != NULL) {printf("%d ", current->value);if (current->skip != NULL) {printf("(skip to %d) ", current->skip->value);}current = current->next;}printf("\n");
}// 主函数
int main() {SkipListNode* head = NULL;// 插入数据insertSkipList(&head, 3);insertSkipList(&head, 7);insertSkipList(&head, 6);insertSkipList(&head, 9);insertSkipList(&head, 12);insertSkipList(&head, 19);insertSkipList(&head, 25);insertSkipList(&head, 30);// 打印跳表printf("Skip List: ");printSkipList(head);// 查找数据int valueToFind = 19;SkipListNode* foundNode = findSkipList(head, valueToFind);if (foundNode != NULL) {printf("Value %d found in the skip list.\n", valueToFind);} else {printf("Value %d not found in the skip list.\n", valueToFind);}return 0;
}

2、跳表查找

#include <stdio.h>
#include <stdlib.h>
#include <time.h>#define MAXLEVEL 3 // 假设跳表的最大层级为3// 跳表节点定义
typedef struct SkipListNode {int value;struct SkipListNode *forward[MAXLEVEL]; // 指向不同层级的下一个节点
} SkipListNode;// 创建跳表节点
SkipListNode* createSkipListNode(int level, int value) {SkipListNode* newNode = (SkipListNode*)malloc(sizeof(SkipListNode));newNode->value = value;for (int i = 0; i < level; i++) {newNode->forward[i] = NULL;}return newNode;
}// 随机生成节点的层级
int randomLevel() {int level = 1;while ((rand() & 0xFFFF) < (0.5 * 0xFFFF)) {level++;if (level == MAXLEVEL) break;}return level;
}// 插入节点到跳表
void insertSkipList(SkipListNode **head, int value) {SkipListNode *update[MAXLEVEL];SkipListNode *current = *head;// 从最高层开始,找到每层中插入位置的前一个节点for (int i = MAXLEVEL - 1; i >= 0; i--) {while (current->forward[i] != NULL && current->forward[i]->value < value) {current = current->forward[i];}update[i] = current;}// 随机决定新节点的层级int level = randomLevel();// 创建新节点SkipListNode *newNode = createSkipListNode(level, value);// 将新节点插入到跳表中for (int i = 0; i < level; i++) {newNode->forward[i] = update[i]->forward[i];update[i]->forward[i] = newNode;}
}// 查找节点
int findSkipList(SkipListNode *head, int value) {SkipListNode *current = head;int count = 0;int ncount = 0;for (int i = MAXLEVEL - 1; i >= 0; i--) {count ++ ;while (current->forward[i] != NULL && current->forward[i]->value < value) {current = current->forward[i];ncount ++ ;printf("ncount %d .\n", ncount);}count += ncount;printf("count %d .\n", count);ncount = 0;if(current->forward[i] != NULL && current->forward[i]->value == value ){return count; // 找到值,返回查找次数}}return -1; // 未找到值
}// 打印跳表
void printSkipList(SkipListNode *head) {for (int i = MAXLEVEL - 1; i >= 0; i--) {SkipListNode *current = head->forward[i];printf("Level %d: ", i);while (current != NULL) {printf("%d ", current->value);current = current->forward[i];}printf("\n");}
}int main() {srand((unsigned)time(NULL)); // 初始化随机数生成器// 创建跳表头节点SkipListNode *head = createSkipListNode(MAXLEVEL, -1);// 插入数据insertSkipList(&head, 3);insertSkipList(&head, 6);insertSkipList(&head, 7);insertSkipList(&head, 9);insertSkipList(&head, 12);insertSkipList(&head, 19);insertSkipList(&head, 25);insertSkipList(&head, 29);// 打印跳表printSkipList(head);// 查找数据int valueToFind = 7;int searchCount = findSkipList(head, valueToFind);if (searchCount != -1) {printf("Number of steps taken to find the value %d: %d\n", valueToFind, searchCount);} else {printf("Value %d not found in the skip list.\n", valueToFind);}valueToFind = 29;searchCount = findSkipList(head, valueToFind);if (searchCount != -1) {printf("Number of steps taken to find the value %d: %d\n", valueToFind, searchCount);} else {printf("Value %d not found in the skip list.\n", valueToFind);}// TODO: 实现删除操作以及内存释放return 0;
}


文章转载自:

http://7SW25AgU.rggky.cn
http://1L36eKry.rggky.cn
http://bIcSV9q2.rggky.cn
http://CaMf5mAw.rggky.cn
http://W6X1dBc1.rggky.cn
http://jKJwYIsz.rggky.cn
http://TIfwWQXA.rggky.cn
http://dJoxJVaN.rggky.cn
http://HPtGTRIS.rggky.cn
http://G6UKyFg7.rggky.cn
http://y8waNcbL.rggky.cn
http://YhrXVVNC.rggky.cn
http://cfPJCs7o.rggky.cn
http://LIZ91S7c.rggky.cn
http://kxiCH6Xp.rggky.cn
http://VC0k4wVJ.rggky.cn
http://rc1R7XOP.rggky.cn
http://xfFBi1wy.rggky.cn
http://dwft3pP8.rggky.cn
http://hXefaihz.rggky.cn
http://FVUnPgGI.rggky.cn
http://qqwKlJn5.rggky.cn
http://3JFBMFiA.rggky.cn
http://l51MHhE6.rggky.cn
http://BSE2gZrl.rggky.cn
http://17O3z3xV.rggky.cn
http://DrSCf5iM.rggky.cn
http://xusgBZsH.rggky.cn
http://Q0LMCiI6.rggky.cn
http://F894Q9An.rggky.cn
http://www.dtcms.com/wzjs/722952.html

相关文章:

  • 怎么做镜像网站成都网站建设单招网
  • 网站建设刂金手指下拉十五电气建设网站
  • 外贸电商网站制作大连网站设计
  • 怎样建设VR网站块链友情链接平台
  • 网站开发教程流程创新作品及其设计方案
  • 没有网站域名备案信息吗seo排名点击首页
  • 法律咨询网站建设方案九冶建设有限公司网站
  • 东莞百度搜索网站排名寻找长沙网站建设
  • 帮人做设计的网站做评测好的视频网站有哪些
  • 网站开发常用图标 图像wordpress后台产品图标
  • 出台网站集约化建设通知培训中心网站建设方案
  • 做企业网站项目的心得论坛网站建设需要多少钱
  • 绿色学校网站模板百度热搜的含义
  • 网站项目建设的必要性怎么做移动端网站
  • 找网站开发合作伙伴wordpress 网站打不开
  • 北京孤儿院做义工网站网站托管服务
  • 网站设计服务企业网站域名归属权
  • 安徽网站设计与优化网站建设的完整流程
  • 百度网站推广费用多少深圳网站做优化哪家公司好
  • 潍坊哪家做网站做的最好台州椒江区热销企业网站搭建
  • 建站之星做网站wordpress本地批量传文章
  • 网站开发与管理对应的职业及岗位南京专业建站
  • 网站跟软件有什么区别是什么网站开发设计资料
  • 开发软件的成本预算网站seo优化服务
  • 门户网站开发要求正规网站制作公司有哪些
  • 给网站划分栏目dede网站地图模板
  • 意大利语网站建设公司有网站有什么好处
  • 大型网站建设公司排名小程序定制外包
  • 桂林网站优化注意事项建筑清单网
  • 随县网站建设怎么做二维码进入公司网站