# 数据结构-初学者不会初始化(2)

www.MyException.Cn  网友分享于：2013-02-25  浏览：5次

int max_elem(Line *A);
void select_elem (Line *A,int elem);
void disp (Line *A);

main ()
{
int Max;
int item;
int elem;
Line L;
create_line (&L);
disp(&L);
printf ( "input want insert item: " );
scanf ( "%d ",&item);
printf ( "input a number insert the array: ");
scanf ( "%d ",&elem);
insert_elem(&L,item,elem);
disp(&L);
printf ( "input want delete elem 's value: ");
scanf ( "%d ",&elem);
del_elem(&L,elem);
disp(&L);
printf ( "input want select elem 's value: ");
scanf ( "%d ",&elem);
select_elem (&L,elem);
Max=max_elem(&L);
printf ( "Max elem is :%d\n ",Max);
}

void disp (Line *A)
{
int i;
for (i=0;i <A-> last;i++)
{
printf ( "%d ",A-> array[i]);
if ((i+1)==10)
printf ( "\n ");
}
printf ( "\n ");
}

void create_line (Line *A)
{
int i,n;
A-> last=0;
n=rand()%30+1;
for (i=0;i <n;i++)
{
// srand((unsigned)time(NULL));
A-> array[i] = (rand()%20)+1;
A-> last=i+1;
}
}

void del_elem(Line *A, int elem)
{
int i,j;
if (A-> last==0)
{
printf ( "The array is null! ");

}
else
for (i=0;i <A-> last;i++)
{
if (A-> array[i]==elem)
{
for (j=i;j <A-> last;j++)
A-> array[j]=A-> array[j+1];
A-> last--;
i--;
}
}

}

void insert_elem(Line *A, int item, int elem)
{
int i;
if (A-> last==MAX)
{
printf ( "The array is full! ");
}
if (item> =MAX||item <=0)
{
printf ( "Input error! ");
}
else
{
for (i=(A-> last+1);i> =item;i--)
{
A-> array[i]=A-> array[i-1];
}
A-> array[i]=elem;
A-> last++;
}
}

void select_elem (Line *A,int elem)
{
int i;
if (A-> last==0)
{
printf ( "The array is null! ");
}
else
{
for (i=0;i <A-> last;i++)
{
if (A-> array[i]==elem)
printf ( "Have %dth elem equal to %d\n ",i+1,elem);
}
}
}

int max_elem(Line *A)
{
int i,Max,t;
if (A-> last==0)
{
printf ( "The array is null! ");
return -1;
}
else
{
Max=A-> array[0];
for (i=1;i <A-> last;i++)
{
if (A-> array[i]> Max)
{
Max=A-> array[i];
t=i;
}
}
}
printf ( "the %dth is max_elem_number!\n ",t+1);
return Max;
}