定长顺序表

头文件

#pragma once#define SIZE 10
typedef struct SeqList
{int elem[SIZE];//保存数据int length;//有效数据个数
}SeqList,*PSeqList ;//typedef SeqList*  PSeqList;//PSeqList == SeqList*void InitSeqList(PSeqList plist);//初始化bool Insert(PSeqList plist,int pos,int val);//在指定位置插入元素int Search(PSeqList plist,int key);//查找元素bool DeleteVal(PSeqList plist,int key);//删除元素bool Deletepos(PSeqList plist,int pos,int *rtval);//删除位置位//rtval:输出参数,保存删除成功void Show(PSeqList plist);//打印bool IsEmpty(PSeqList plist);//判空bool IsFull(PSeqList plist);//判满
//获取迫使下标储存的数据
bool GetVal(PSeqList plist,int pos,int val);
//修改pos下标的数据
bool SetVal(PSeqList plist,int pos,int newval);
//获取关键字前驱的值
bool GetPriVal(PSeqList plist,int key,int *rtval);
//获取关键字后继的值
bool GetNextVal(PSeqList plist,int key,int *rtval);
//获取当前长度
int GetLength(PSeqList plist);
//清空所有数据
void Clear(PSeqList plist);
//销毁顺序表
void Destory(PSeqList plist);

 


cpp文件 

