线性表--算法设计题2.29
已知A,B和C为三个递增有序的线性表,现要求对A表作如下操作:删去那些既在B表中出现又在C表中出现的元素。试对顺序表编写实现上述操作的算法.(注意:题中没有特别指明同一表中的元素值各不相同)。
C code:
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 2
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define TRUE 1
typedef int Status;
struct SqList
{
int *elem;
int length;
int listsize;
};
void InitList(SqList &L)
{
L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
Status ListInsert(SqList &L,int i,int e)
{
int *newbase,*q,*p;
if(i<1||i>L.length+1)
return ERROR;
if(L.length==L.listsize)
{
newbase=(int*)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(int));
if(!newbase)
exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LIST_INCREMENT;
}
q=L.elem+i-1;
for(p=L.elem+L.length-1; p>=q; --p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
void CreateList(SqList &L,int len)
{
int i,t;
printf("Please input elems: ");
for(i=1; i<=len; i++)
{
scanf("%d",&t);
ListInsert(L,i,t);
}
}
void ListTraverse(SqList L,void(*vist)(int&))
{
int *p=L.elem;
int i;
for(i=1; i<=L.length; i++)
{
vist(*p++);
}
printf("\n");
}
void print1(int &c)
{
printf("%d ",c);
}
int cmp(const void *a,const void *b)
{
return *(int*)a - *(int*)b;
}
void ListSort(SqList &L)
{
qsort(L.elem,L.length,sizeof(int),cmp);
}
void ListCross(SqList La,SqList Lb,SqList &Lc)
{
int i=0,j=0,k=1;
while(i<La.length && j<Lb.length)
{
if(La.elem[i]<Lb.elem[j])
i++;
else
{
if(La.elem[i]>Lb.elem[j])
j++;
else
{
ListInsert(Lc,k,La.elem[i]);
i++;
k++;
}
}
}
}
void ListMinus(SqList La,SqList Lb,SqList &Lc)
{
int i=0,j=0,k=1;
while(i<La.length && j<Lb.length)
{
if(La.elem[i]<Lb.elem[j])
{
ListInsert(Lc,k,La.elem[i]);
i++;
k++;
}
else
{
if(La.elem[i]>Lb.elem[j])
{
j++;
}
else
{
i++;
j++;
}
}
}
}
int main()
{
SqList La,Lb,temp,Lc,Ld;
InitList(La);
InitList(Lb);
InitList(Lc);
InitList(Ld);
InitList(temp);
int La_len,Lb_len,Lc_len;
printf("input length of La: ");
scanf("%d",&La_len);
printf("input length of Lb: ");
scanf("%d",&Lb_len);
printf("input length of Lc: ");
scanf("%d",&Lc_len);
CreateList(La,La_len);
CreateList(Lb,Lb_len);
CreateList(Lc,Lc_len);
printf("List a: ");
ListTraverse(La,print1);
printf("List b: ");
ListTraverse(Lb,print1);
printf("List c: ");
ListTraverse(Lc,print1);
printf("After sorting List a: ");
ListSort(La);
ListTraverse(La,print1);
printf("After sorting List b: ");
ListSort(Lb);
ListTraverse(Lb,print1);
printf("After sorting List c: ");
ListSort(Lc);
ListTraverse(Lc,print1);
ListCross(Lb,Lc,temp);
ListSort(temp);
printf("temp List: ");
ListTraverse(temp,print1);
ListMinus(La,temp,Ld);
ListSort(Ld);
printf("List d: ");
ListTraverse(Ld,print1);
return 0;
#include<stdlib.h>
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 2
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define TRUE 1
typedef int Status;
struct SqList
{
int *elem;
int length;
int listsize;
};
void InitList(SqList &L)
{
L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
Status ListInsert(SqList &L,int i,int e)
{
int *newbase,*q,*p;
if(i<1||i>L.length+1)
return ERROR;
if(L.length==L.listsize)
{
newbase=(int*)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(int));
if(!newbase)
exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LIST_INCREMENT;
}
q=L.elem+i-1;
for(p=L.elem+L.length-1; p>=q; --p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
void CreateList(SqList &L,int len)
{
int i,t;
printf("Please input elems: ");
for(i=1; i<=len; i++)
{
scanf("%d",&t);
ListInsert(L,i,t);
}
}
void ListTraverse(SqList L,void(*vist)(int&))
{
int *p=L.elem;
int i;
for(i=1; i<=L.length; i++)
{
vist(*p++);
}
printf("\n");
}
void print1(int &c)
{
printf("%d ",c);
}
int cmp(const void *a,const void *b)
{
return *(int*)a - *(int*)b;
}
void ListSort(SqList &L)
{
qsort(L.elem,L.length,sizeof(int),cmp);
}
void ListCross(SqList La,SqList Lb,SqList &Lc)
{
int i=0,j=0,k=1;
while(i<La.length && j<Lb.length)
{
if(La.elem[i]<Lb.elem[j])
i++;
else
{
if(La.elem[i]>Lb.elem[j])
j++;
else
{
ListInsert(Lc,k,La.elem[i]);
i++;
k++;
}
}
}
}
void ListMinus(SqList La,SqList Lb,SqList &Lc)
{
int i=0,j=0,k=1;
while(i<La.length && j<Lb.length)
{
if(La.elem[i]<Lb.elem[j])
{
ListInsert(Lc,k,La.elem[i]);
i++;
k++;
}
else
{
if(La.elem[i]>Lb.elem[j])
{
j++;
}
else
{
i++;
j++;
}
}
}
}
int main()
{
SqList La,Lb,temp,Lc,Ld;
InitList(La);
InitList(Lb);
InitList(Lc);
InitList(Ld);
InitList(temp);
int La_len,Lb_len,Lc_len;
printf("input length of La: ");
scanf("%d",&La_len);
printf("input length of Lb: ");
scanf("%d",&Lb_len);
printf("input length of Lc: ");
scanf("%d",&Lc_len);
CreateList(La,La_len);
CreateList(Lb,Lb_len);
CreateList(Lc,Lc_len);
printf("List a: ");
ListTraverse(La,print1);
printf("List b: ");
ListTraverse(Lb,print1);
printf("List c: ");
ListTraverse(Lc,print1);
printf("After sorting List a: ");
ListSort(La);
ListTraverse(La,print1);
printf("After sorting List b: ");
ListSort(Lb);
ListTraverse(Lb,print1);
printf("After sorting List c: ");
ListSort(Lc);
ListTraverse(Lc,print1);
ListCross(Lb,Lc,temp);
ListSort(temp);
printf("temp List: ");
ListTraverse(temp,print1);
ListMinus(La,temp,Ld);
ListSort(Ld);
printf("List d: ");
ListTraverse(Ld,print1);
return 0;
}
作者:cpoint
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.