处理结构数组的初始化问题

哈夫曼树的创建方法(一个指针)


```c
HuffmanCode* readHuffmanCodesFromFile(const char* filename, int numCodes) {
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        perror("无法打开文件");
        exit(1);
    }

   // 1 1 0 0 0 1
    const int maxLines = 1000;
    HuffmanCode* huffmanCodes = (HuffmanCode*)malloc(maxLines * sizeof(HuffmanCode));

    char line[100]; // 假设每行不超过 100 个字符
    numCodes = 0;

    while (fgets(line, sizeof(line), file) != NULL) {
        if (numCodes >= maxLines) {
            fprintf(stderr, "超过编码储存数量。\n");
            fclose(file);
            free(huffmanCodes);
            exit(1);
        }

        // 解析每一行,假设格式为 "字符: 编码"
        char character;
        char code[100];
        if (sscanf(line, "%c: %s", &character, code) == 2) {
            huffmanCodes[numCodes].character = character;
            huffmanCodes[numCodes].code = strdup(code);
            (numCodes)++;
        }
    }

    fclose(file);
    return huffmanCodes;
}

### 调用方法

```c
 BiTNode *root = buildHuffmanTree(symbols, frequencies, size);

双重结构指针构造方法

luxian **create(char *path[], char *time[], char *starPath[], char *endPath[], char *chengyuan[], int flightNum, int already, int totle)
{
	luxian **root = (luxian **)malloc(sizeof(luxian *)); // 创建指针数组 root
	for (int i = 0; i < flightNum; i++)
	{
		// 为每个指针分配内存
		// 初始化队列
		root[i] = new luxian;
		InitQueue(root[i]->feiji->remainWait);
	}
	for (int i = 0; i < flightNum; i++)
	{

		InitQueue(root[i]->feiji->remainWait);
		// 设置 luxian 结构体实例的成员值
		root[i]->path = path[i];
		root[i]->rutine = time[i];
		root[i]->Star = starPath[i];
		root[i]->End = endPath[i];
		root[i]->feiji->chengyuan = chengyuan[i];
		root[i]->feiji->already = already;
		root[i]->feiji->totle = totle;
		root[i]->feiji->left = root[i]->feiji->totle - root[i]->feiji->already;
	}

	return root;
}

调用方法


```c
	int flightNum = 2;
	int num = 1;
	int already = 1;
	int totle = 2;
	char ch;
	luxian **hangxian;
	char *path[2] = {"aToz", "sTox"};
	char *time[5] = {"nine to twelevels", "twelevels to pmtwo", "pmtwo to pm4"};

	char *starPath[4] = {"a", "s", "d", "f"};
	char *endPath[5] = {"z", "x", "c", "v"};
	char *chengwurenyuan[2] = {"hong", "zhang"};
	char name[20];
	char *name1;

hangxian = create(path, time, starPath, endPath, chengwurenyuan, flightNum, already, totle);
		

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值