C语言顺序表基本函数.doc_第1页
C语言顺序表基本函数.doc_第2页
C语言顺序表基本函数.doc_第3页
C语言顺序表基本函数.doc_第4页
C语言顺序表基本函数.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一.无空间限制顺序表1.宏定义:#include#include#define OVERFLOW -2#define OK 1#define TRUE 1#define ERROR 0#define FALSE 0#define LIST_SEQLIST_INIT_SIZE 100#define LIST_SEQLIST_INCR_SIZE 100#define ElemType *;2.结构体:typedef structElemType *elem;int last;int length;Seqlist;3.基本函数:int Initlist_Seqlist_ 1(Seqlist *L)/*初始化链表:1.先决条件:无;2.函数作用:开辟空间并用elem记住首地址,和初始化链表相关数据last,length。*/(*L).elem=(ElemType *)malloc(LIST_SEQLIST_INIT_SIZE*sizeof(ElemType);if(!(*L).elem) return OVERFLOW;(*L).length=LIST_SEQLIST_INIT_SIZE;(*L).last=0;return OK;int Initlist_Seqlist_ 2(Seqlist *L)/*初始化链表:1.先决条件:初始化结构体数据;2.函数作用:删除已有链表,重新开辟空间并用elem记住首地址,和初始化链表相关数据last,length。*/if(*L).elem) free(*L).elem);(*L).elem=(ElemType *)malloc(LIST_SEQLIST_INIT_SIZE*sizeof(ElemType);if(!(*L).elem) return OVERFLOW;(*L).length=LIST_SEQLIST_INIT_SIZE;(*L).last=0;return OK;int Insert_Seqlist(Seqlist *L)/*插入数据e到链表上:1.先决条件:初始化结构体数据,原数据按递增顺序排列或无数据;2.函数作用:不受空间限制地插入数据,插入后数据仍按递增顺序排列。*/ElemType e,*p,*q;int count;printf(请输入数据:);scanf(%d,&e);/会因ElemType不同而不同if(*L).last=(*L).length)(*L).elem=(ElemType *)realloc(*L).elem,(LIST_SEQLIST_INIT_SIZE+LIST_SEQLIST_INCR_SIZE)*sizeof(ElemType); if(*L).elem=NULL) return OVERFLOW; else (*L).length+=LIST_SEQLIST_INCR_SIZE;for(p=(*L).elem,count=(*L).last;e*p&count0;p+,count-);q=p;for(p=(*L).elem+(*L).last);p!=q;p-)*p=*(p-1);*p=e; (*L).last+;return OK;int Delete_Seqlist(Seqlist *L,)/*删除函数:1.先决条件:无空间限制顺序表;2.函数作用:从顺序表中删除自第i个元素开始的k个元素。*/ElemType *p,*q; int i,k;printf(将从顺序表中删除自第i个元素开始的k个元素,请输入i,k(以空格间隔):n);scanf(%d%d,&i,&k);getchar();if(i(*L).last)printf(删除起点越界!n);return ERROR;if(k(*L).last)printf(删除个数越界!n);return ERROR;for(p=(*L).elem+i-1,q=p+k;q!=(*L).elem+(*L).last;q+,p+)*p=*q;(*L).last=(*L).last-k;return OK;int Printf_Seqlist(Seqlist *L)/*显示函数:1.先决条件:无空间限制顺序表;2.函数作用:输出顺序链表的各个数据。*/ElemType *p;int count=0; for(p=(*L).elem;p(*L).elem+(*L).last;p+)printf(%d ,*p);/*这里的”%d”要因具体的情况而定*/count+;if(count%10=0)printf(n); printf(n);return OK;int Delete_Seqlist_2(Seqlist *L)/*删除函数:1.先决条件:初始化顺序表;2.函数作用:删除值在x到y(x=y含x,y)之间的所有元素*/int count=0,index=0;double x,y;printf(请输入要删除元素值的范围x,y(xlastindex)if(xelemindex&L-elemindexelemindex-count=L-elemindex;index+;L-last=index-count;return OK;int Seperate(Seqlist *L,Seqlist *overzero,Seqlist *lowzero)/*分拆函数1.先决条件:无2.函数作用:初始化overzero,lowzero并且将一个顺序表L分拆成两个顺序表overzero,lowzero以0为分界线*/int index=0;Initlist_Seqlist_1(overzero);Initlist_Seqlist_1(lowzero);while(L-lastindex)if(L-elemindex=0)Insert_Seqlist_2(overzero,L-elemindex);elseInsert_Seqlist_2(lowzero,L-elemindex);index+;return 0;int Insert_Seqlist_2(Seqlist *L,ElemType e)/*尾插入数据e到顺序表上:1.先决条件:初始化结构体数据;2.函数作用:不受空间限制地在尾部插入数据。*/ElemType *p,*q;if(*L).last=(*L).length)(*L).elem=(ElemType *)realloc(*L).elem,(LIST_SEQLIST_INIT_SIZE+LIST_SEQLIST_INCR_SIZE)*sizeof(ElemType); if(*L).elem=NULL) return OVERFLOW; else (*L).length+=LIST_SEQLIST_INCR_SIZE;L-elemL-last=e; (*L).last+;return OK;注:先决条件:初始化结构体数据为L.elem=NULL;L.last=0;L.length=0;4.主函数int main()Seqlist L;L.elem=NULL;L.last=0;L.length=0;ElemType e;int status,choice,i,k;doprintf(*n);printf(1.初始化顺序表 2.增加数据 3.删除数据 4.显示数据 5.退出n);printf(*n);printf(请输入选择(15):);scanf(%d,&choice);getchar();switch(choice) case 1:status=Initlist_Seqlist(&L); if(status=OK) printf(顺序表现初始化了!n); else if(status=OVERFLOW) printf(申请空间失败,链表没初始化!n); else printf(程序执行中途出错,请修改程序!n); break;case 2:printf(请输入数据:); scanf(%d,&e); status=Increase_Seqlist(&L,e); if(status=OK) printf(成功增加数据!n); else if(status=OVERFLOW) printf(申请空间失败,所有数据丢失!n); else printf(程序执行中途出错,请修改程序!n); break;case 3:printf(将从顺序表中删除自第i个元素开始的k个元素,请输入i,k(以空格间隔):n); scanf(%d%d,&i,&k); getchar(); status=Delete_Seqlist(&L,i,k); if(status=1) printf(删除数据成功!n); break;case 4:Printf_Seqlist(&L);break; case 5:printf(谢谢使用!n);break; default :printf(输入错误,请重新输入!n);break;while(choice!=5);return 0;二有空间限制顺序表1.宏定义#include#include#define MAXSIZE 1024typedef * datatype;2.结构体typedef structdatatype dataMAXSIZE;int last;Seqlist;3.基本函数Seqlist *Init_Seqlist()/*初始化顺序表函数1.先决条件:无;2.函数作用:首先申请顺序表空间,然后初始化顺序表指针即L-last=-1,返回栈s的地址*/Seqlist *L;L=(Seqlist *)malloc(sizeof(Seqlist);L-last=-1;return L;int Length_Seqlist(Seqlist *L)/*计算长度函数1.先决条件:初始化结构体数据;2.函数作用:计算顺序表长度,并返回长度*/return L-last+1;int Get_Seqlist(Seqlist *L,int i,datatype *x)/*找表元函数1.先决条件:初始化结构体数据;2.函数作用:找到表中第i个元素,并将其存放到x中,成功返回1,因越界失败返回0*/if(iL-last+1)printf(取值越界.n);return 0;*x=L-datai-1;return 1;int Location_Seqlist(Seqlist *L,datatype x)/*查找函数1.先决条件:初始化结构体数据;2.函数作用:在顺序表中查找与给定值x相等的数据元素,成功返回i(存储位置),因表为空失败返回-1,因不存在失败返回-2*/int i=0;if(L-last=-1)printf(顺序表为空.n);/*可以去掉此句*/return -1;while(ilast)if(L-datai=x)return i;i+;printf(顺序表中不存在此数据!n);return -2;int Insert_Seqlist(Seqlist *L,int i,datatype x)/*插入函数1.先决条件:初始化结构体数据;2.函数作用:在表的第i个位置上插入一个值为x的新元素,成功返回1,因表满失败返回-1,因插入位置错失败返回0*/int j;if(L-last=MAXSIZE-1)printf(表满.n);/*可以去掉此句*/return -1;if(iL-last+2)printf(位置错.n);/*可以去掉此句*/return 0;for(j=L-last;j=i-1;j-)L-dataj+1=L-dataj;L-datai-1=x;L-last+;return 1;int Delete_Seqlist(Seqlist *L,int i,datatype *x)/*删除函数1.先决条件:初始化结构体数据;2.函数作用:将表中第i个元素从顺序表中去掉,成功返回1,因位置错失败返回0*/if(iL-last+1)printf(不存在第%d个元素.n,i);/*可以去掉此句*/return 0;*x=L-datai-1;while(ilast)L-datai-1=L-datai;i+;L-last-;return 1;int Printf_Seqlist(Seqlist *L)/*遍历函数1.先决条件:初始化结构体数据;2.函数作用:从头到尾输出顺序表,成功返回1*/int i=0,j=0;while(ilast)printf(%d ,L-datai);/*因datatype不同而不同*/i+;j+;if(j%10=0)printf(n);printf(n);return 1;4.主函数int main()Seqlist *L;int choice,i;datatype x;doprintf(*n);printf(1.初始化 2.计算长度 3.取表元 4.按值查找 5.插入 6.删除 7.遍历 8.退出n);printf(*n);printf(请输入选择(18):);scanf(%d,&choice);getchar();switch(choice)case 1:if(L=Init_Seqlist() printf(初始化成功

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论