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 分钟前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫4 分钟前
go orm GORM
开发语言·后端·golang
黑不溜秋的1 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
李白同学2 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?3 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农3 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿3 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!3 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴3 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程3 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