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 删除表
随机推荐
- DMZ
DMZ是英文“demilitarized zone”的缩写,中文名称为“隔离区”,也称“非军事化区”.它是为了解决安装防火墙后外部网络的访问用户不能访问内部网络服务器的问题,而设立的一个非安全系统与安 ...
- minigui交叉编译整理
简介 MiniGUI 是一款面向嵌入式系统的高级窗口系统(Windowing System)和图形用户界面(Graphical User Interface,GUI)支持系统,由魏永明先生于 1998 ...
- poj4052
题意:求一个文章(长度5.1e6)里面出现了多少个指定的模式串.重复出现只记一次.而且如果两个模式串都出现的情况下,一个是另一个的子串,则该子串不算出现过. 分析:AC自动机. 由于子串不算所以加一些 ...
- the XMLHttpRequest Object
Ajax的核心是XMLHttpRequest对象.XMLHttpRequest对象允许异步发送请求并且以文本的形式返回结果.发送什么样的请求(what),在服务器端怎样处理这个请求(how),返回什么 ...
- ios UIButton shadowcolor 导致黑边问题
注意这个属性,会导致按钮文字有一定黑边,其实就是阴影效果,如果不是想要的效果,应该把它设置为clearcolor.这种情况在亮色背景下比较突出.
- 项目管理工具~Jira
作用:工程管理 提交BUG 描述,截图,记录BUG ID 自定义DashBoard 添加Gadget 自定义布局 统计要素 TimeSheet 1.组内人力使用分布 2.员工工作量 Jira 过滤器设 ...
- ABAP 内表的行列转换-发货通知单2
*&---------------------------------------------------------------------* *& Report Z_TEST_C ...
- Divide and conquer:Aggressive Cows(POJ 2456)
侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 #include <iostream> #include <algorithm> #include < ...
- Hibernate双向多对多对象关系模型映射
1 双向many-to-many 业务模型: 描述员工和项目 一个员工同时可以参与多个项目 一个项目中可以包含多个员工 分析:数据库的数据模型,通过中间关系表,建立两个one-to-many构成man ...
- struts.xml配置
1. package标签 package:完成有业务相关的Action(应用控制器的)管理 name:给包起的名字(反映该包中Action的功能),用来完成包和包之间的继承.默认继承struts-de ...