您的位置:首页 > 其它

第二章(1).线性表

2015-04-26 17:21 155 查看
#include<stdio.h>
#include<stdlib.h>
[code]#defineLIST_INIT_SIZE10//线性表存储空间的初始分配量#defineLISTADD10//线性表存储空间的分配增量#defineTRUE1#defineFALSE0#defineERROR-1
typedefintElemType;
typedefstruct
{
ElemType*elem;//存储空间基址
intlength;//当前长度
intlistsize;//当前分配的容量
}SqList,*Sqlist;
voidInitList_Sq(SqlistL)//构造一个空线性表{L->elem=(ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE);if(!L->elem)exit(0);L->length=0;//当前长度L->listsize=LIST_INIT_SIZE;//当前分配的存储容量(以sizeof(ElemType)为单位)}
intListLength(SqListL)//求线性表长度{returnL.length;}intListEmpty(SqListL)//判断线性表是否为空{if(L.length==0){returnTRUE;}else{returnFALSE;}}
voidListInsert(SqlistL,inti,ElemTypee)//在线性表中插入元素{ElemType*q,*p;ElemType*newbase;if(i<1||i>L->length+1){exit(0);}else{if(L->length>=L->listsize)//存储空间已满,需要增加分配{newbase=(ElemType*)realloc(L->elem,(L->listsize+LISTADD)*sizeof(ElemType));//realloc.追加if(!newbase){exit(0);//分配失败}else{L->elem=newbase;//新基址L->listsize+=LISTADD;//增加后长度}}else{q=&(L->elem[i-1]);//q为插入的位置for(p=&(L->elem[L->length-1]);p>=q;--p){*(p+1)=*p;//插入位置及以后的元素都右移}*q=e;//插入e++L->length;}}}ElemTypeGetElem(SqListL,inti,ElemType*e)//得到线性表中的元素并返回{if(i<1||i>L.length){exit(0);}else{*e=L.elem[i-1];return*e;}}ElemTypeListDelete(SqlistL,inti,ElemType*e)//删除元素{ElemType*q,*p;if(i<1||i>L->length){exit(0);}else{p=&(L->elem[i-1]);//q为删除的元素的位置*e=*p;q=L->elem+L->length-1;for(++p;p<=q;++p){*(p-1)=*p;//删除元素位置及以后的元素都左移}--L->length;return*e;}}//返回第一个与e满足关系compare()的数据元素的位序,若这样的元素不存在,则返回值为0.intLocateElem(SqListL,ElemTypee)//LocateElem(L,e,compare()),compare()为数据元素判定函数{//此处只比较非字符inti;for(i=0;i<L.length;i++){if(e==L.elem[i]){return(i+1);}}return0;}intmain(void){SqListL;inti;ElemType*e;e=NULL;InitList_Sq(&L);printf("%d\n",ListLength(L));for(i=1;i<12;i++){ListInsert(&L,i,i);}ListInsert(&L,12,13);printf("%d\n",GetElem(L,1,e));//printf("%d\n",ListDelete(&L,3,e));//printf("%d\n",GetElem(L,2,e));printf("%d\n",LocateElem(L,13));return0;}//**************************数组合并*****************************//#include<stdio.h>#include<stdlib.h>#defineLIST_INIT_SIZE100//线性表存储空间的初始分配量#defineLISTADD10//线性表存储空间的分配增量#defineTRUE1#defineFALSE0#defineERROR-1typedefintElemType; typedefstruct { ElemType*elem;//存储空间基址 intlength;//当前长度 intlistsize;//当前分配的容量 }SqList,*Sqlist;voidInitList_Sq(SqlistL)//构造一个空线性表{L->elem=(ElemType*)malloc(sizeof(ElemType)*LIST_INIT_SIZE);if(!L->elem)exit(0);L->length=0;//当前长度L->listsize=LIST_INIT_SIZE;//当前分配的存储容量(以sizeof(ElemType)为单位)}intListLength(SqListL)//求线性表长度{returnL.length;}intListEmpty(SqListL)//判断线性表是否为空{if(L.length==0){returnTRUE;}else{returnFALSE;}}voidListInsert(SqlistL,inti,ElemTypee)//在线性表中插入元素{ElemType*q,*p;ElemType*newbase;if(i<1||i>L->length+1){exit(0);}else{if(L->length>=L->listsize)//存储空间已满,需要增加分配{newbase=(ElemType*)realloc(L->elem,(L->listsize+LISTADD)*sizeof(ElemType));//realloc.追加if(!newbase){exit(0);//分配失败}else{L->elem=newbase;//新基址L->listsize+=LISTADD;//增加后长度}}else{q=&(L->elem[i-1]);//q为插入的位置for(p=&(L->elem[L->length-1]);p>=q;--p){*(p+1)=*p;//插入位置及以后的元素都右移}*q=e;//插入e++L->length;}}}ElemTypeGetElem(SqListL,inti,ElemType*e)//得到线性表中的元素并返回{if(i<1||i>L.length){exit(0);}else{*e=L.elem[i-1];return*e;}}ElemTypeListDelete(SqlistL,inti,ElemType*e)//删除元素{ElemType*q,*p;if(i<1||i>L->length){exit(0);}else{p=&(L->elem[i-1]);//q为删除的元素的位置*e=*p;q=L->elem+L->length-1;for(++p;p<=q;++p){*(p-1)=*p;//删除元素位置及以后的元素都左移}--L->length;return*e;}}//返回第一个与e满足关系compare()的数据元素的位序,若这样的元素不存在,则返回值为0.intLocateElem(SqListL,ElemTypee)//LocateElem(L,e,compare()),compare()为数据元素判定函数{inti;for(i=0;i<L.length;i++){if(e==L.elem[i]){return(i+1);}}return0;}//数组合并,剔除相同元素voidUnion(SqlistLa,SqListLb)//将所有在Lb中且不在La中的元素添加到La中{inti,La_len,Lb_len;ElemTypee,a;La_len=ListLength(*La),Lb_len=ListLength(Lb);for(i=1;i<=Lb_len;i++){a=GetElem(Lb,i,&e);//取Lb中第i个数据元素赋给e必须赋值,不然后面的e则仍然为一个地址值//printf("%d\n",GetElem(Lb,i,&e));It'sright!//printf("%d\n",e);It'swrong!if(!LocateElem(*La,a)){//printf("A");ListInsert(La,++La_len,a);//printf("%d\n",GetElem(*La,6,&e));}//printf("%d\n",ListLength(*La));}}intmain(void){inti,j;SqListLa,Lb;ElemType*e;inta[]={1,2,3,4,5};intb[]={4,5,6,7,8,9,0};e=NULL;//不要忘记初始化InitList_Sq(&La);InitList_Sq(&Lb);for(i=0;i<5;i++){La.elem[i]=a[i];++La.length;}//printf("%d\n",ListLength(La));//printf("%d\n",GetElem(La,4,e));for(j=0;j<7;j++){Lb.elem[j]=b[j];++Lb.length;}//printf("%d\n",ListLength(Lb));//printf("%d\n",GetElem(Lb,6,e));Union(&La,Lb);//合并//printf("%d\n",ListLength(La));for(i=1;i<=ListLength(La);i++){printf("%d",GetElem(La,i,e));if(i%5==0){printf("\n");}}printf("\n");return0;}[/code]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: