1 #includestdio.h 2 #includestdlib.h 3 #define LEN sizeof(struct A) 4 5 struct A 6 { 7 int num; 8 struct A * next; 9 }; 10 struct A *creat( void ) //创建链表 11 { 12 struct A *head, *p1, * p2; 13 int n, i = 0 ; 14 scanf( " %d " , n); 15
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define LEN sizeof(struct A) 4 5 struct A 6 { 7 int num; 8 struct A *next; 9 }; 10 struct A *creat(void)//创建链表 11 { 12 struct A *head, *p1, *p2; 13 int n, i = 0; 14 scanf("%d", &n); 15 p1 = p2 = (struct A*)malloc(LEN); 16 scanf("%d", &p1->num); 17 head = NULL; 18 while (i <n) 19 { 20 i = i + 1; 21 if (i == 1) 22 head = p1; 23 else 24 p2->next = p1; 25 p2 = p1; 26 if (i <n) 27 { 28 29 p1 = (struct A*)malloc(LEN); 30 scanf("%d", &p1->num); 31 } 32 } 33 p2->next = NULL; 34 return head; 35 } 36 struct A *selectsort(struct A *head)//选择排序 37 { 38 struct A *cur, *p, *q; 39 cur = head; 40 int a = 0, b = 0; 41 if (cur == NULL) 42 return NULL; 43 while (cur != NULL) 44 { 45 p = cur->next; 46 q = cur->next; 47 a = cur->num; 48 b = a; 49 while (p != NULL) 50 { 51 if (b>p->num) 52 { 53 b = p->num; 54 q = p; 55 } 56 p = p->next; 57 } 58 if (a != b) 59 { 60 q->num = a; 61 cur->num = b; 62 } 63 cur = cur->next; 64 } 65 return head; 66 } 67 void print(struct A *head)//打印链表 68 { 69 struct A *p; 70 p = head; 71 if (head != NULL) 72 { 73 do 74 { 75 printf("%d ", p->num); 76 p = p->next; 77 } while (p != NULL); 78 } 79 } 80 int main() 81 { 82 struct A *head = creat(); 83 selectsort(head); 84 print(head); 85 return 0; 86 }