单链表实现交差和
2015-09-08 23:23
281 查看
#define MAXSIZE 10 #define ADDSIZE 5 typedef int ElemType; typedef struct{ ElemType *elem; int length; int size; }SqList;
<pre name="code" class="cpp">#include "stdio.h" #include "malloc.h" #include"head.h" void InitList(SqList *L){ L->elem=(ElemType *)malloc(MAXSIZE * sizeof(ElemType)); if(L!=NULL){ (*L).length=0; (*L).size=MAXSIZE; printf("内存分配成功!\n"); }else{ printf("内存分配失败!\n"); } } int ListInsert(SqList *L,int i,ElemType e){ int j; if(i<1||i>(*L).length+1){ ElemType * newElem; int k; newElem = (ElemType *)malloc((L->size+ADDSIZE)*sizeof(ElemType)); for(k=0;k<L->length;k++){ newElem[k]=L->elem[k]; } L->elem = newElem; L->size=L->size+ADDSIZE; } i--; for(j=L->length;j>i;j--){ L->elem[j]=L->elem[j-1]; } L->elem[i]=e; L->length++; return 1; } void ListTraverse(SqList *L){ int i; if(ListEmpty(L)){ return ; } for(i=0;i<L->length;i++){ printf("%d\t",L->elem[i]); } printf("\n"); } int ListEmpty(SqList *L){ return (L->length==0); } int ListLength(SqList *L){ return (L->length); } int ListSize(SqList *L){ return L->size; } ElemType GetElement(SqList *L,int i){ ElemType e=0; if(i<1||i>L->length){ return e; } e=L->elem[i-1]; return e; } int LocateElement(SqList *L,int i){ ElemType e=0; if(i<1||i>L->length){ return e; } e=L->elem[i-1]; return e; } void ClearList(SqList *L){ L->length=0; } void DestroyList(SqList *L){ free(L->elem); L->length=0; L->size=0; } int PutElement(SqList *L,int i,ElemType newValue){ int j; for(j=0;j<L->length;j++){ if(i==j+1){ L->elem[j]=newValue; return 1; } } return 0; } int ListDelete(SqList *L,int i){ int j; if(i<1||i>L->length){ return 0; } i--; for(j=i;j<L->length-1;j++){//向后移动指针域 L->elem[j]=L->elem[j+1]; } L->length--;//总长度少一 return 1; }
<pre name="code" class="cpp">#include "body.c" SqList * SetDifference(SqList *La,SqList *Lb,SqList *Lc){ int i,j; ElemType e; for(i=1;i<=ListLength(La);i++){ e=GetElement(La,i); for(j=1;j<=ListLength(Lb);j++){ if(e==GetElement(Lb,j)){ break; }else if(j==ListLength(Lb)){ ListInsert(Lc,ListLength(Lc)+1,e); } } } return Lc; } SqList * SetJiao(SqList *La,SqList *Lb,SqList *Lc){ int i,j; ElemType e; for(i=1;i<=ListLength(La);i++){ e=GetElement(La,i); for(j=1;j<=ListLength(Lb);j++){ if(e==GetElement(Lb,j)){ ListInsert(Lc,ListLength(Lc)+1,e); } } } return Lc; } SqList * SetHe(SqList *La,SqList *Lb,SqList *Lc){ int i,j; ElemType e; for(i=1;i<=ListLength(La);i++){ e=GetElement(La,i); ListInsert(Lc,ListLength(Lc)+1,e); } for(i=1;i<=ListLength(Lb);i++){ e=GetElement(La,i); ListInsert(Lc,ListLength(Lc)+1,e); } return Lc; } main(){ SqList La; SqList Lb; SqList Lc,*lc,*lc1,*lc2; SqList Le,*le; int i; InitList(&La); InitList(&Lb); InitList(&Lc); InitList(&Le); for(i=1;i<=10;i++){ ListInsert(&La,i,i); } for(i=1;i<=5;i++){ ListInsert(&Lb,i,i); } printf("La="); ListTraverse(&La); printf("Lb="); ListTraverse(&Lb); lc = SetDifference(&La,&Lb,&Lc); printf("²î="); ListTraverse(lc); ClearList(lc); lc1 = SetJiao(&La,&Lb,&Lc); printf("½»="); ListTraverse(lc1); ClearList(lc1); lc2 = SetHe(&La,&Lb,&Lc); printf("ºÍ="); ListTraverse(lc2); ClearList(lc2); le = SetDifference(SetHe(&La,&Lb,&Lc),SetJiao(&La,&Lb,&Lc),&Le); printf("²¢="); ListTraverse(le); }
相关文章推荐
- STM32 F4 ADC DMA Temperature Sensor
- Linux磁盘与目录的容量
- 正则表达式
- 【工具类】jenv安装使用
- 15_09_08 小知识点
- redhat6和centos7在yum应用上的区别
- tomcat:内存溢出的解决方法!
- ButterKnife--View注入框架
- Memcached 笔记与总结(3)安装 php-memcache(windows 系统下)
- 【jQuery】保持this所指向的元素(以Ajax为例说明)
- Java简单注册格式验证(正则表达式)
- C++中 引用&与取地址&的区别
- JavaScript之isPrototypeOf和instanceof
- 笔记-大话设计模式-18 备忘录模式
- 敌兵布阵
- jqueryEasyUI datagrid 跨域获取数据。
- mysql中的where和having子句的区别
- ScrollView嵌套ListView问题
- Markdown学习(一)
- 获取进程的内核转储