# include <stdio.h>
# include <stdlib.h>
# define MAX 100
# include <assert.h>
typedef struct
{
int data[ MAX] ;
int length;
} SeqList;
int SeqSearch ( SeqList* S, int key)
{
printf ( "关键字序列:" ) ;
for ( int j = 0 ; j < S-> length; j++ )
{
printf ( "%d " , S-> data[ j] ) ;
}
int i = 0 ;
S-> data[ S-> length] = key;
printf ( "\n查找%d所比较的关键字:\n" , key) ;
while ( S-> data[ i] != key)
{
printf ( "%d " , S-> data[ i] ) ;
i++ ;
}
if ( i < S-> length)
{
printf ( "\n元素%d的位置是%d\n" , key, i) ;
return ( i + 1 ) ;
}
else
{
return - 1 ;
}
}
int main ( int argc, char * argv[ ] )
{
int a[ 10 ] = { 3 , 6 , 2 , 10 , 1 , 8 , 5 , 7 , 4 , 9 } ;
int n = sizeof ( a) / sizeof ( a[ 0 ] ) ;
SeqList* S = ( SeqList* ) malloc ( sizeof ( SeqList) ) ;
assert ( S) ;
for ( int i = 0 ; i < n; i++ )
{
S-> data[ i] = a[ i] ;
}
S-> length = n;
SeqSearch ( S, 5 ) ;
return 0 ;
}