1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define ElemType int 5 #define N 5 //定义链表的结点数目 6 typedef struct Node 7 { 8 ElemType data; 9 struct Node *next; 10 }Node; 11 typedef struct Node *LinkList; 12 13 14 15 Node* CreatList(Node *La,int n) 16 { 17 int i; 18 struct Node *s; 19 La = (LinkList)malloc(sizeof(Node)); 20 La->next = NULL; //初始化 21 for(i = 1;i <= n;i++) 22 { 23 s = (LinkList)malloc(sizeof(Node)); 24 scanf("%d",&(s->data)); 25 s->next = La->next; 26 La->next = s; //利用头插法构造链表 27 } 28 29 return La; 30 } 31 //函数值调换 32 int SortList(Node *Lb,int n) 33 { 34 int i; 35 int a[n]; 36 for(i = 0;i < n;i++) 37 { 38 Lb = Lb->next; 39 a[i] = Lb->data; 40 } 41 for(i = n-1;i >= 0;i--) 42 { 43 printf("%3d",a[i]); 44 } 45 return 1; 46 } 47 int main(int argc, char *argv[]) 48 49 { 50 int i; 51 struct Node *L,*p; 52 L = CreatList(L,N); 53 p = L; 54 printf("the data is:\n"); 55 for(i = 1;i <= N;i++) 56 { 57 p = p->next; 58 printf("%3d",p->data); 59 } 60 printf("\nAfter sorted,the data is\n"); 61 SortList(L,N); 62 getch(); 63 // system("pause"); 64 return 0; 65 }
时间: 2024-10-26 10:47:55