输入一个数,查找一个链表中与这个数最接近的,并且输出?哪里有问题
//建立一个链表 找出与它最接近的数据#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct student
{
char name[10];
int age;
struct student *next;
};
void find(struct student *head,int age)
{
int array[4]={0};
int max = 50;
int j,temp;
struct student *p = malloc(sizeof(struct student));
struct student *q = NULL;
p = head;
int i = 0;
while(p != NULL)
{
array[i] = (p->age)-age;
p = p->next;
if(array[i]<0)
array[i] = 0 -array[i];
i++;
}
for(j=0;j<4;j++)
{
if(array[j]<max)
{
max = array[j];
temp = j;
}
}
q =head;
for(j=0;j<temp;j++)
q = q->next;
printf("%s",q->name);
}
int main()
{
struct student stru[4] = {{"wang",24},{"wwe",27},{"wll",30},{"yyf",34}};
int i;
for(i=0;i<4;i++)
{
printf("%s %d",stru[i].name,stru[i].age);
printf("\n");
}
find(stru,29);
return 0;
}