链表实现冒泡排序

来源:互联网 发布:网络免费赚钱项目 编辑:程序博客网 时间:2024/06/06 15:02

单链表实现冒泡排序

[cpp] view plain copy
print?
  1. /************************************************************************/  
  2. /* 用单链表实现冒泡排序,输入为0-9的数字字符                               */  
  3. /************************************************************************/  
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6.   
  7. #define PR  printf   
  8.   
  9. //定义一个单链表结构体  
  10. typedef struct node{  
  11.    long data;  
  12.    struct node * next;  
  13. }lklist;  
  14.   
  15. //快速排序,小的数据往前排,后的数据往后排  
  16. lklist* sort(lklist * head)  
  17. {  
  18.     if (head == NULL)  
  19.         return NULL;  
  20.     lklist* p = head->next;  
  21.     lklist* p_pre = p;  
  22.     bool flag = false;   //用于标记是否有交换,当数组有序的时候,提高判断效率  
  23.   
  24.     while(p_pre->next != NULL)  
  25.     {  
  26.         long temp = p_pre->data;  
  27.         p = p->next;  
  28.         while (p)  
  29.         {      
  30.             if(temp <= (p->data))  
  31.             {  
  32.                 p = p->next;  
  33.                 continue;  
  34.             }  
  35.             else  
  36.             {  
  37.                 long temp_change;  
  38.                 temp_change = p->data;  
  39.                 p->data = p_pre->data;  
  40.                 p_pre->data = temp_change;  
  41.                 p = p->next;  
  42.                 flag = true;  
  43.             }  
  44.             if (flag == false)  
  45.             {  
  46.                 return head;  
  47.             }  
  48.         }         
  49.         p_pre = p_pre->next;  
  50.         p = p_pre;  
  51.     }  
  52.     return head;  
  53.       
  54. }  
  55. //向含有一个头节点的单链表中插入数据  
  56. lklist* create_lklist(lklist *head)  
  57. {     
  58.     PR("please input the number of the data and end with q(Q):\n");  
  59.     while(1)  
  60.     {  
  61.         char ch[2];  
  62.         scanf("%s",ch);  
  63.         if(ch[0] == 'q' || ch[0] == 'Q')  //输入为q(Q)时停止输入  
  64.             break;  
  65.         else  
  66.         {  
  67.             long intData = strtol(ch,0,10);  
  68.             lklist* temp = NULL;  
  69.             temp = (lklist*)malloc(sizeof(struct node));  
  70.             temp->data = intData;  
  71.             temp->next = NULL;  
  72.       
  73.             temp->next = head->next;  
  74.             head->next = temp;  
  75.         }             
  76.     }  
  77.     return head;  
  78. }   
  79. //打印单链表中的数据信息  
  80. void printf_lklist(lklist* head)  
  81. {  
  82.     if (NULL == head)  
  83.     {  
  84.         return;  
  85.     }  
  86.     lklist * p = head->next;  
  87.     PR("the sorted number is:\n");  
  88.     while (p)  
  89.     {         
  90.         PR("%d  ",p->data);  
  91.         p = p->next;       
  92.     }  
  93.   
  94.     PR("\n");  
  95. }  
  96. //释放有数据的链表中的节点  
  97. void free_lklist(lklist* head)  
  98. {  
  99.     if (NULL == head)  
  100.     {  
  101.         return;  
  102.     }  
  103.     lklist * p = head->next;  
  104.     lklist *pre;  
  105.     while (p)  
  106.     {  
  107.         pre = p;  
  108.         p = p->next;  
  109.         free(pre);  
  110.     }  
  111. }  
  112.   
  113. void main()  
  114. {     
  115.     lklist *head = (struct node *)malloc(sizeof(struct node));  
  116.     head->next = NULL;  //头节点不存放数据  
  117.     head = create_lklist(head);  
  118.   
  119.     head = sort(head);  
  120.     printf_lklist(head);  
  121.     free_lklist(head);  
  122.     free(head); //释放申请的头节点  
  123. }  

原创粉丝点击