#2
兔子君2014-03-07 17:24
好吧,此帖已自行解决.封楼.
#include "stdio.h" #include "malloc.h" #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status; typedef int ElemType; typedef struct { ElemType *elem; int length; int listsize; }SqList; Status InitList_Sq(SqList & L) // ??????Ï??a??ˆø????ƒœfl?C??????L???? { L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int)); if(!L.elem) return ERROR; L.length=0; L.listsize=LIST_INIT_SIZE; return OK; } Status ListInsert_Sq(SqList & L,int i,ElemType e) // ??⁄?????C??œfl?C??????L??ƒ??⁄i??ˆ??™??ÿ??Æ«????»Î?C¬??ƒ??™??ÿe???? // i??ƒ??œ??®????Œ™1??‹i??‹ListLength_Sq(L)+1 { int k; if(i<1||i>L.length+1) return ERROR; if(i<=L.length) for(k=L.length+1;k>i-1;k--) L.elem[k+1]=L.elem[k]; L.elem[i-1]=e; L.length++; return OK; } int GetElem(SqList L,int i,ElemType & e) //??ı oÃıo˛:œfl?C??????L????æ????????⁄,1<=i<=ListLength(L)???? //??Ÿ◊˜??????˚:????e????aÿœfl?C??????L???C??⁄i??ˆ ˝æ›??™??ÿ??ƒ???????? { if(i<=0 || i>L.listsize ) return ERROR; else e=L.elem [i-1]; return OK; } void ListPrint(SqList L) //??????????˚???????? { int i; ElemType e; if (L.length ==0) { printf("NULL\n"); return ; } for(i=1;i<=L.length ;i++) { GetElem(L,i,e); printf("%d ",e); } printf("\n"); } void Intersection(SqList A,SqList B,SqList &C)//«ÎÕ????∆ //C=A????B { int i,j,k; k=0; for(i=0;i<A.length;i++) { for(j=0;j<B.length;j++) { if(A.elem[i]==B.elem[j]) { C.elem[C.length++]=A.elem[i]; break; } } } } int main() { SqList A,B,C; int i,n,x; InitList_Sq(A); InitList_Sq(B); InitList_Sq(C); scanf("%d",&n); for(i=1;i<=n;i++) //??????ÏAoØ??œ { scanf("%d",&x); ListInsert_Sq(A,i,x); } scanf("%d",&n); for(i=1;i<=n;i++) //??????ÏBoØ??œ { scanf("%d",&x); ListInsert_Sq(B,i,x); } Intersection(A,B,C); ListPrint(C); return 0; } |
用线性表的顺序表示方式表示集合,集合中的元素不重复,现有A、B两个集合,求C集合,C集合为AB两个集合的交集,C中元素的顺序按A中元素原有的顺序存放。函数void Intersection(Sqlist A,Sqlist B,Sqlist &C)即实现上述功能,请完善。
其中,
Status InitList_Sq(SqList & L)
Status ListInsert_Sq(SqList & L,int i,ElemType e)
两个函数(顺序表第一题)可使用之前写过的代码。
#include "stdio.h"
#include "malloc.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList & L) // 构造一个空的线性表L。
{
}
Status ListInsert_Sq(SqList & L,int i,ElemType e)
// 在顺序线性表L的第i个元素之前插入新的元素e,
// i的合法值为1≤i≤ListLength_Sq(L)+1
{
}
int GetElem(SqList L,int i,ElemType & e)
//初始条件:线性表L已经存在,1<=i<=ListLength(L)。
//操作结果:用e返回线性表L中第i个数据元素的值。
{
if(i<=0 || i>L.listsize )
return ERROR;
else
e=L.elem [i-1];
return OK;
}
void ListPrint(SqList L) //打印整张表
{
int i;
ElemType e;
if (L.length ==0)
{
printf("NULL\n");
return ;
}
for(i=1;i<=L.length ;i++)
{ GetElem(L,i,e);
printf("%d ",e);
}
printf("\n");
}
void Intersection(SqList A,SqList B,SqList &C)//请完善
//C=A∩B
//C中元素的顺序按A中元素原有的顺序存放
{
}
int main()
{
SqList A,B,C;
int i,n,x;
InitList_Sq(A);
InitList_Sq(B);
InitList_Sq(C);
scanf("%d",&n);
for(i=1;i<=n;i++) //构造A集合
{
scanf("%d",&x);
ListInsert_Sq(A,i,x);
}
scanf("%d",&n);
for(i=1;i<=n;i++) //构造B集合
{
scanf("%d",&x);
ListInsert_Sq(B,i,x);
}
Intersection(A,B,C);
ListPrint(C);
return 0;
}