指定任意层数;
指定任意层数内长度;
生成树形层状结构;
实现数据节点的挂载,和寻址;
适用于:桶排序、基数排序
#include <stdio.h>
#include <stdlib.h>
/*
author:YXP
e-mail:yxp189@protonmail.com
如有问题,欢迎和我联系~
转载请标明出处~
*/
int NOW_LEVEL = 0;
int LEVEL = 1; //搭建结构的层数;从0开始
int TIME = 0; // 生成程序的迭代次数
int counter = 0;//层数计数器
struct _level {
int level_th; //第几层
int lenth; //当前层的宽度
struct _level* last_level; //上一层的地址
struct _level** next_level; //下一层的链接
struct _node* head; //储存节点头
struct _node* tail; //储存末尾节点
};
struct _node { //节数据点定义
char CH_store;
int order;
int *traits;
struct _node* next;
};
int Build_tree(struct _level** TOP, int *level_th, int level_num);//生成层次结构
void Initalize_level(struct _level** HEAD, int *level_th, int now_level_th);//初始化层
int Insert(struct _level** TOP,struct _node* Node,int* level_th);//插入
void Print_content(struct _level** TOP,int *position_arr);//打印指定位置元素
int Find_ch_place(char CH,int** positin);//找到字符(10x10)的插入位置
int Create_struct_node(struct _node** Node,char CH);//创建数据节点
int main(int argc, char *argv[]) {
int level_th[2] = {10,10};//层的长度生成
struct _level* TOP = NULL;
Build_tree(&TOP, level_th, LEVEL);//建立层级
printf ("TIMES = %d\n",TIME);//打印建立的调用次数
struct _node* Node;
Create_struct_node(&Node,'A');//创建节点
Insert(&TOP,Node,Node->traits);//插入
Print_content(&TOP,Node->traits);//打印
int i;
printf("数据插入位置>>");
for (i=0;i<=LEVEL;i++){//输出存储的位置
printf("%d ",Node->traits[i]);
}
return 0;
}
int Find_ch_place(char CH,int** positin){
int temp = CH;
*positin = (int*)malloc((LEVEL+1)*sizeof(int));
int i;
for (i=0;i<(LEVEL+1);i++){
(*positin)[i] = temp%10;
temp/=10;
}
return 0;
}
int Create_struct_node(struct _node** Node,char CH)
{
*Node = (struct _node*)malloc(sizeof(struct _node));
(*Node)->CH_store = CH;
(*Node)->order = 0;
(*Node)->next = NULL;
Find_ch_place(CH,&((*Node)->traits));
}
void Print_content(struct _level** TOP,int *position_arr)
{
struct _level** temp1 = NULL,**temp2 = TOP;
int i,position;
for(i=0;i<LEVEL;i++){
temp1 = temp2;
position = position_arr[i];
temp2 = (*temp1)[position].next_level;
}
position = position_arr[i];
struct _node* CH_store = (*temp2)[position].tail;
printf ("CHAR = %c\n",CH_store->CH_store);
}
int Insert(struct _level** TOP,struct _node* Node,int* level_th)
{
int now_level = 0,position = 0;
struct _level** temp1 = NULL,**temp2 = TOP;
for (now_level=0;now_level<LEVEL;now_level++){
temp1 = temp2;
position = Node->traits[now_level];
temp2 = (*temp1)[position].next_level;
}
position = Node->traits[now_level];;
if ((*temp2)[position].tail == NULL){
Node->order = 0;
(*temp2)[position].head = Node;
(*temp2)[position].tail = Node;
}else{
Node->order = (*temp2)[position].tail->order + 1;
(*temp2)[position].tail->next= Node;
(*temp2)[position].tail = Node;
}
return 0;
}
int Build_tree(struct _level** TOP, int *level_th, int level_num)
{
Initalize_level(TOP, level_th, NOW_LEVEL);
return 0;
}
void Initalize_level(struct _level** HEAD, int *level_th, int now_level_th)
{
TIME ++;
int i = 0;
(*HEAD) = (struct _level*)malloc(level_th[now_level_th]*sizeof(struct _level));
for (i=0;i<level_th[now_level_th];i++)
{
(*HEAD)[i].level_th = now_level_th;
(*HEAD)[i].lenth = level_th[now_level_th];
if (now_level_th == 0) {
(*HEAD)[i].last_level = NULL;
}
else {
(*HEAD)[i].last_level = *HEAD;
}
(*HEAD)[i].head = NULL;
(*HEAD)[i].tail = NULL;
if (now_level_th<LEVEL) {
(*HEAD)[i].next_level = (struct _level**)malloc(level_th[now_level_th+1] * sizeof(struct _level*));
int j;
NOW_LEVEL++;
for (j=0;j<level_th[now_level_th+1];j++){
Initalize_level(&((*HEAD)[i].next_level[j]), level_th, now_level_th+1);
}
NOW_LEVEL--;
}
else {
(*HEAD)[i].next_level = NULL;
}
}
return;
}