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;
	}

}
相关推荐
潮汐退涨月冷风霜几秒前
数字图像处理(1)OpenCV C++ & Opencv Python显示图像和视频
c++·python·opencv
长河1 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言
第七序章1 小时前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
Cyan_RA91 小时前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
逆小舟3 小时前
【Linux】人事档案——用户及组管理
linux·c++
再见晴天*_*4 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
lqjun08276 小时前
Qt程序单独运行报错问题
开发语言·qt
hdsoft_huge7 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘8 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务8 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