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

建立网站后台知名的建站公司

建立网站后台,知名的建站公司,如何做古诗词网站,国外空间设计网站本栏目致力于从0开始使用纯C语言将经典算法转换成能够直接上机运行的程序,以项目的形式详细描述数据存储结构、算法实现和程序运行过程。 参考书目如下: 《数据结构C语言版-严蔚敏》 《数据结构算法解析第2版-高一凡》 软件工具: dev-cpp 0…

本栏目致力于从0开始使用纯C语言将经典算法转换成能够直接上机运行的程序,以项目的形式详细描述数据存储结构、算法实现和程序运行过程。

参考书目如下:

        《数据结构C语言版-严蔚敏》

        《数据结构算法解析第2版-高一凡》

软件工具:

        dev-cpp


0、准备工作

在项目下创建line.c和line.h文件。

1、线性表操作

1.1 准备工作line.h

定义线性表数组的长度和扩容量

// 线性表长度
#define LIST_INIT_SIZE 100
// 线性表扩容量
#define LISTINCREMENT 10

定义线性表结构体,存储线性表其实地址和线性表元素个数和线性表总长度。

typedef struct
{ElemType *elem;    // 线性表的起始地址 int length;       // 表中元素个数 int listsize;     // 表的总长度 
}SqList;

1.2 初始化链表

按照线性表的长度申请空间,并且初始化线性表元素个数和表的总长度。

//  初始化线型数组链表 
//  需要修改线性表,传入线性表地址 
Status InitList(SqList *L)
{// 按照表的长度申请空间,并赋值给*L的elem (*L).elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));// 申请失败,直接结束程序 if(!((*L).elem)){printf("数据空间申请失败!\n"); exit(OVERFLOW);}(*L).length = 0;(*L).listsize = LIST_INIT_SIZE;return OK;
}

1.3 插入数据

// 插入数据e在线性表的i位置上 1<=i<=n
//  需要修改线性表(分配空间已占满的时候需要扩容),传入线性表地址 

// 插入元素 1<=i<=n
//  需要修改线性表,传入线性表地址 
Status InsertList(SqList *L,int i,ElemType e) 
{ElemType *newbase,*p,*q; // 下标不合适返回失败 if(i<1 || i > (*L).length + 1)  return ERROR;// 当前分配的空间已占满,按增量重新分配 if((*L).length >= (*L).listsize) {newbase = (ElemType *)realloc((*L).elem,((*L).listsize + LISTINCREMENT) * sizeof(ElemType));if(!newbase){		printf("数据空间增量申请失败!\n"); exit(OVERFLOW);	}(*L).elem = newbase;(*L).listsize = (*L).listsize + LISTINCREMENT;}// 插入数据p = &(*L).elem[i-1];q = &(*L).elem[(*L).length+1];while(q > p){*q = *(q-1);q--; }*q = e;(*L).length++;return OK;
}

1.4 遍历列表

遍历列表过程中可能要做的事情不一样,因此将功能封装进函数中,然后将函数传入遍历函数中,进行执行。

// 链表的遍历 
Status ListTraverse(SqList L,void (*v)(ElemType *))
{int i;for(i=0;i<L.length;i++){v(L.elem+i);} printf("\n");return OK;
}

1.6 删除数据

// 删除元素 1<=i<=n
//  需要修改线性表,传入线性表地址 
// 通过e返回要删除的数值 
Status ListDelete(SqList *L,int i,ElemType *e) 
{	ElemType *p = NULL,*q=NULL; // 下标不合适返回失败 if(i<1 || i > (*L).length)  return ERROR;// 定位到要删除的位置p = &(*L).elem[i-1];*e = *p;q = &(*L).elem[(*L).length-1];// 将p指向空间,后面的数据向前移动。while(p <= q){*p = *(p+1);p++;} // 改变长度(*L).length--;return OK;	
}

1.7 销毁线性表

// 销毁线性表
//  需要修改线性表,传入线性表地址 
// 释放动态申请空间即可。
Status DestroyList(SqList *L) 
{// 释放堆区空间 free((*L).elem);// 将指针指向NULL,避免野指针。 (*L).elem = NULL;// 将线性表的长度的尺寸清0 (*L).length = 0;(*L).listsize = 0;return OK;
}

