//求集合A和B的交集
#include
#include
#define MAXSIZE 100
/*
順序表結構體定義
*/
typedef struct node {
int data[MAXSIZE];
int length;
}SeqList,*PSeqList;
/*
順序表的初始化
*/
PSeqList Init_SeqList(void)
{
PSeqList PL;
PL=(PSeqList)malloc(sizeof(SeqList));
if(PL)
PL->length=0;
return PL;
}
void Inter_Sec(PSeqList A,PSeqList B)
{
int i=0,j;
int k=B->length;
while(ilength)
{
int x=A->data[i];
j=0;
while(jdata[j])
j++;
if(j>=k)
{
for(int m=i;mlength;m++)
A->data[m]=A->data[m+1];
A->length--;
}
else
i++;
}
}
int main()
{
int i;
PSeqList A=Init_SeqList();
PSeqList B=Init_SeqList();
A->length=6;
B->length=5;
for(i=0;i
A->data[i]=i+1;
for(i=0;i
B->data[i]=i-1;
Inter_Sec(A,B);
for(i=0;ilength;i++)
printf("%d ",A->data[i]);
getchar();
return 0;
}
j==k j=k?