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

永州做网站常用搜索引擎有哪些

永州做网站,常用搜索引擎有哪些,海报模板免费网站,360门户网站怎样做目录 Test.c测试代码 test1 test2 test3 🎇Test.c总代码 Heap.h头文件&函数声明 头文件 函数声明 🎇Heap.h总代码 Heap.c函数实现 ☁HeapInit初始化 ☁HeapDestroy销毁 ☁HeapPush插入数据 【1】插入数据 【2】向上调整Adjustup❗ …

目录

Test.c测试代码

test1

test2

test3

🎇Test.c总代码 

Heap.h头文件&函数声明

头文件

函数声明

🎇Heap.h总代码

Heap.c函数实现 

☁HeapInit初始化

☁HeapDestroy销毁

☁HeapPush插入数据

【1】插入数据

【2】向上调整Adjustup❗

数据交换Swap

☁HeapPop删除数据

【1】交换数据

【2】删除数据

【3】向下调整Adjustdown❗

假设法找Child 

数据交换Swap

☁HeapTop堆顶元素

☁HeapSize堆元素个数

☁HeapEmpty判断为空否

🎇Heap.c总代码 


拖了很久的【堆】,本周主要学习就是【数据结构】。本章【堆】是以【小堆】为例子。

  •  堆表面是数组,内核是完全二叉树/满二叉树
  • ❗HeapPush
  • ❗HeapPop
  • 函数如果需要多次复用才会提取出来
  • free会对NULL进行检查 
  • 用循环写起来很方便的代码就不要使用递归
  • do while循环用于无论循环条件如何,循环都会执行一次
  • 步骤--注意事项--循环结束条件--时间复杂度(下篇重点讲)

Test.c测试代码

#include"Heap.h"
int main()
{HP php;//HP*ph=&php//test1(&php);test2(&php);test3(&php);return 0;
}

test1

//建堆
void test1(HP* ph)
{int a[] = { 4,5,3,7,8,2,1,9,10 };HeapInit(ph);int i = 0;for (i = 0; i < sizeof(a) / sizeof(a[0]); i++){HeapPush(ph, a[i]);}
}

test2

//找出堆的前K个数字
void test2(HP* ph)
{int a[] = { 4,5,3,7,8,2,1,9,10 };HeapInit(ph);int i = 0;int k = 5;for (i = 0; i < sizeof(a) / sizeof(a[0]); i++){HeapPush(ph, a[i]);}while (k--){printf("%d ", HeapTop(ph));HeapPop(ph);}printf("\n");
}

 

test3

//排序--升序
void test3(HP* ph)
{int a[] = { 4,5,3,7,8,2,1,9,10 };HeapInit(ph);int i = 0;for (i = 0; i < sizeof(a) / sizeof(a[0]); i++){HeapPush(ph, a[i]);}while (HeapEmpty(ph))//为NULLflase{printf("%d ", HeapTop(ph));HeapPop(ph);}
}

🎇Test.c总代码 

#include"Heap.h"
//建堆
void test1(HP* ph)
{int a[] = { 4,5,3,7,8,2,1,9,10 };HeapInit(ph);int i = 0;for (i = 0; i < sizeof(a) / sizeof(a[0]); i++){HeapPush(ph, a[i]);}
}//找出堆的前K个数字
void test2(HP* ph)
{int a[] = { 4,5,3,7,8,2,1,9,10 };HeapInit(ph);int i = 0;int k = 5;for (i = 0; i < sizeof(a) / sizeof(a[0]); i++){HeapPush(ph, a[i]);}while (k--){printf("%d ", HeapTop(ph));HeapPop(ph);}printf("\n");
}void test3(HP* ph)
{int a[] = { 4,5,3,7,8,2,1,9,10 };HeapInit(ph);int i = 0;for (i = 0; i < sizeof(a) / sizeof(a[0]); i++){HeapPush(ph, a[i]);}while (HeapEmpty(ph))//为NULLflase{printf("%d ", HeapTop(ph));HeapPop(ph);}
}int main()
{HP php;//HP*ph=&php//test1(&php);test2(&php);test3(&php);return 0;
}

Heap.h头文件&函数声明