1.8 清空线性表

// 清空线性表
//  需要修改线性表,传入线性表地址 
// 将所有空间清为0
Status ClearList(SqList *L) 
{// 将空间里面的数据清0 int i;for(i=0;i<(*L).listsize;i++){(*L).elem[i] = 0;}// 将长度清0;(*L).length = 0; return OK;
}

1.9 判断线性表是否为空

// 判断线性表是否为空, 空表返回TRUE,否则返回FALSE
//  不需要修改线性表,仅用于访问,传入线性表即可 
Status ListEmpty(SqList L) 
{return L.length == 0 ? TRUE : FALSE; 
}

1.10 获取线性表的长度

// 获取 线性表长度
//  不需要修改线性表,仅用于访问,传入线性表即可 
Status ListLength(SqList L) 
{return L.length;
}

1.11 获取某个位置的数据

// 获取某个下标对应的元素 1<=i<=n
//  不需要修改线性表,仅用于访问,传入线性表即可 
Status GetElem(SqList L,int i,ElemType *e) 
{// 下标不合适返回失败 if(i<1 || i > L.length)  return ERROR;*e = L.elem[i-1];return OK; 
}

1.12 按照条件查找某个元素所在位置

将条件判断关系封装在函数内部,通过函数指针的形式传入到查找函数内。

// 查找元素e所在的位序, 1<=i<=n;如果找不到,返回0 
Status LocateElem(SqList L,ElemType e,Status (*compare)(ElemType,ElemType))
{int i;for(i=0;i<L.length;i++){if(compare(L.elem[i],e)){return i+1;} } return 0;
}

1.13 查找元素的前驱元素

// 查找元素的前驱
// 不是第一个元素有前驱 ,如果是第一个或者找不到则没有意义 
Status  PriorElem(SqList L,ElemType cur_e,ElemType *prev_e) 
{// idx为位序  1<=i<=n;int idx = LocateElem(L,cur_e,equal);if(idx <= 1) {return INFEASIBLE;}else{*prev_e = L.elem[idx-2];return OK;}	
}

1.14 查找元素的后驱元素

// 查找元素的后继 
Status  NextElem(SqList L,ElemType cur_e,ElemType *next_e) 
{// idx为位序  1<=i<=n;int idx = LocateElem(L,cur_e,equal);if(idx >= L.length) {		return INFEASIBLE;}else{*next_e = L.elem[idx];return OK;}	
}

1.15 两个线性表的并集

