#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 40
typedef struct {
int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq( SqList &L ) {
L.elem = ( int * )malloc( MAX * sizeof( int ) );
if( ! L.elem ) exit( -2 );
L.length = 20;
L.listsize = MAX;
return 1;
}
int DeleteOddNumbers( SqList &L ) {
int i = 0,j,k,m;
int *p,*q;
for( i ; i < L.length ; i ++ ) {
k = 0;
for( j = 1 ; j <= sqrt(L.elem[i]) ; j ++ ) {
m = L.elem[i] % j;
if( m == 0 ) k ++ ;
}
if( k < 2 ) {
p = &(L.elem[i - 1]);
q = L.elem + L.length - 1;
for( ++p ; p <= q ; ++p )
*(p-1) = *p;
-- L.length;
i --;
}
}
return 1;
}
int main () {
SqList a;
InitList_Sq( a );
printf("Input L:");
for( int i = 0; i < a.length ; i ++ )
scanf("%d",&a.elem[i]);
for( i = 0; i < a.length ; i ++ )
printf("%d ",a.elem[i]);
printf("\n");
DeleteOddNumbers(a);
for( i = 0 ; i < a.length ; i ++ )
printf("%d ",a.elem[i]);
printf("\n");
getch();
free( a.elem );
return 0;
}
删除顺表中所有为奇数的项