#include<stdio.h>
#include<assert.h>
#include"seqlist.h"void InitSeqList(PSeqList plist)//初始化
{assert(plist != NULL);plist->length = 0;
}bool Insert(PSeqList plist,int pos,int val)//在指定位置插入元素
{assert(plist != NULL);if(pos < 0 || pos > plist->length || plist ->length == SIZE){return false;}//移动数据for(int i =plist-> length-1; i>=pos;i--){plist->elem[i+1] = plist->elem[i];}//插入新数据plist->elem[pos] = val;//更新新数据plist->length ++;
}int Search(PSeqList plist,int key)//查找元素
{assert(plist != NULL);for(int i = 0;i<plist->length-1;i++){if(plist->elem[i] == key){return i;//找到返回下标}}return -1;//找不到返回-1
}//删除位置位
//rtval:输出参数,保存删除成功
bool DeletePos(PSeqList plist,int pos,int *rtval)
{assert(plist != NULL);if(pos<0 || pos >plist->length){return false;}if(rtval != NULL){*rtval = plist->elem[pos];}//往前移动数据for(int i = pos;i<plist->length-1;i++){plist->elem[i] = plist ->elem[i+1];}//更新数据长度plist->length --;return true;
}bool DeleteVal(PSeqList plist,int key)//删除元素
{int i = Search(plist,key);if( i < 0 ){return false;}return DeletePos(plist,i,NULL);}void Show(PSeqList plist)//打印
{assert(plist != NULL);for(int i=0;i <plist->length;i++){printf("%d ",plist->elem[i]);}printf("\n");
}bool IsEmpty(PSeqList plist)//判空
{return plist->length ==0;
}static bool IsFull(PSeqList plist)//判满
{return plist->length == SIZE;
}
//获取pos下标储存的数据
bool GetVal(PSeqList plist,int pos,int *rtval)
{assert(plist != NULL);if(pos <0 || pos>=plist->length){return false;}if(rtval != NULL){*rtval = plist->elem[pos];}return true;
}
//修改pos下标的数据
bool SetVal(PSeqList plist,int pos,int newval)
{assert(plist != NULL);if(pos<0 || pos>=plist->length){return false;}plist->elem[pos] = newval;return true;
}
bool GetPriVal(PSeqList plist,int key,int *rtval)
{int i = Search(plist,key);if(i <= 0){return false;}if(rtval != NULL){*rtval = plist->elem[i-1];}return true;
}bool GetNextVal(PSeqList plist,int key,int *rtval)
{int i = Search(plist,key);if(i < 0 || i == plist->length-1){return false;}if(rtval != NULL){*rtval = plist->elem[i+1];}return true;
}int GetLength(PSeqList plist)
{return plist->length;
}void Clear(PSeqList plist)
{plist->length = 0;
}void Destory(PSeqList plist)
{Clear(plist);
}

主函数

#include"seqlist.h"
#include"stdio.h"int main()
{SeqList seq;InitSeqList(&seq);for(int i = 0;i<20;i++){Insert(&seq,i,i);}Show(&seq);Search(&seq,7);//查找元素DeleteVal(&seq,7);//删除元素Show(&seq);Insert(&seq,3,30);Show(&seq);Clear(&seq);Show(&seq);return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/526130.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

不定长顺序表

头文件 pragma once#define INIT_SIZE 10typedef struct DSeqList {int *elem;//指向保存数据的内存int length;//有效数据个数int listsize;//总格子数 }DSeqList;typedef DSeqList * PDSeqList;//初始化函数 void InitDSeqList(PDSeqList plist);//往plist的pos位置插入数据…

单链表(带头节点)

带头结点单链表的内存分布情况 头文件 #pragma once //带头节点的单链表 //单链表尾节点的next为NULL //List为一条链表&#xff1b;Node* 一个节点的地址 typedef struct Node {int data;//数据Node *next;//下一个节点的地址 }Node ,*List ;//List Node *//初始化 void Ini…

双向链表(带头结点)

带头结点双向链表的内存分布情况 头文件 #pragma once //双向链表 typedef struct DNode {int data;DNode* next;DNode* prio; }DNode , *DList ;//初始化 void InitList(DList plist);//头插法 bool Insert_head(DList plist,int val);//尾插法 bool Insert_tail(DList plist…

给出一个数:(1)求解有几位数,(2)分别输出每一位数字(3)按逆序输出各位数字

目录 &#xff08;1&#xff09;求解有几位数 (2)分别输出每一位数字 (3)按逆序输出各位数字 &#xff08;1&#xff09;求解有几位数 #include<stdio.h>unsigned int GetFigures(int n) {unsigned int figures 0; while(n!0){n / 10;figures;}return figures; }int …

栈(不定长顺序表)

栈是一种数据结构&#xff0c;是一种只能在一端进行插入和删除操作的特殊线性表。它按照后进先出的原则存储数据&#xff0c;先进入的数据被压入栈底&#xff0c;最后的数据在栈顶&#xff0c;需要读数据的时候从栈顶开始弹出数据&#xff08;最后一个数据被第一个读出来&#…

c++两个数组对比去掉重复的元素_LeetCode 题解 | 167.两数之和 II 输入有序数组...

点击上方蓝字设为星标下面开始今天的学习&#xff5e;力扣 167.两数之和 II - 输入有序数组(点击文末阅读原文查看题目)题目描述给定一个已按照 升序排列 的有序数组&#xff0c;找到两个数使得它们相加之和等于目标数。函数应该返回这两个下标值 index1 和 index2&#xff0c;…

循环队列(线性)

队列遵循先进先出的原则&#xff0c;一般从队尾插入&#xff0c;从队头删除。指向队头的指针称为front&#xff0c;队尾指针为rear。&#xff08;目的是为了方便插入和删除元素&#xff09; 假设队列一共有五个元素&#xff0c;当没有元素的时候&#xff0c;front和rear指针都指…

如何c51和mdk共存兼容_2020年网站如何做seo优化

原标题&#xff1a;2020年网站如何做seo优化2017年SEO优化已不再是简单按部就班的去做优化了&#xff0c;随着搜索引擎算法的不断改进和用户搜索习惯的变化&#xff0c;如今的SEO优化更复杂了&#xff0c;那么除了传统的页面元素外&#xff0c;SEO优化还应该注意哪些问题呢?今…

thinkphp仿素材火教程_国外都用古风效果图获奖了,为什么你连素材都没有?

又到了学生党们开学的日子了现在就有小伙伴问台小妹要做效果图了有没有好看的资源有&#xff0c;当然有&#xff0c;必须有&#xff01;文末附古风常用素材及图纸合集说到好看&#xff0c;就要说到一直都很火的古风元素不管是前段时间上映的电影《哪吒》还是之前的古装剧《延禧…

队列(单链表)

头文件 #pragma once//利用带头节点的单链表实现队列&#xff0c;队头为第一个数据节点typedef struct Node {int data;struct Node *next; }Node;//数据节点typedef struct HNode {struct Node *front;//队头指针struct Node *rear;//队尾指针 }HNode,*PLQueue;//头节点void …

c# 微服务学习_资深架构师学习笔记:什么是微服务?

们先来看看为什么要考虑使用微服务。构建单体应用我们假设&#xff0c;您开始开发一个打车应用&#xff0c;打算与 Uber 和 Hailo 竞争。经过初步交流和需求收集&#xff0c;您开始手动或者使用类似 Rails、Spring Boot、Play 或者 Maven 等平台来生成一个新项目。该新应用是一…

两栈共享空间

两个相同类型的栈&#xff0c;可能第一个栈已经满了&#xff0c;但是第二个栈还是空的&#xff0c;将两个相同类型的栈合并在一起&#xff0c;可以节省一部分空间。 数组有两个端点&#xff0c;分别为两个栈的栈底&#xff0c;一个栈的栈底的位置为数组为0的地方&#xff0c;另…

crontab shell 每5秒执行_视频 |全球最快全自动播种分拣机器人,每5秒处理一件货物...

平均每5秒处理一件货物玻璃杯、服装、化妆品轻松吸取能应对海量SKU的播种分拣机器人▼视频中的机器人是XYZ Robotics独立开发的一款全自动仓储分拣机器人Putwall Sorting。该产品已于2018年12月推出&#xff0c;应用于欧莱雅。它将是全球范围内&#xff0c;处理速度最快、可应对…

僵尸进程和守护进程

僵死进程&#xff1a;父进程未结束&#xff0c;子进程已结束&#xff0c;并且父进程已经获取子进程的退出状态。这种进程&#xff0c;进程主体空间已经释放&#xff0c;只有PCB还未释放。 处理方法&#xff1a; &#xff08;1&#xff09;父进程调用wait函数或者waitpid函数获…

python3.6里有xhr吗_python – XHR请求URL在尝试解析其内容时不存在

你有几个问题&#xff1a;>网址应为http://www.whoscored.com/stageplayerstatfeed>错误的GET参数>缺少重要的标题>你需要response.json(),而不是response.body固定版本&#xff1a;import requestsurl http://www.whoscored.com/stageplayerstatfeedparams {fie…

GDB调试(基本命令)

GDB调试分为两种模式&#xff0c;一种是debug版本&#xff0c;一种是release版本。一般GDB主要调试的是C/C的程序。 &#xff08;1&#xff09;debug版本&#xff1a;debug版本为可调式版本&#xff0c;生成的可执行文件中包含调试需要的信息。 &#xff08;2&#xff09;rel…

unity底层运行机制_Unity跨平台的机制原理

首先需要了解的是&#xff0c;Unity3D的C#基础脚本模块是通过Mono来实现的。什么是Mono&#xff1f;参考下百度百科&#xff1a;Mono是一个由Novell公司(由Xamarin发起)主持的项目&#xff0c;并由Miguel de lcaza领导的&#xff0c;一个致力于开创.NET在Linux上使用的开源工程…

pbl和sbl_谈PBL和SBL教学法结合模式

&#xff11;教学效果评估完成全部教学内容后两组进行统一测评。测试包括基础理论知识客观题和病例分析考试题成绩进行比较。对学生进行问卷调查&#xff0c;测评学生的满意度&#xff0c;包括学习兴趣、自主学习能力、临床思维能力和团队合作精神&#xff14;项&#xff0c;每…

求Sn = a+aa+aaa+...+aaaaaa(n个a),其中a是一个数字,n代表a的位数,例如 2+22+222+2222+22222(此时n=5),n由键盘输入。

求Sn aaaaaa...aaaaaa(n个a)&#xff0c;其中a是一个数字&#xff0c;n代表a的位数&#xff0c;例如 222222222222222(此时n5)&#xff0c;n由键盘输入。 #include<stdio.h> #include<math.h>//通过a和n构造aa...a这样的数字 static int GetNum(int a,int n) {in…

python字符串乘一个数_Python--初识庐山真面目

基础知识变量只能自上而下&#xff0c;比如&#xff1a;y2 print(xy) x3 # 错误&#xff0c;Python只能自上而下读取变量 -------------------------------------------------- y2 x3​​ print(xy # 正确​​变量名的第一个字符不能是数字关键字不能当做…