user-0 提问: 动态通讯录，结构体这样排序为什么不对？？？？？？

回复:
user: user-1
content: 数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633

user: user-2
content: #define _CRT_SECURE_NO_WARNINGS 1  #include<stdio.h> #include<stdlib.h> #include<string.h>  struct S {     char name[20];     //int num[12];     char num[12];     int age;     char sex[2];     struct S *next; };  //展示联系人 void show(struct S *head) {     struct S*cur = head;     while (cur != NULL)     {         printf("%s %s %d %s\n", cur->name, cur->num, cur->age, cur->sex);         cur = cur->next;     } }  //排序 void sort(struct S *head) {     struct S*pre = head;     struct S*cur = head;     for (pre = head; pre != NULL; pre = pre->next)     {         for (cur = pre; cur != NULL; cur = cur->next)         {             if ((*pre).age > (*cur).age)             { #if 1                 struct S tmp;                 strcpy(tmp.name, pre->name);                 strcpy(tmp.num, pre->num);                 tmp.age = pre->age;                 strcpy(tmp.sex, pre->sex);                  strcpy(pre->name, cur->name);                 strcpy(pre->num, cur->num);                 pre->age = cur->age;                 strcpy(pre->sex, cur->sex);                  strcpy(cur->name, tmp.name);                 strcpy(cur->num, tmp.num);                 cur->age = tmp.age;                 strcpy(cur->sex, tmp.sex); #else                 struct S tmp;                 tmp = (*pre);                 (*pre) = (*cur);                 (*cur) = tmp; #endif             }         }     }     show(head); }  //查找联系人 void find(struct S *head) {     //int arr[20];     char arr[20];     struct S*cur = head;     printf("请输入你要查找的联系人姓名：");     scanf("%s", arr);     while (cur != NULL)     {         if (strcmp(cur->name, arr) == 0)         {             printf("%s %s %d %s\n", cur->name, cur->num, cur->age, cur->sex);             break;         }         cur = cur->next;     }     if (cur == NULL)     {         printf("没有找到！\n");     } } //删除联系人 void erase(struct S**head) {     struct S *cur = *head;     struct S *pre = NULL;     char arr[32] = { 0 };     printf("请输入你要删除的联系人姓名：");     scanf("%s", arr);     while (cur!= NULL)     {         if (strcmp(cur->name, arr) == 0)         {             if (cur == *head)    //如果被删除的元素是头指针             {                 *head = cur->next;             }             else             {                 pre->next = cur->next;             }         }         else             pre = cur;         cur = cur->next;     }     show(*head); } //添加联系人 void insert(struct S **head) {     //创建一个链表     struct S *p=(struct S*)malloc(sizeof(struct S));     printf("姓名：>");     scanf("%s", p->name);     printf("电话：>");     scanf("%s", p->num);     printf("年龄：>");     scanf("%d", &p->age);     printf("性别：>");     scanf("%s", p->sex);     p->next = NULL;    //最后一 个元素指向下一个结构体     if (*head == NULL)  //插入首元素     {         *head = p;     }     else               // 插入其他元素     {         struct S *cur = *head;         while (cur->next != NULL)         {             cur = cur->next;         }         cur->next = p;     }     show(*head); } void menu() {     printf("****************************************\n");     printf("*1   添加联系人*******2   删除联系人****\n");     printf("*3   查找联系人*******4   联系人排序****\n");     printf("****************************************\n"); } int main() {     int input;     struct S *head = NULL; //定义一个结构体变量person     do     {         menu();         printf("Enter your choice:");         scanf("%d", &input);         switch (input)         {             case 1:insert(&head); break;             case 2:erase(&head); break;             case 3:find(head); break;             case 4:sort(head); break;             case 0:printf("退出\n"); break;             default:printf("输入错误，请重新输入\n"); break;         }     } while (input);     system("pause");     return 0; } 参考一下吧  排序选择只交换数据部分，指针（地址）部分不要移动。若按照你的交换是数据和地址都交换了。 另外，num若是指电话号码，就不要定义成int类型了。当然还有局部变量arr