#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct node *Link;
struct node{
int item;
Link next;
}
Link NODE(int item, Link next)
{
link t=malloc(sizeof *t);
t->item = item;
t->next = next;
return t;
}
void show_list(link head)
{
link t;
for(t=head->next; t; t->next)
printf("%3d", t->item);
printf("/n");
}
Link init_list()
{
int i; Link t = NULL;
for(i=0; i<=N; i++) t=NODE(rand()%100, t);
return t;
}
Link selection_sort(Link head)
{
link s=NULL, t, max;
while(head->next)
{
for(max=head, t=max->next; t->next; t=t->next)
if(t->next->item > max->next->item) max = t;
t=max->next; max->next=t->next;
t->next=s; s=t;
}
head->next = s;
return head;
}
int main(void)
{
Link head=init_list();
show_list(head);
head=selection_sort(head);
show_list(head);
return 0;
}
|