?? 6-6-2.c
字號:
/*中國系統分析員顧問團,http://www.csai.cn*/
/*程序員下午考試指南書籍源碼*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int value;
struct node *next;
};
struct node *create(int a[], int n){
struct node *h, *q;
for(h=NULL;n;n--){
q = ( struct node *)malloc(sizeof(struct node ));
q -> value = *a++;
q->next = h;
h = q;
}
return h;
}
void sort( struct node **h ){
struct node *p,*q,*r,*s,*hl;
hl = p = ( struct node*)malloc(sizeof(struct node ));
p->next = *h;
while( p->next != NULL ){
q = p->next;
r = p;
while( q->next != NULL ){
if ( q->next->value <r->next->value )
r = q;
q = q->next;
}
if( r != p ){
s = r->next;
r->next = s->next;
s->next = p->next;
p->next = s;
}
p = p->next;
}
*h = hl->next;
free(hl);
}
int test_data[] = {5,9,3,4,5,7,8};
main(){
struct node *h, *p;
h = create(test_data,sizeof(test_data)/sizeof(test_data[0]));
for(p = h; p; p = p->next) printf("%5d",p->value);
printf("\n");
sort(&h);
for(p = h; p; p = p->next) printf("%5d",p->value);
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -