C-线性顺序表的增删改查
闲来无事,练练手,写点C代码,对于线性表的简单操作。编辑工具Notpad++,编译工具tcc.
/*
*the sequence of the list
*author:JanneLee
*data:2013-10-26
*/ #include <stdio.h>
#include <stdlib.h>/*rand()*/
#include <time.h>/*time()*/
#ifdef DEBUG
#include <assert.h>
#endif
#define MAX_SIZE 1000
#define N 10
typedef struct Sqlist{
int data[MAX_SIZE];
int length;
}Sqlist;
int InsertElement(Sqlist *s,int pos,int val);
int DelElemByPos(Sqlist *s,int pos);
int DelElemByVal(Sqlist *s,int val);
int UpdateElemByPos(Sqlist *s,int pos,int val);
int FindElemByVal(Sqlist s,int val);
int ShowSqlist(Sqlist s);
int main(){
printf("====program begin========\n");
Sqlist s;
s.length=;
//InsertElement(&s,0,10);
//InsertElement(&s,1,20);
/*generate a sequence with random value*/
/*begin*/
int i=;
srand(time(NULL));
for(;i<N;i++){
InsertElement(&s,i,rand()%N);
}
/*end*/
//DelElemByPos(&s,3);
//DelElemByVal(&s,3);
UpdateElemByPos(&s,,);
printf("pos:%d\n",FindElemByVal(s,));
ShowSqlist(s);
printf("======program end======\n");
return ;
}
/*
*function name:InsertElement
*feature:insert a value in the position of pos
*parameters:*s-the sequence of the list,pos-the insert position,val-the value
*there if the c++ we can use &s,the c we shoud use star s to make the s change.
*return: if 1-insert succeed,if 0 the failed
*author:Jannelee
*data:2013-10-26
*/
int InsertElement(Sqlist *s,int const pos,int const val){
int ret_flag=;
if(pos>MAX_SIZE||pos<){
ret_flag=;
}else{
int i=;
for(i=pos;i<s->length;i++){
s->data[i+]=s->data[i];
}
s->data[pos]=val;
s->length++;
}
return ret_flag;
}
/*
*function name:ShowSqlist
*feature:show the sequence of the list
*parameters:s-the sequence
*return :1-succeed,0-error
*author:JanneLee
*date:2013-10-26
*/
int ShowSqlist(Sqlist const s){
int ret_flag=;
printf("=========show the Sqlist===========\n");
int i=;
for(;i<s.length;i++){
printf("%d ",s.data[i]);
}
printf("\n==========the show end=============\n");
return ret_flag;
}
/*
*function name:DelElemByPos
*feature:delete the element by the position of the element in the sequence.
*parameters:*s- the sequence ,pos-the position for delete
*return : if 1- succeed ,0-faild
*author:JanneLee
*date:2013-10-26
*/
int DelElemByPos(Sqlist *s,int pos){
int ret_flag=;
if(pos<||s->length<pos){
ret_flag=;
}else{
int i=;
for(;i<s->length;i++){
s->data[i]=s->data[i+];
}
s->length--;
}
return ret_flag;
}
/*
*function name:DelElemByVal
*feature:delete the elements by the value in the sequence
*this may delect every elements have the same value of val in the sequence.
*parameters:*s - the sequence , val- the value
*reutrn : 1- succeed ,0 -faild
*author:JanneLee
*date:2013-10-26
*/
int DelElemByVal(Sqlist *s ,int val){
int ret_flag=;
int i=,j=;
for(;i<s->length;i++){
if(s->data[i]==val){
for(j=i;j<s->length;j++){
s->data[j]=s->data[j+];
s->length--;
}
}else{
ret_flag=;
}
}
return ret_flag;
}
/*
*function name:UpdateElemByPos
*feature:update the value of the sequence int the position of pos
*parameters:*s - the sequence,pos-the position,val-the value
*return:-1-no this value,0-faild
*author:JanneLee
*date:2013-10-26
*/
int UpdateElemByPos(Sqlist *s,int pos,int val){
int ret_flag=;
if(pos<||pos>s->length){
ret_flag=;
}else{
s->data[pos]=val;
}
return ret_flag;
}
/*
*function name:FindElemByVal
*feature:get the vale of the position in the sequence,if the value has repeat in the
*sequence ,just find the first position in the sequence.
*parameters:s - the sequence,val-the value
*return:-1-no this value,otherwise ,the position of the value
*author:JanneLee
*date:2013-10-26
*/
int FindElemByVal(Sqlist s,int val){
printf("the sequence length:%d\n",s.length);
int ret_pos=-;
int i=;
for(;i<s.length;i++){
if(val==s.data[i]){
ret_pos=i+;
i=s.length+;
}
}
return ret_pos;
}
C-线性顺序表的增删改查的更多相关文章
- Hibernate5笔记2--单表的增删改查操作
单表的增删改查操作: (1)定义获取Session和SessionFactory的工具类: package com.tongji.utils; import org.hibernate.Session ...
- Mysql数据表的增删改查
---恢复内容开始--- Mysql数据表的增删改查 1.创建表 语法:CREATE TABLE 表名(字段1,字段2,字段3.......) CREATE TABLE `users` ( `us ...
- Django学习笔记(10)——Book单表的增删改查页面
一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...
- ORM 实现数据库表的增删改查
这次通过反射技术来实现一下数据库表的增删改查对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 注:引用时约束了以下几点: 数据 ...
- spool命令、创建一个表,创建而且copy表,查看别的用户下的表,rowid行地址 索引的时候使用,表的增删改查,删除表,oracle的回收站
1.spool命令 spool "D:\test.txt" spool off SQL> host cls 2.创建一个表 SQL> --条件(1):有创建 ...
- python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)
day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...
- AngularJS中使用$http对MongoLab数据表进行增删改查
本篇体验使用AngularJS中的$http对MongoLab数据表进行增删改查. 主页面: <button ng-click="loadCourse()">Load ...
- day 57 data 插件 表的增删改查
一 data的含义 在匹配的元素集合中的所有元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值. 1 .data(key, value): 描述:在匹配 ...
- mysql 数据表的增删改查 目录
mysql 表的增删改查 mysql 表的增删改查 修改表结构 mysql 复制表 mysql 删除表
随机推荐
- 大数据热点问题TOP K
1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...
- centos 设置永久dns
最近在折腾一个问题. 由于服务器的带宽是联通5M, 不稳定.而且所处的网络的dns解析貌似老出问题,每隔一定周期解析时间特别长. 于是乎,想在本地做一个dns,这样可以减少dns解析时间,并做些静态配 ...
- centos 终端 字体颜色
默认情况下,没有颜色. https://www.centos.org/docs/2/rhl-gsg-en-7.2/ls-color.html git也默认没有颜色,破解如下: git config - ...
- 应用HTK搭建语音拨号系统3:创建绑定状态的三音素HMM模型
选自:http://maotong.blog.hexun.com/6261873_d.html 苏统华 哈尔滨工业大学人工智能研究室 2006年10月30日 声明:版权所有,转载请注明作者和来源 该系 ...
- win7下配置Apache本地虚拟主机
我们有时候从网上下载下来的php源码很多都是应用在网站根目录下的,而我们又想在本地先测试一遍确定没有问题了再上传空间,但一换到子目录下的时候因为路径问题,使得许多图片.内容都无法显示. 这个时候我们就 ...
- ubuntu14.04安装django
1) sudo apt-get install python-pip#安装pip 2) pip install Django==1.8.1
- 【云计算】docker前世今生
下一代云计算模式:Docker正掀起个性化商业革命 作者: 吴宁川 来源: ITValue 发布时间: 2015-09-20 10:41 阅读: 12976 次 推荐: 24 ...
- 《Head First Servlet JSP》学习笔记二
一. 二. 三. 四. 五. 六. 七. 八. 九. 十. 十一. 十二.
- poll函数
poll函数与select函数的功能基本一样,其定义如下: #include <poll.h> int poll(struct pollfd fds[], nfds_t nfds, int ...
- CentOS 7部署flume
CentOS 7部署flume 准备工作: 安装java并设置java环境变量,在`/etc/profile`中加入 export JAVA_HOME=/usr/java/jdk1.8.0_65 ex ...