图片附件: 游客没有浏览图片的权限,请
登录 或
注册
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Person
{
char name[20];
long id;
float salary;
} PERSON, *PPERSON;
typedef struct _LIST_
{
PERSON per;
struct _LIST_ *next;
} LIST, *PLIST;
PLIST _insertsort(PLIST h, PPERSON a)
{
PLIST p, q, s;
p = h;
q = NULL;
s = (PLIST)malloc(sizeof(LIST));
strcpy(s->per.name, a->name);
s->per.id = a->id;
s->per.salary = a->salary;
s->next = NULL;
while (p)
{
if (a->salary <= p->per.salary)
{
s->next = p;
break;
}
q = p;
p = p->next;
}
if (q)
q->next = s;
else
h = s;
return h;
}
void _free(PLIST h)
{
PLIST p=h, q;
while (p)
{
q = p->next;
free(p);
p = q;
}
}
void _list(PLIST h)
{
printf("name\tid\tsalary\n");
printf("------- ------- -------\n");
PLIST p=h;
while (p)
{
printf("%s\t%d\t%.2f\n", p->per.name, p->per.id, p->per.salary);
p = p->next;
}
}
PERSON allone[6]={{"jone",12345,339.0},{"david",13916,449.0},{"marit",27519,311.0},
{"jasen",42876,623.0},{"peter",23987,400.0},{"yoke",12335,511.0}};
int main()
{
PLIST h=NULL;
int i,j;
for (i=0; i<6; i++)
h = _insertsort(h, &allone[i]);
_list(h);
_free(h);
return 0;
}