c++ - 对嵌套结构单链和双链进行排序

标签 c++ c xcode visual-c++ gcc

我嵌套为:

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 * 则由您管理内存分配和释放。

关于c++ - 对嵌套结构单链和双链进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22203653/

相关文章:

c++ - cpp 中 Graph 的最简单实现

c - 用顶点数组 : nothing displayed 绘制一系列点

c++ - 仅为具有相同基类的类授予对函数的访问权限

c++ - 对临时对象的 const 引用的列表初始化

无法打印链接列表

c - 为什么我调用这个函数8次,结果都是上次的?在 C 中

ios - Swift 和 XCode 7 beta 2 不显示正在播放的歌曲的元数据

json - alamofire.error Code=-6006 “无法序列化 JSON

objective-c - UIScrollView 上的多个(相同的)UIView

c++ - 一个声明中多个函数的原型(prototype)