教育资源为主的文档平台

当前位置: 查字典文档网> 所有文档分类> > 计算机软件及应用> 线性表的顺序表示和实现

线性表的顺序表示和实现

上传者:陈瓅
|
上传时间:2015-04-22
|
次下载

线性表的顺序表示和实现

#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; /* * structe */ 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 = INCREMENT_SIZE; return OK; } /* * destroy a list */ 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(L.length == 0) 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(L.elem[i] == cur_e) { if(i != 0) *pre_e = L.elem[i-1]; else return ERROR; } } if(i >= L.length) return ERROR; } Status NextElem(const 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; } Status InsertElem(SqList *L,int i,Elemtype e) { Elemtype *new; if (i < 1 || i > L->length+1) return ERROR; if(L->length == L->size) { new = (Elemtype*)realloc(L->elem,(L->size + INCREMENT_SIZE) * sizeof(Elemtype)); if(!new) return ERROR; L->elem = new; 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 = & amp;L->elem[i - 1]; *e = *p; for(;p < &L->elem[L->length];p++) *p = *(p + 1); --L->length; return OK; } void visit(Elemtype e) { printf("%d\n",e); } Status TraverseList(const SqList L,void(*visit)(Elemtype e)) { int i; for(i =

0;i < L.length;i++) visit(L.elem[i]); return OK; } int main() { SqList L; if(InitList(&L)) { Elemtype e; printf("init_success\n"); int i; for(i = 0;i < 10; i++) InsertElem(&L,i+1,i); 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 next elemnt is %d\n",e); DeleteElem(&L,1,&e); printf("delet first element is %d\n",e); printf("list:"); TraverseList(L,visit); if(DestroyList(&L)) printf("\ndestroy_success\n"); } }

版权声明:此文档由查字典文档网用户提供,如用于商业用途请与作者联系,查字典文档网保持最终解释权!

下载文档

热门试卷

2016年四川省内江市中考化学试卷
广西钦州市高新区2017届高三11月月考政治试卷
浙江省湖州市2016-2017学年高一上学期期中考试政治试卷
浙江省湖州市2016-2017学年高二上学期期中考试政治试卷
辽宁省铁岭市协作体2017届高三上学期第三次联考政治试卷
广西钦州市钦州港区2016-2017学年高二11月月考政治试卷
广西钦州市钦州港区2017届高三11月月考政治试卷
广西钦州市钦州港区2016-2017学年高一11月月考政治试卷
广西钦州市高新区2016-2017学年高二11月月考政治试卷
广西钦州市高新区2016-2017学年高一11月月考政治试卷
山东省滨州市三校2017届第一学期阶段测试初三英语试题
四川省成都七中2017届高三一诊模拟考试文科综合试卷
2017届普通高等学校招生全国统一考试模拟试题(附答案)
重庆市永川中学高2017级上期12月月考语文试题
江西宜春三中2017届高三第一学期第二次月考文科综合试题
内蒙古赤峰二中2017届高三上学期第三次月考英语试题
2017年六年级(上)数学期末考试卷
2017人教版小学英语三年级上期末笔试题
江苏省常州西藏民族中学2016-2017学年九年级思想品德第一学期第二次阶段测试试卷
重庆市九龙坡区七校2016-2017学年上期八年级素质测查(二)语文学科试题卷
江苏省无锡市钱桥中学2016年12月八年级语文阶段性测试卷
江苏省无锡市钱桥中学2016-2017学年七年级英语12月阶段检测试卷
山东省邹城市第八中学2016-2017学年八年级12月物理第4章试题(无答案)
【人教版】河北省2015-2016学年度九年级上期末语文试题卷(附答案)
四川省简阳市阳安中学2016年12月高二月考英语试卷
四川省成都龙泉中学高三上学期2016年12月月考试题文科综合能力测试
安徽省滁州中学2016—2017学年度第一学期12月月考​高三英语试卷
山东省武城县第二中学2016.12高一年级上学期第二次月考历史试题(必修一第四、五单元)
福建省四地六校联考2016-2017学年上学期第三次月考高三化学试卷
甘肃省武威第二十三中学2016—2017学年度八年级第一学期12月月考生物试卷

网友关注视频

沪教版牛津小学英语(深圳用) 四年级下册 Unit 3
第4章 幂函数、指数函数和对数函数(下)_六 指数方程和对数方程_4.7 简单的指数方程_第一课时(沪教版高一下册)_T1566237
三年级英语单词记忆下册(沪教版)第一二单元复习
《小学数学二年级下册》第二单元测试题讲解
沪教版牛津小学英语(深圳用) 四年级下册 Unit 8
外研版英语三起6年级下册(14版)Module3 Unit1
人教版二年级下册数学
沪教版八年级下册数学练习册一次函数复习题B组(P11)
冀教版英语五年级下册第二课课程解读
冀教版小学英语四年级下册Lesson2授课视频
外研版英语三起5年级下册(14版)Module3 Unit2
沪教版八年级下册数学练习册20.4(2)一次函数的应用2P8
【部编】人教版语文七年级下册《老山界》优质课教学视频+PPT课件+教案,安徽省
【部编】人教版语文七年级下册《过松源晨炊漆公店(其五)》优质课教学视频+PPT课件+教案,江苏省
沪教版牛津小学英语(深圳用) 六年级下册 Unit 7
二年级下册数学第二课
沪教版牛津小学英语(深圳用) 四年级下册 Unit 7
外研版英语七年级下册module3 unit1第二课时
【部编】人教版语文七年级下册《老山界》优质课教学视频+PPT课件+教案,安徽省
河南省名校课堂七年级下册英语第一课(2020年2月10日)
二次函数求实际问题中的最值_第一课时(特等奖)(冀教版九年级下册)_T144339
第8课 对称剪纸_第一课时(二等奖)(沪书画版二年级上册)_T3784187
外研版英语七年级下册module3 unit2第一课时
冀教版小学数学二年级下册1
沪教版牛津小学英语(深圳用) 四年级下册 Unit 2
冀教版小学数学二年级下册第二单元《有余数除法的整理与复习》
【部编】人教版语文七年级下册《泊秦淮》优质课教学视频+PPT课件+教案,辽宁省
化学九年级下册全册同步 人教版 第25集 生活中常见的盐(二)
沪教版八年级下册数学练习册21.3(3)分式方程P17
化学九年级下册全册同步 人教版 第18集 常见的酸和碱(二)