# include
#include
Typedef int ElemType;
30//# define LIST_INIT_SIZE linear table storage space of the initial distribution
10//# define LISTINCREMENT linear table storage space allocation of incremental
Typedef struct
{
ElemType * elem;//storage base address
Int length;//the current length
Int listsize;//the current allocation of storage capacity (to sizeof (ElemType))
} SqList;
/* your program will be embedded in here */
# define TRUE 1
# define FALSE 0
# define OK 1
# define the ERROR 0
# define OVERFLOW - 2
Typedef int the Status;
The Status InitList_Sq (SqList & amp; L)
{
L.e lem=(ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType));
if(! L.e lem)
Return the ERROR;//storage allocation failure
L.l ength=0;//the length and width of the table of 0
L.l istsize=LIST_INIT_SIZE;//the initial storage capacity
Return OK;
}
Int ListLength_Sq SqList (L)
{
Return L.l ength;
}
Status GetElem_Sq (SqList L, int I ElemType & amp; E)
{
If (i<1 | | i> L.l ength + 1)
Return the ERROR;
E=L.e lem [I - 1);
Return OK;
}
The Status ListInsert_Sq (SqList & amp; L, int I ElemType e)
{
ElemType * p * q * newbase;
If (i<1 | | i> L.l ength + 1)
Return the ERROR;//I illegal
If (L.l ength>=L.l istsize)//the current storage space is full
{
Newbase=(ElemType *) realloc (L.e lem, (L.l istsize + LISTINCREMENT) * sizeof (ElemType));
if(! Newbase)
The exit (OVERFLOW);//storage allocation failure
L.e lem=newbase;//the new base address
L.l istsize +=LISTINCREMENT;
}
Q=L.e lem + I - 1;//q for the insert position;
//if (L.l ength>
=1)For (p=L.e lem + L.l ength - 1; p>=q; - p)//p
is the last position* (p + 1)=* p;//element (one-way
* q=e;//insert e
+ + L.l ength;//table increase long;
Return OK;
}
Status Equal (ElemType ElemType x, y)//comparing x and y are Equal
{
If (x==y)
return TRUE;
return FALSE;
}
ElemType int LocateElem_Sq (SqList L, e, the Status (* compare) (ElemType ElemType))//look for the element
{
ElemType * p;
Int I=1;
P=L.e lem.
If ((i<=L.l ength) & amp; & (* p++!=e))
+ + I;
If (i<=L.l ength)
Return the I;
The else
return 0;
}
Void union_Sq (SqList La SqList Lb, SqList & amp; Lc)//linear table La, Lb respectively set A, B, C=?B
{
InitList_Sq (Lc);
ElemType e;
Int La_len Lb_len, Lc_len=0, I;
La_len=ListLength_Sq (La);
Lb_len=ListLength_Sq (Lb);
for(i=1; i<=La_len; I++)
{
GetElem_Sq (La, I, e);
ListInsert_Sq (Lc, + + Lc_len, e);
}
for(i=1; i<=Lb_len; I++)
{
GetElem_Sq (Lb, I, e);
If (LocateElem_Sq (Lc, e, Equal)==0)
ListInsert_Sq (Lc, + + Lc_len, e);
}
}
Int main ()
{
Int I, len.
Int m, n;//store two sets the initial length respectively
ElemType e;
SqList La, Lb, Lc;
The scanf (" % d % d ", & amp; M, & amp; N);
InitList_Sq (La);
InitList_Sq (Lb);//to build two empty
for(i=1; i<=m; I++)//set up the first set
{
The scanf (" % d ", & amp; E);
ListInsert_Sq (La, I, e);
}
for(i=1; i<=n; I++)//set up the second collection
{
The scanf (" % d ", & amp; E);
ListInsert_Sq (Lb, I, e);
}
Union_Sq (La and Lb, Lc);//calculate collection La, Lb intersection of Lc
Len=ListLength_Sq (Lc);
for(i=1; i<=len; I++)//output
{
GetElem_Sq (Lc, I, e);
Printf (" % 4 d ", e);
}
return 0;
}
CodePudding user response:
LocateElem_Sq pointer usage problemIf ((i<=L.l ength) & amp; & (* p++!=e))
Change
If ((i<=L.l ength) & amp; & (* (p++)!=e))//attention priority
Or change to
If ((i<=L.l ength) & amp; & (* (p + I - 1)!=e))
In addition, LocateElem_Sq parameters was useless to compare a function pointer (of course, not to also do not affect the code logic, just feel changed parameters meaningless)
CodePudding user response:
Cry, always part of the right to change the also is such, don't know what to changeCodePudding user response:
What is your part of the so-called right phenomenon?For example, such as what elements in a collection, collection of b what elements, and finally set c what elements?
CodePudding user response:
The