对嵌套结构单链接和双链接进行排序

sort nested struct single linked and double

本文关键字:链接 排序 结构 单链接 嵌套      更新时间:2023-10-16

我嵌套为:

typedef struct a{
    const char *        LongName;
    const char *        ShortName;
    int                 a;
    struct a    *       next;
}Level5;
typedef struct a1{
    const char *        LongName;
    const char *        ShortName;
    int                 frequency5;
    int                 id;
    Level5  *           linked;
    struct a1*          next_a1;
}Level4;
typedef struct a2{
    const char      *   Name;
    int                 frequency4;
    Level4      *       linked;
    int                 id;
    struct a2   *       next;
}Level3;
typedef struct a3{
    const char *        LongName;
    const char *        ShortName;
    int                 frequency3
    int                 id;
    Level3      *       linked;
}Level2;
typedef struct a4{
    const char *        LongName;
    const char *        ShortName;
    _Bool           top;
    int             id;
    int             frequency2;
    Level2      *   lined;
}Level1;

typedef struct cat{
    int         id;
const char  *   CatName;
Level1      *   linked;
}CAT;
CAT     total[256];

现在我想对链接的嵌套结构结构进行排序,如下所示:

 1. Level 5 -> LongName
 2. Level 4 -> ShortName
 3. Level 3 -> Name
 4. Level 2 -> Frequency3
               Level 1
                   |
                   | Level 2
     ====================================================
     | Level3         |Level3         |level 3
 =============       
 |     next       next   
level4 ->     lev4 ->   lev4
 |              |
 |                   =====================================
===============================       |    
|       next        next           Level5 ->  level5  ->  level5
Level5  ->    Level5  ->  level5   

看起来您具有以下数据结构:

Level5 node --> Level5 node --> Level5 node  
  |  
  v  
Level4 node --> Level4 node --> Level4 node  
  |  
  v  
Level3 node --> level3 node --> Level3 node  
  |  
  v  
Level2 node --> level2 node --> Level2 node  

如果是这种情况,可以简化数据结构:

struct Node
{
};
struct Node_Row
{
   unsigned int level;
   std::list<Node> nodes_in_row;
};
struct data_structure
{
   std::list<Node_Row> levels;
};

您应该有一个对行进行排序的比较器函数和另一个对级别进行排序的比较器函数。

另外,由于您将问题标记为C++,因此您应该省去一些麻烦并使用std::string而不是char *std::string为您管理动态内存分配和释放,而char *管理内存分配和释放。