C++ 统计一个字符串当每个字符出现的权重。

abbccc$b

b:2
本题目为第一步,读入待编码字符串,建造一个森林,请补全下列代码。

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

typedef char elemtype;

//带权值的二叉树

typedef struct BiTNode{

elemtype data;

int weight; //权重

struct BiTNode *lchild, *rchild; /*左右孩子指针*/

}BiTNode,*BiTree;

//用单链表表示森林

typedef struct linkNode{

BiTree tree;

struct linkNode *next;

}LinkNode, *Forest;

//创建森林

int createForest(Forest forest){

//待补全,读入字符串,以'$'为结束符,根据每个字符出现频率为权值,构造森林。并返回森林中树的个数

}

int main()

{Forest forest = malloc(sizeof(linkNode));

//森林的单链表包含一个头结点,头结点符号'$'

forest->tree = malloc(sizeof(BiTNode));

forest->tree->data = '$';

forest->next = NULL;createForest(forest);

char ch1;

scanf("%c", &ch1);

linkNode * p = forest->next;

while (p != NULL)

{

if (p->tree->data == ch1)

{

printf("%c:%d\n", p->tree->data, p->tree->weight);

}

p = p->next;

}

}

输入

abbccc$b

输出

b:2

样例输入 Copy

hello world$l

样例输出 Copy

l:3

提示

只需要补全并提交以下代码:

//创建森林

int createForest(Forest forest){

//待补全,读入字符串,以'$'为结束符,根据每个字符出现频率为权值,构造森林。

}

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef char elemtype;


//带权值的二叉树
typedef struct BiTNode {
	elemtype data;
	int weight; //权重
	struct BiTNode* lchild, * rchild; /*左右孩子指针*/
}BiTNode, * BiTree;


//用单链表表示森林
typedef struct linkNode {
	BiTree tree;
	struct linkNode* next;
}LinkNode, * Forest;

//创建森林
int createForest(Forest forest)
{
	//待补全,读入字符串,以'$'为结束符,根据每个字符出现频率为权值,构造森林。并返回森林中树的个数
	
	int count = 0; // 森林中树的个数
	BiTree tree;
	LinkNode* p = forest;

	char ch;
	

	char list[128] = {0};
	scanf("%c", &ch);
	while (ch != '$') {
		scanf("%c", &ch);
		list[ch] += 1;
	}
	for (int i = 0; i < 128; i++) {
		if (!list[i])
			continue;

		tree = (BiTNode*)malloc(sizeof(BiTNode));
		tree->data = i;
		tree->weight = list[i];
		tree->lchild = NULL;
		tree->rchild = NULL;

		LinkNode* newNode = (LinkNode*)malloc(sizeof(LinkNode));
		newNode->tree = tree;
		newNode->next = NULL;

		p->next = newNode;
		p = p->next;
		count++;
	}
	return count;
}


int main()
{
	Forest forest = (linkNode*)malloc(sizeof(linkNode));
	//森林的单链表包含一个头结点,头结点符号'$'
	forest->tree = (BiTNode*)malloc(sizeof(BiTNode));
	forest->tree->data = '$';
	forest->next = NULL;
	createForest(forest);
	char ch1;
	scanf("%c", &ch1);
	linkNode* p = forest->next;
	while (p != NULL)
	{
		if (p->tree->data == ch1)
		{
			printf("%c:%d\n", p->tree->data, p->tree->weight);
		}
		p = p->next;
	}

}
相关推荐
CodeWithMe16 分钟前
【C/C++】EBO空基类优化介绍
开发语言·c++
404.Not Found26 分钟前
Day46 Python打卡训练营
开发语言·python
love530love28 分钟前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
凌辰揽月30 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
海奥华234 分钟前
go中的接口返回设计思想
开发语言·后端·golang
lifallen36 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰36 分钟前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python
k要开心37 分钟前
从C到C++语法过度1
开发语言·c++
小吕学编程40 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
whoarethenext1 小时前
使用 C/C++的OpenCV 实时播放火柴人爱心舞蹈动画
c语言·c++·opencv