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

}
相关推荐
wei_shuo32 分钟前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>32 分钟前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
小小小小王王王41 分钟前
求猪肉价格最大值
数据结构·c++·算法
GO兔43 分钟前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼1 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
岁忧1 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
future14122 小时前
C#学习日记
开发语言·学习·c#
码农编程录2 小时前
【c/c++3】类和对象,vector容器,类继承和多态,systemd,std&boost
c++
king_harry2 小时前
Java程序-OceanBase Connector/J 示例
开发语言
傻啦嘿哟3 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#