线性表的基本操作
来源:互联网 发布:java.lang.instrument 编辑:程序博客网 时间:2024/04/30 03:03
哎,说来惭愧,现在才感悟到编码才是王道,懂得原理并不能真正地做到能够完美解决实现问题,浪费了美好的本科四年时光,所以现在要抓紧,开始编代码咯!时不时再回顾下。今天写了下线性表的基本操作,并且实现了它,突然感觉很有成就感!建议大家一定要敲一敲!因为真的很重要。下面我就一一解释下咯!
先把头文件和一些变量的定义写出来吧!
/********************************* *@fileName: 2.1.c *@author : jeff *@email : jeff666@outlook.com *@created : 2015-11-20 20:41:10**********************************/#include<stdio.h>#include<stdlib.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INIT_SIZE 10 //初始化表长#define INCREMENT_SIZE 5 //分配增量typedef int Status;typedef int Elemtype;
(一)存储结构体
typedef struct{ Elemtype *elem;//存储空间基址 int length; //当前长度 int size; //当前分配的表长大小}SqList;
初始化一个空的线性表
Status InitList(SqList *L){ L->elem=(Elemtype*)malloc(INIT_SIZE*sizeof(Elemtype)); if(!L->elem) return ERROR; L->length=0; L->size=INIT_SIZE; return OK;}
销毁线性表
Status DestroyList(SqList* L){ free(L->elem); L->length=0; L->size=0; return OK;}
清空线性表
Status ClearList(SqList *L){ L->length=0; return OK;}
判断线性表是否为空
Status isEmpty(const SqList L){ if(0==L.length) return TRUE; else return FALSE;}
获取长度
Status getLength(const SqList L){ return L.length;}
根据位置获取元素
Status GetElem(const SqList L,int i,Elemtype *e){ if(i<1||i>L.length) return ERROR; *e=L.elem[i-1]; return OK;}
比较两个元素是否相等
Status compare(Elemtype e1,Elemtype e2){ if(e1==e2) return 0; else if(e1<e2) return -1; else return 1;}
查找元素
Status FindElem(const SqList L,Elemtype e,Status (*compare)(Elemtype,Elemtype)){ int i; for(i=0;i<L.length;++i) { if(!(*compare)(L.elem[i],e)) { return i+1; } } if(i>=L.length) return ERROR;}
查找前驱元素
Status PreElem(const SqList L,Elemtype cur_e,Elemtype* pre_e){ int i; for(i=0;i<L.length;++i) { if(cur_e==L.elem[i]) { if(i>0) { *pre_e=L.elem[i-1]; return OK; } else return ERROR; } } if(i>=L.length) return ERROR;}
查找后继元素
Status NextElem(SqList L,Elemtype cur_e,Elemtype* next_e){ int i; for(i=0;i<L.length;++i) { if(cur_e==L.elem[i]) { if(i<L.length-1) { *next_e=L.elem[i+1]; return OK; } else return ERROR; } } if(i>=L.length) return ERROR;}
插入元素,这里尤其注意realloc函数的使用
Status InsertElem(SqList *L,int i,Elemtype e){ Elemtype* newList; if(i<1||i>L->length+1) return ERROR; if(L->length>=L->size) { newList=(Elemtype*)realloc(L->elem,(L->size+INCREMENT_SIZE)*sizeof(Elemtype)); if(!newList) return ERROR; L->elem=newList; L->size+=INCREMENT_SIZE; } Elemtype *p=&L->elem[i-1]; Elemtype *q=&L->elem[L->length-1]; for(;q>=p;q--) *(q+1)=*q; *p=e; ++L->length; return OK;}
删除元素
Status DeleteElem(SqList *L,int i,Elemtype *e){ if(i<1||i>L->length) return ERROR; Elemtype *p=&L->elem[i-1]; *e=*p; for(;p<&L->elem[L->length-1];p++) *p=*(p+1); --L->length; return OK;}
访问元素
void visit(Elemtype e){ printf("%d",e); }Status TraverseList(const SqList L,void (*visit)(Elemtype)){ int i; for(i=0;i<L.length;++i) visit(L.elem[i]); return OK;}
主函数
int main(){ SqList L; if(InitList(&L)) { printf("Init success!\n"); Elemtype e; int i; for(i=0;i<10;++i) { InsertElem(&L,i+1,i); } TraverseList(L,visit); printf("\n"); printf("length is %d\n",getLength(L)); if(GetElem(L,1,&e)) printf("The first element is %d\n",e); else printf("element is not exist!\n"); if(isEmpty(L)) printf("List is empty!\n"); else printf("list is not empty!\n"); printf("The 5 at %d\n",FindElem(L,5,compare)); PreElem(L,6,&e); printf("The 6's previous element is %d\n",e); NextElem(L,6,&e); printf("The 6's previous element is %d\n",e); DeleteElem(&L,5,&e); printf("delete five element is %d\n",e); TraverseList(L,visit); if(DestroyList(&L)) printf("destroy success!\n"); } return 0;}
0 0
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表的基本操作
- 线性表基本操作
- 线性表基本操作
- SWUST ACM 训练题部分题解 hdu1384 && hdu3666 && hdu 4786 &&uva 1395 && uva 1151
- 数据库锁(二)
- web.config 关闭调试模式
- Checklists学习日志之UITableView的全部属性、方法以及代理方法执行顺序
- String,StringBuffer与StringBuilder
- 线性表的基本操作
- 谓词大揭秘
- Webkit之Port类(ChromeClientQt类)
- android 常用调试命令
- 继承方式代理
- Java常用排序算法(快速排序/插入排序/shell排序)
- 学习搭建NDK的环境
- Android日志管理程序
- github_将项目放在github仓库中