头文件

#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int HpDataType;//定义堆结构体
typedef struct Heap
{HpDataType* a;int size;int capacity;
}HP;

函数声明

//常用接口
void HeapInit(HP* php);
void HeapDestroy(HP* php);
void HeapPush(HP* php,HpDataType x);
void HeapPop(HP* php);
HpDataType HeapTop(HP* php);
int HeapSize(HP* php);
bool HeapEmpty(HP* php);

🎇Heap.h总代码

#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>typedef int HpDataType;//定义堆结构体
typedef struct Heap
{HpDataType* a;int size;int capacity;
}HP;//常用接口
void HeapInit(HP* php);
void HeapDestroy(HP* php);
void HeapPush(HP* php,HpDataType x);
void HeapPop(HP* php);
HpDataType HeapTop(HP* php);
int HeapSize(HP* php);
bool HeapEmpty(HP* php);

Heap.c函数实现 

☁HeapInit初始化

#include"Heap.h"
void HeapInit(HP* php)
{assert(php);php->a = NULL;php->size = 0;php->capacity = 0;
}

☁HeapDestroy销毁

void HeapDestroy(HP* php)
{assert(php);free(php->a);//不用担心为NULL,free会对NULL做检查php->size = php->capacity = 0;
}

☁HeapPush插入数据

 先插入一个10到数组的尾上,再进行向上调整算法,直到满足堆。

【1】插入数据

void HeapPush(HP* php, HpDataType x)
{assert(php);//扩容if (php->size == php->capacity){int newcapacity = php->capacity == 0 ? 4 : 2 * php->capacity;HpDataType* tmp = (HpDataType*)realloc(php->a, newcapacity * sizeof(HpDataType));if (tmp == NULL){printf("fail realloc");return;}php->capacity = newcapacity;php->a = tmp;}//插入数据php->a[php->size] = x;php->size++;//向上调整//数组,调整元素下标AdjustUp(php->a, php->size - 1);
}

【2】向上调整Adjustup❗

//向上调整算法
void AdjustUp(HpDataType* a, int child)
{int parent = (child - 1) / 2;while ( child > 0 )//此刻parent已经数组越界{if (a[child] < a[parent]){//交换Swap(&a[child], &a[parent]);child = parent;parent = (parent - 1) / 2;}else//child>=parent{break;}}
}
数据交换Swap
//交换
void Swap(HpDataType* H1, HpDataType* H2)
{HpDataType tmp = *H1;*H1 = *H2;*H2 = tmp;
}

☁HeapPop删除数据

删除堆是删除堆顶的数据,将堆顶的数据根最后一个数据一换,然后删除数组最后一个数据,再进行向下调整算法。

//删除
void HeapPop(HP* php)
{assert(php);assert(php->size);//1.交换Swap(&php->a[0], &php->a[php->size - 1]);//2.删除php->size--;//3.向下调整--数组,结束条件size有关,调整的位置parentAdjustdown(php->a, php->size, 0);
}

【1】交换数据

	//1.交换Swap(&php->a[0], &php->a[php->size - 1]);

【2】删除数据

	//2.删除php->size--;

【3】向下调整Adjustdown❗

//3.向下调整--数组,结束条件size有关,调整的位置parent
Adjustdown(php->a, php->size, 0);
//向下调整
void Adjustdown(HpDataType* a, int size, int parent)
{//假设法int child = parent * 2 + 1;while (child < size )//why child < size && child+1<size{if (child + 1 < size && a[child + 1] < a[child]){child++;}//比较if (a[child] < a[parent]){//交换Swap(&a[child], &a[parent]);parent = child;child = child * 2 + 1;}else//>={break;}}
}
假设法找Child 
	int child = parent * 2 + 1;if (a[child + 1] < a[child]){child++;}
数据交换Swap
//交换
void Swap(HpDataType* H1, HpDataType* H2)
{HpDataType tmp = *H1;*H1 = *H2;*H2 = tmp;
}

☁HeapTop堆顶元素

HpDataType HeapTop(HP* php)
{assert(php);assert(php->size);return php->a[0];
}

☁HeapSize堆元素个数

