zoukankan      html  css  js  c++  java
  • 单链表的排序

    问题: 将无序的单链表按从小到大排序

    创新方法: 只对结点存放的值修改。  

    思路:建立指针数组,映射到结点存放的值的地址, 然后间接访问这些结点的值,对它们排序。

    代码:

    预编译 

    #include <stdlib.h>
    #include <stdio.h>
    #define status int
    #define FALSE 0
    #define TRUE 1

    采用的链表结构

    typedef struct NODE{
        struct NODE *next;    /* 指向下一个结点 */
        int value;            /* 存放结点值 */
    } Node, *PNode, **List; 
    
    typedef struct LINK{
        struct NODE *next;    /* 指向下一个结点 */
    } Linker;

    单链表排序

      1 /*
      2 功能描述:
      3     对单链表排序( 从大到小 )
      4 
      5 参数:
      6     p_head -- 链表的头指针
      7 
      8 返回值:
      9      如果排序成功,返回TRUE; 否则, 返回FALSE 
     10 */
     11 status
     12 SortTheChain( Linker *p_head )
     13 {
     14         static int Count( Linker  *p_head );
     15         static status Map( Linker *p_head, int  ***p_index, int node_num );
     16 
     17     int node_num;        /* 存放链表中结点个数 */
     18     int **index = NULL; /* 存放映射 */
     19     int i, j;            /* 分别控制比较的趟数和次数 */
     20     int temp;            
     21 
     22     /* 计算结点个数 */
     23     node_num = Count( p_head );
     24     
     25     /* 映射结点的value字段 */
     26     if ( Map( p_head, &index, node_num ) == FALSE ){
     27         printf( "映射失败
    " );
     28         return FALSE;
     29     }
     30 
     31     /* 排序 */
     32     for ( i = 1; i <= node_num - 1; i ++ ){
     33         for ( j = 0; j < node_num - i; j ++ ){
     34             if ( *index [j] < *index [j + 1] ){
     35                 temp = *index [j];
     36                 *index [j] = *index [j + 1];
     37                 *index [j + 1] = temp;
     38             }
     39         }
     40     }
     41     
     42     /* 释放掉映射 */
     43     free( index );
     44     index = NULL;
     45     return TRUE;
     46 } 
     47 
     48 
     49 /*
     50 功能描述:
     51     计算单链表的长度
     52 
     53 参数:
     54     p_head -- 指向链表的头结点 
     55 
     56 返回值:
     57     返回链表中结点个数 
     58 */
     59 static int 
     60 Count( Linker *p_head )
     61 {
     62     PNode p = NULL;
     63     int num = 0;
     64     
     65     if ( p_head == NULL ){
     66         printf( "链表不存在
    " );
     67         return num;
     68     }
     69 
     70     for ( p = p_head->next; 
     71             p != NULL;
     72             p = p->next ){
     73                 num ++;
     74             }
     75 
     76     return num;    
     77 }
     78 
     79 /*
     80 功能描述:
     81     映射整形指针到单链表value字段的地址 
     82 
     83 参数:
     84     p_head -- 指向头结点  
     85     index -- 指向待映射的整形指针集
     86     node_num -- 链表的结点个数 
     87     
     88 返回值:
     89     如果映射成功, 返回TRUE; 否则, 返回FALSE 
     90 */
     91 static status 
     92 Map( Linker *p_head, int ***p_index, int node_num )
     93 {
     94     int i;
     95     PNode p = NULL;
     96 
     97     /* 申请内存 */
     98     *p_index = (int **) malloc ( node_num * sizeof(int *) );
     99     if ( *p_index == NULL ){
    100         printf( "内存不足
    " );
    101         return FALSE;
    102     }
    103     
    104     /* 建立映射 */
    105     for ( i = 0, p = p_head->next; p != NULL; p = p->next ){
    106         (*p_index) [i ++] = &p->value;
    107     }
    108     return TRUE;
    109 }
    排序以及需要调用的函数
  • 相关阅读:
    百度云盘下载限速破解
    (五)Struts之Action类基础(二)
    (四)Decorator设计模式解决GET/POST请求的乱码问题(转)
    (三)Struts之Action类基础(一)
    (二)Struts.xml文件详解
    (一)Struts2 基础
    (三十一)web 开发基础项目
    mysql的索引
    数据库的acid
    String StringBuffer和StringBuilder
  • 原文地址:https://www.cnblogs.com/the-one/p/4646912.html
Copyright © 2011-2022 走看看