// 求两个线性表的并集
void unionList(SqList *La,SqList Lb) 
{int la_len = ListLength(*La);int lb_len = ListLength(Lb);int i;ElemType e;for(i=1;i<=lb_len;i++){// 将数据从Lb中读出放入到e里面去 GetElem(Lb,i,&e);// 检测e数据在不在La中。if(!LocateElem(*La,e,equal)) {// 不在将e放入到La中InsertList(La,++la_len,e); }}
}

1.16 两个有序线性表的合并

// 两个递增有序链表的合并。
void MergeList(SqList La,SqList Lb,SqList *Lc) 
{// 初始化LcInitList(Lc);int la_len = ListLength(La);int lb_len = ListLength(Lb); int i=1,j=1,k=0;ElemType ai,bj;while((i<=La.length) && (j <= Lb.length)){GetElem(La,i,&ai);GetElem(Lb,j,&bj);if(ai <= bj){InsertList(Lc,++k,ai);++i;}else{InsertList(Lc,++k,bj);++j;}}while(i <= La.length){GetElem(La,i++,&ai);InsertList(Lc,++k,ai);}while(j <= Lb.length){GetElem(Lb,j++,&bj);InsertList(Lc,++k,bj);}
}

1.17 判断两个数是否相等-查找辅助函数

// 辅助函数 -- 两个数据的相等判断 
Status equal(ElemType e1,ElemType e2)
{return e1 == e2 ? TRUE : FALSE;
}

1.18 访问数据--遍历的辅助函数

// 遍历辅助函数
void visit(ElemType *e)
{printf("%d ",*e);
} 

1.19 测试功能代码

void line_test(void)
{SqList l;InitList(&l);srand((unsigned int)time(NULL));	// 测试插入 int i;for(i=1;i<=5;i++){InsertList(&l,i,rand() % 10);} l.length = 5; printf("原始数据如下:");ListTraverse(l,visit);	// 接受删除的数据 int res;// 删除位置 int idx = rand() % 5 + 1; printf("删除位置:%d\n",idx);ListDelete(&l,idx,&res);printf("删除的数字是:%d,删除后:",res);ListTraverse(l,visit);//	ClearList(&l);
//	for(i=0;i<10;i++)
//	{
//		printf("%d ",l.elem[i]);
//	} 
//	printf("\n");// 线性表为空和 线性表长度测试if(ListEmpty(l)){printf("当前链表为空\n");} else{printf("链表当前的长度为:%d\n",ListLength(l));}// 获取某个元素测试int val;GetElem(l,1,&val);printf("第1个元素是:%d\n",val); // 查找元素的位序 测试int index = LocateElem(l,5,equal);printf("查找到5在数组的位序为:%d\n",index); // 查找元素的前驱测试Status res1;res1 = PriorElem(l,l.elem[0],&val);if(res1 != INFEASIBLE){printf("l.elem[3]的前驱是:%d\n",val);}// 查找元素的后继测试res1 = NextElem(l,l.elem[0],&val);if(res1 != INFEASIBLE){printf("l.elem[3]的后继是:%d\n",val);}// 两个线性表合并测试SqList l1,l2;InitList(&l1); InitList(&l2);int j;for(j=0;j<5;j++){l1.elem[j] = rand() % 10;} l1.length = 5; printf("线性表1:"); ListTraverse(l1,visit);for(j=0;j<3;j++){l2.elem[j] = rand() % 10;} l2.length = 3; printf("线性表2:");ListTraverse(l2,visit);unionList(&l1,l2);printf("合并后,线性表1:"); ListTraverse(l1,visit);// 两个增序线性表合并检测SqList l3,l4,l5;InitList(&l3); InitList(&l4);	InitList(&l5);	l3.elem[0] = 3;l3.elem[1] = 5;l3.elem[2] = 8;l3.elem[3] = 11;l3.length = 4;printf("线性表l3为:");ListTraverse(l3,visit);l4.elem[0] = 2;l4.elem[1] = 6;l4.elem[2] = 8;l4.elem[3] = 9;l4.elem[4] = 11;l4.elem[5] = 15;l4.elem[6] = 20;l4.length = 7;printf("线性表l4为:");ListTraverse(l4,visit);MergeList(l3,l4,&l5);printf("合并有序线性表l3和l4后为:");ListTraverse(l5,visit);
} 

 项目仓库地址

https://gitee.com/amyliyanice/data_struct.git


文章转载自:
http://bignonia.rywn.cn
http://aircraft.rywn.cn
http://headache.rywn.cn
http://certifiable.rywn.cn
http://seigniorage.rywn.cn
http://peccary.rywn.cn
http://flurry.rywn.cn
http://ocker.rywn.cn
http://tyne.rywn.cn
http://parsimoniously.rywn.cn
http://hogpen.rywn.cn
http://scandalmonger.rywn.cn
http://whack.rywn.cn
http://returnless.rywn.cn
http://scull.rywn.cn
http://thalassography.rywn.cn
http://hazardous.rywn.cn
http://baroscope.rywn.cn
http://cradlesong.rywn.cn
http://pregenital.rywn.cn
http://demagoguery.rywn.cn
http://diapause.rywn.cn
http://counterpoise.rywn.cn
http://lazurite.rywn.cn
http://vito.rywn.cn
http://oxytetracycline.rywn.cn
http://lachrymose.rywn.cn
http://dizen.rywn.cn
http://churr.rywn.cn
http://freightage.rywn.cn
http://holpen.rywn.cn
http://fugle.rywn.cn
http://bromelia.rywn.cn
http://emulational.rywn.cn
http://greensand.rywn.cn
http://perpendicularity.rywn.cn
http://tankman.rywn.cn
http://priestcraft.rywn.cn
http://farm.rywn.cn
http://formosan.rywn.cn
http://intraspecies.rywn.cn
http://unsuspicious.rywn.cn
http://scyros.rywn.cn
http://etep.rywn.cn
http://cerebra.rywn.cn
http://barbule.rywn.cn
http://scuzz.rywn.cn
http://primiparity.rywn.cn
http://zygocactus.rywn.cn
http://disseizee.rywn.cn
http://shelleyan.rywn.cn
http://rockwork.rywn.cn
http://eiger.rywn.cn
http://crown.rywn.cn
http://episepalous.rywn.cn
http://dander.rywn.cn
http://catalogue.rywn.cn
http://wakefield.rywn.cn
http://enduringly.rywn.cn
http://myxomatosis.rywn.cn
http://microbarograph.rywn.cn
http://semidrying.rywn.cn
http://ergal.rywn.cn
http://abutting.rywn.cn
http://spyglass.rywn.cn
http://astucious.rywn.cn
http://christmas.rywn.cn
http://bespoke.rywn.cn
http://subopposite.rywn.cn
http://mpe.rywn.cn
http://nomad.rywn.cn
http://bratty.rywn.cn
http://fierily.rywn.cn
http://myeloperoxidase.rywn.cn
http://entopic.rywn.cn
http://puckery.rywn.cn
http://energize.rywn.cn
http://seismal.rywn.cn
http://satiny.rywn.cn
http://eponymist.rywn.cn
http://stalagmite.rywn.cn
http://bypath.rywn.cn
http://cardigan.rywn.cn
http://neologist.rywn.cn
http://geriatrician.rywn.cn
http://unwariness.rywn.cn
http://twimc.rywn.cn
http://puerile.rywn.cn
http://saddle.rywn.cn
http://leucopoiesis.rywn.cn
http://galactoscope.rywn.cn
http://femme.rywn.cn
http://yearningly.rywn.cn
http://cicatricial.rywn.cn
http://cult.rywn.cn
http://buddhistic.rywn.cn
http://assyriology.rywn.cn
http://blusher.rywn.cn
http://dateable.rywn.cn
http://sliding.rywn.cn
http://www.15wanjia.com/news/90518.html

相关文章:

  • 深圳网站建设选哪家好网络推广员要怎么做
  • 做天猫网站价格表百度站长工具排名
  • 长春设计网站优网营销
  • 电商网站建设方案百度竞价客服
  • 通用精品课程网站建设的需求分析seo是什么专业的课程
  • 构建动态网站设计的理解班级优化大师app
  • 无锡网站建设价格低广州品牌营销策划公司排名
  • 网站建设用php建设优点哪里可以引流到精准客户呢
  • 珠海高端网站设计网络销售怎么做才能做好
  • wordpress子目录站点广州官方新闻
  • 国外网站怎么做近期重大新闻
  • 如何接做网站编程的生意电商大数据查询平台
  • 免费做网站有哪些家谷歌推广方案
  • 国内做视频的网站有哪些备案域名购买
  • 网站开发综合实训心得营销软文范例500
  • 第四章第二节网站建设的教学设计商家怎么入驻百度
  • 织梦网站建设案例如何做电商 个人
  • 做招聘图片的网站站长工具精品
  • 自己有主机怎么做论坛网站百度推广登录入口
  • 深圳自助网站建设费用百度云链接
  • 高端的网站建设公司微博推广方法有哪些
  • 专门卖电子产品的网站申请域名
  • 传统企业如果建立网站软文素材
  • 北京上海网站建设天津百度推广代理商
  • 义乌网站建设软件开发天津百度爱采购
  • 淘宝网站怎么做的好坏网络广告策划方案范文
  • 上海设计招聘网站上海专业seo排名优化
  • 哪个网站是专门做装修的广州seo网络营销培训
  • wordpress新手技巧常州seo博客
  • 主流的网站开发技术百度云登录入口