int HeapSize(HP* php)
{assert(php);return php->size;
}

☁HeapEmpty判断为空否

bool HeapEmpty(HP* php)
{assert(php);return php->size != 0;//!=0是true不为NULL执行   ==0 flase 不执行
}

🎇Heap.c总代码 

#include"Heap.h"
void HeapInit(HP* php)
{assert(php);php->a = NULL;php->size = 0;php->capacity = 0;
}void HeapDestroy(HP* php)
{assert(php);free(php->a);//不用担心为NULL,free会对NULL做检查php->size = php->capacity = 0;
}
//交换
void Swap(HpDataType* H1, HpDataType* H2)
{HpDataType tmp = *H1;*H1 = *H2;*H2 = tmp;
}//向上调整算法
void AdjustUp(HpDataType* a, int child)
{int parent = (child - 1) / 2;while ( child > 0 )//此刻parent已经数组越界{if (a[child] < a[parent]){//交换Swap(&a[child], &a[parent]);child = parent;parent = (parent - 1) / 2;}else//child>=parent{break;}}
}void HeapPush(HP* php, HpDataType x)
{assert(php);//扩容if (php->size == php->capacity){int newcapacity = php->capacity == 0 ? 4 : 2 * php->capacity;HpDataType* tmp = (HpDataType*)realloc(php->a, newcapacity * sizeof(HpDataType));if (tmp == NULL){printf("fail realloc");return;}php->capacity = newcapacity;php->a = tmp;}//插入数据php->a[php->size] = x;php->size++;//向上调整//数组,调整元素下标AdjustUp(php->a, php->size - 1);
}//向下调整
void Adjustdown(HpDataType* a, int size, int parent)
{//假设法int child = parent * 2 + 1;while (child < size )//why child < size && child+1<size{if (child + 1 < size && a[child + 1] < a[child]){child++;}//比较if (a[child] < a[parent]){//交换Swap(&a[child], &a[parent]);parent = child;child = child * 2 + 1;}else//>={break;}}
}
//删除
void HeapPop(HP* php)
{assert(php);assert(php->size);//1.交换Swap(&php->a[0], &php->a[php->size - 1]);//2.删除php->size--;//3.向下调整--数组,结束条件size有关,调整的位置parentAdjustdown(php->a, php->size, 0);
}HpDataType HeapTop(HP* php)
{assert(php);assert(php->size);return php->a[0];
}int HeapSize(HP* php)
{assert(php);return php->size;
}bool HeapEmpty(HP* php)
{assert(php);return php->size != 0;//!=0是true不为NULL执行   ==0 flase 不执行
}

当然,【大堆】只要改变大小符号即可,如果你不想要改变,可以使用【回调函数】!! 

🙂感谢大家的阅读,若有错误和不足,欢迎指正!希望本周可以结束【初阶数据结构】


