Chinaunix首页 | 论坛 | 博客
  • 博客访问: 201379
  • 博文数量: 63
  • 博客积分: 2764
  • 博客等级: 少校
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-28 21:11
文章分类

全部博文(63)

文章存档

2011年(35)

2010年(28)

我的朋友

分类: C/C++

2010-09-06 17:51:57




#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;

}


阅读(824) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~