闲来无事,练练手,写点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. 15 BasicHashTable基本哈希表类(二)——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...

  2. Http Request

    function getSend($url,$param) { $ch = curl_init($url."?".$param); curl_setopt($ch,CURLOPT_ ...

  3. php返回json数组

    1.后端 //处理json数组中文问题 function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { s ...

  4. python脚本-开机自动联网

    办公室的电脑开机之后,需要打开浏览器登录校园用户认证网页,输入用户名密码登录认证之后才能联网,每次都这样搞很麻烦,写了个Python脚本自动完成这个工作. onStartup.py #file:onS ...

  5. CentOS 6.4 搭建git 服务器

    CentOS 6.4 搭建git 服务器 (2013-11-22 19:04:09)转载▼ 标签: it 分类: Linux 此文件是依据markdown所编写,更好效果参见本人github的文档ht ...

  6. JavaScript——Window对象

    1.serTimeout()和setinterval()可用于注册在指定的时间之后单词或者重复调用的函数. 2.window对象的location属性引用的是Location对象,表示该窗口当前显示的 ...

  7. 使用SharePoint 2010 母版页

    SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但 ...

  8. javascript 数组去重

    2015年5月15日 20:17:05 星期五 原理: .......(说不清楚, 自己看代码吧, 很简单.....) //去重 var hash_already_input = {}; for (v ...

  9. ACM/ICPC 之 DP-浅谈“排列计数” (POJ1037)

    这一题是最近在看Coursera的<算法与设计>的公开课时看到的一道较难的DP例题,之所以写下来,一方面是因为DP的状态我想了很久才想明白,所以借此记录,另一方面是看到这一题有运用到 排列 ...

  10. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...