大家帮忙看一看我的代码有什么问题?
  • 板块灌水区
  • 楼主Yuzekai
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/4/14 17:03
  • 上次更新2024/4/14 20:31:01
查看原帖
大家帮忙看一看我的代码有什么问题?
1257765
Yuzekai楼主2024/4/14 17:03
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int m = 5;
typedef struct node{
  char data;
  struct node** child[m];
  int child_count;
}t;
t tb = {'b'},tc={'c'},td={'d'},tf={'f'},th={'h'},tz={'z'},ty={'y'};
t *t5,*t6,*ta;

void int6(){
	t6=(t*)malloc(sizeof(t));
	t6->data='g';
  t6->child = (t**)malloc(2 * sizeof(t*));
  t6->child_count = 2;
	t6->child[0]=&tz;
	t6->child[1]=&ty;
}
void int5(){
	t5 = (t*)malloc(sizeof(t));
	t5->data='e';
  t5 = (t**)malloc(3 * sizeof(t));
  t5->child_count = 3;
	t5->child[0]=&tf;
	t5->child[1]=&th;
	t5->child[2]=t6;
}
void init(){
    ta=(t*)malloc(sizeof(t));
    ta -> data='a';
  ta=(t**)malloc(5 * sizeof(t));
  ta->child_count = 5;
    ta -> child[0]=&tb;
    ta -> child[1]=&tc;
    ta -> child[2]=&td;
    ta -> child[3]=t5;
    ta -> child[4]=t6;
  printf("%c\n", ta -> child[4] -> data);
  printf("%c\n", ta -> child[4] -> child[0] -> data);
  printf("%c\n", ta -> child[4] -> child[1] -> data);
}
void get(t *ts){
    int i=0,m1;
    if(ts == null){
    	//printf("%c\n",ts -> data);
      return;
    }
  printf("%c\n",ts -> data);
    //for(i = 0; i < 5; i++){
      //if(ts -> child[i]){
        //   printf("%d\n", i);
          // get(ts -> child[i]);
        //}else{
        //break;
      //}
    //}
    for (i = 0; i < ts->child_count; i++) {
      printf("%d\n", i);
        get(ts->child[i]);
    }
}

void freeTree(Node* root) {
    if (root == NULL)
        return;

    for (int i = 0; i < root->child_count; i++) {
        freeTree(root->child[i]);
    }

    free(root->child);
    free(root);
}

int main(int argc, const char * argv[]) {
    int5();
    int6();
    init();
    get(ta);
    printf("Hello, World!\n");
  freeTree(ta);
    return 0;
}
2024/4/14 17:03
加载中...