闲来无事,练练手,写点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-线性顺序表的增删改查的更多相关文章

  1. Hibernate5笔记2--单表的增删改查操作

    单表的增删改查操作: (1)定义获取Session和SessionFactory的工具类: package com.tongji.utils; import org.hibernate.Session ...

  2. Mysql数据表的增删改查

    ---恢复内容开始--- Mysql数据表的增删改查 1.创建表   语法:CREATE TABLE 表名(字段1,字段2,字段3.......) CREATE TABLE `users` ( `us ...

  3. Django学习笔记(10)——Book单表的增删改查页面

    一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...

  4. ORM 实现数据库表的增删改查

    这次通过反射技术来实现一下数据库表的增删改查对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 注:引用时约束了以下几点: 数据 ...

  5. spool命令、创建一个表,创建而且copy表,查看别的用户下的表,rowid行地址 索引的时候使用,表的增删改查,删除表,oracle的回收站

      1.spool命令 spool "D:\test.txt" spool off SQL> host cls 2.创建一个表 SQL> --条件(1):有创建 ...

  6. python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)

    day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...

  7. AngularJS中使用$http对MongoLab数据表进行增删改查

    本篇体验使用AngularJS中的$http对MongoLab数据表进行增删改查. 主页面: <button ng-click="loadCourse()">Load ...

  8. day 57 data 插件 表的增删改查

    一 data的含义 在匹配的元素集合中的所有元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值.      1 .data(key, value):     描述:在匹配 ...

  9. mysql 数据表的增删改查 目录

    mysql 表的增删改查 mysql 表的增删改查 修改表结构 mysql 复制表 mysql 删除表

随机推荐

  1. Ubuntu 中安装 NetBeans IDE

    NetBeans 8.2 刚刚发布,如果你还没有安装的话,这篇简短的教程将会演示如何在 Ubuntu 系统上快速的安装.对安装 NetBeans 需要帮助的开发者来说,这往篇文章会是不错的指导. 给那 ...

  2. qt-4.8.5 显示图片居中笔记

    已经太久没有写过qt的程序了,所以导致的后果就是一个很简单的程序写了老半天还没写完整. 今天想实现的功能在原来软件的基础上显示他的版本. 因为想在该界面显示一个logo,一开始在pc机上跑发现图片一直 ...

  3. BZOJ 1044: [HAOI2008]木棍分割

    Description 求 \(n\) 根木棍长度为 \(L\) ,分成 \(m\) 份,使最长长度最短,并求出方案数. Sol 二分+DP. 二分很简单啊,然后就是方案数的求法. 状态就是 \(f[ ...

  4. ubuntu14.04显卡驱动问题(amd5600k集显7650d)

    安装ubuntu的时候,多次莫名其妙地黑屏,起初以为是最新得ubuntu14.04安装包不稳定,所以一直尝试了很多次. 重启-->安装-->黑屏(硬盘仍在转动,屏幕上什么都没有)--> ...

  5. UOJ 做题记录

    UOJ 做题记录 其实我这么弱> >根本不会做题呢> > #21. [UR #1]缩进优化 其实想想还是一道非常丝播的题目呢> > 直接对于每个缩进长度统计一遍就好 ...

  6. SSM 集成的两个配置文件

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> < ...

  7. 堆栈指针 ---delete 使用

    对拥有堆中一个有效对象的地址的指针进行删除操作的结果,是将这个堆内存的状态从“使用中” 变为“可用”(此时的可用就是指可以调用内存)释放了,可以再次覆盖此处;;     对指针内存进行删除操作后,指针 ...

  8. CEF3开发者系列之JS与C++交互之一

    JS与Native交互是相对于比较困难的技术,在学习这门技术之前,我们先了解下浏览器内核中的JS引擎与chromium内核的V8引擎相关知识.在浏览器应用中,JS与本地代码互相调用,得益于浏览器内核对 ...

  9. iptables之链之间的跳转

    创建一个新的链     按照管理,用户自定义的链用小写来区分它们 iptables -N newchain 可以在这个链的尾部跳转到INPUT链 iptables -A newchain -j INP ...

  10. android打电话的小程序

    主要使用了Intent以及uses-permission标签. call.rar 下载后直接导入项目