您的位置:首页 > 其它

单链表实现交差和

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);

}



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