文章转载自:
http://nuncio.sqLh.cn
http://aluminothermics.sqLh.cn
http://terrific.sqLh.cn
http://saxonise.sqLh.cn
http://ruthfulness.sqLh.cn
http://horn.sqLh.cn
http://autolyzate.sqLh.cn
http://irrelated.sqLh.cn
http://butyric.sqLh.cn
http://bookable.sqLh.cn
http://valedictorian.sqLh.cn
http://accurately.sqLh.cn
http://protocol.sqLh.cn
http://noble.sqLh.cn
http://tupperware.sqLh.cn
http://hogan.sqLh.cn
http://chryselephantine.sqLh.cn
http://slogan.sqLh.cn
http://tappit.sqLh.cn
http://asphaltic.sqLh.cn
http://misdo.sqLh.cn
http://stickball.sqLh.cn
http://nucleometer.sqLh.cn
http://evert.sqLh.cn
http://thrillingness.sqLh.cn
http://inoculation.sqLh.cn
http://chorda.sqLh.cn
http://pesach.sqLh.cn
http://amortize.sqLh.cn
http://hystrichosphere.sqLh.cn
http://hydromagnetics.sqLh.cn
http://tripalmitin.sqLh.cn
http://unpen.sqLh.cn
http://hollowware.sqLh.cn
http://marvy.sqLh.cn
http://horseshit.sqLh.cn
http://subjoin.sqLh.cn
http://carapace.sqLh.cn
http://raincoat.sqLh.cn
http://gothamite.sqLh.cn
http://speedboat.sqLh.cn
http://vampirism.sqLh.cn
http://saltatorial.sqLh.cn
http://posthumous.sqLh.cn
http://crestfallen.sqLh.cn
http://schistosome.sqLh.cn
http://brimmer.sqLh.cn
http://dinerout.sqLh.cn
http://syntonize.sqLh.cn
http://aardwolf.sqLh.cn
http://bnfl.sqLh.cn
http://neurohormone.sqLh.cn
http://moslemic.sqLh.cn
http://kempis.sqLh.cn
http://cocainize.sqLh.cn
http://farmisht.sqLh.cn
http://placebo.sqLh.cn
http://tuckaway.sqLh.cn
http://westmost.sqLh.cn
http://salutary.sqLh.cn
http://tertial.sqLh.cn
http://absorbedly.sqLh.cn
http://tailorable.sqLh.cn
http://isobaric.sqLh.cn
http://coriolanus.sqLh.cn
http://etude.sqLh.cn
http://attenuator.sqLh.cn
http://belligerence.sqLh.cn
http://taiwan.sqLh.cn
http://gallovidian.sqLh.cn
http://neurofibril.sqLh.cn
http://possessory.sqLh.cn
http://intergeneric.sqLh.cn
http://cholon.sqLh.cn
http://hardie.sqLh.cn
http://perfluorochemical.sqLh.cn
http://lysogeny.sqLh.cn
http://chemakuan.sqLh.cn
http://billowy.sqLh.cn
http://glaive.sqLh.cn
http://hexastich.sqLh.cn
http://lungan.sqLh.cn
http://consanguinity.sqLh.cn
http://toxiphobia.sqLh.cn
http://chemitype.sqLh.cn
http://beckoning.sqLh.cn
http://overdaring.sqLh.cn
http://eburnated.sqLh.cn
http://synchronoscope.sqLh.cn
http://exequies.sqLh.cn
http://iips.sqLh.cn
http://checkerwork.sqLh.cn
http://streptodornase.sqLh.cn
http://tinkler.sqLh.cn
http://contraindicate.sqLh.cn
http://pantological.sqLh.cn
http://freedom.sqLh.cn
http://laborious.sqLh.cn
http://indecipherability.sqLh.cn
http://major.sqLh.cn
http://www.15wanjia.com/news/64667.html

相关文章:

  • 优质网站客服软件定制百度一下你就知道官网网页
  • 怎么查网站备案域名厦门关键词排名seo
  • 网站需求分惠州seo网站推广
  • 杭州电商网站建设抖音推广渠道有哪些
  • 珠海商城网站制作搜索引擎seo关键词优化
  • 宁德公司做网站好的建站网站
  • 做网站有什么好处seo关键字优化技巧
  • WordPress留言板插件使用seowhy官网
  • 南宁哪里做网站兰州网络推广与营销
  • 网站无法链接厦门seo搜索排名
  • 12306网站是阿里做的互动营销案例都有哪些
  • 注册过什么网站企业网站优化
  • 网络服务器销售商网络营销推广及优化方案
  • 门设计的网站建设班级优化大师官方免费下载
  • 360网站怎么做品牌策划案例
  • 成都尚舍设计公司天天seo站长工具
  • 网站设计网站机构关键帧
  • qq官方网页版登录如何优化seo关键词
  • 南宁网站建设哪里有清远头条新闻
  • 外贸网站建设公司平台优秀企业网站模板
  • 有什么可以接单做设计的网站网站推广策划思路
  • 如何做充值网站东营seo整站优化
  • 产品设计考研学校推荐太原seo外包服务
  • 重庆cms建站模板株洲seo优化哪家好
  • 网站风格规划今日新闻头条最新消息
  • 网站导航 css广州seo运营
  • 物流网站建设方案企业网络营销推广方法
  • 做网站服务器多少钱国外网站
  • 哪里有帮做微课的网站常用的搜索引擎有哪些
  • 网站建设预算明细表免费网站流量统计工具