在下列添加边的代码中增加边的权值:// 添加边 void AddEdge(Graph* G, int v1, int v2) { ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v2; p->next = G->vertices[v1].firstarc; G->vertices[v1].firstarc = p; ++G->arcnum; p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v1; p->next = G->vertices[v2].firstarc; G->vertices[v2].firstarc = p; ++G->arcnum; }
时间: 2024-03-17 19:46:00 浏览: 27
可以在代码中增加一个变量来表示边的权值,例如我们可以在ArcNode结构体中增加一个weight变量,然后在AddEdge函数中对这个变量进行赋值即可。
修改后的代码如下:
```
// 添加边
void AddEdge(Graph* G, int v1, int v2, int weight) {
ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = v2;
p->weight = weight; // 添加权值
p->next = G->vertices[v1].firstarc;
G->vertices[v1].firstarc = p;
++G->arcnum;
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = v1;
p->weight = weight; // 添加权值
p->next = G->vertices[v2].firstarc;
G->vertices[v2].firstarc = p;
++G->arcnum;
}
```
在调用AddEdge函数时,需要传入一个表示边权值的参数。例如,如果要添加从v1到v2的权值为3的边,可以这样调用:
```
AddEdge(&G, v1, v2, 3);
```
相关问题
完善代码:void AddEdge(Graph* G,char v1,char v2,int weight) { ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v2; p->weight = weight; // 添加权值 p->next = G->vertices[v1].firstarc; G->vertices[v1].firstarc = p; ++G->arcnum; p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = v1; p->weight = weight; // 添加权值 p->next = G->vertices[v2].firstarc; G->vertices[v2].firstarc = p; ++G->arcnum; }
void AddEdge(Graph* G, char v1, char v2, int weight) {
ArcNode* p1 = (ArcNode*)malloc(sizeof(ArcNode));
p1->adjvex = v2;
p1->weight = weight;
p1->next = G->vertices[v1].firstarc;
G->vertices[v1].firstarc = p1;
++G->arcnum;
ArcNode* p2 = (ArcNode*)malloc(sizeof(ArcNode));
p2->adjvex = v1;
p2->weight = weight;
p2->next = G->vertices[v2].firstarc;
G->vertices[v2].firstarc = p2;
++G->arcnum;
}
在原有代码上,增加了 ArcNode* p2 指针,用于添加双向边。在第一段代码中,我们添加了指向 v2 的边,而在第二段代码中,我们添加了指向 v1 的边。这样就可以确保两个顶点之间的边是双向的。
对以下代码进行细致注释#pragma once #include <map> #include <iomanip> //哈夫曼树存储结构 struct HuffmanNode { int weight; //结点字符和权值 int parent, lchild, rchild; }; class Huffman { private: map<char, int> code; map<string, char> sc_code; map<char, string> cs_code; const int MAXWEIGHT = 2147483647; HuffmanNode* hf = NULL; int N = 0; public: //无参构造 Huffman(); //初始化 void Initialization(); //选择 void select(HuffmanNode*& hf, int n, int& s1, int& s2); //创建Huffman树 void createHuffmanTree(HuffmanNode*& hf, int n); //Huffman编码实现 char** createHuffmanCode(HuffmanNode*& hf, int n); //编码 void Encoding(); //译码 void Decoding(); //印代码文件 void Print(); //印哈夫曼树 void TreePrinting(); };
// 防止头文件重复引用
#pragma once
// 使用map需要引入的头文件
#include <map>
// 用于输出格式化的头文件
#include <iomanip>
// 定义哈夫曼树的结构体
struct HuffmanNode {
int weight; // 结点字符和权值
int parent, lchild, rchild; // 结点父节点、左孩子节点和右孩子节点
};
// 定义哈夫曼编码的类
class Huffman {
private:
// 原始字符 -> 权值的映射
map<char, int> code;
// 压缩后的二进制位 -> 原始字符的映射
map<string, char> sc_code;
// 原始字符 -> 压缩后的二进制位的映射
map<char, string> cs_code;
// 定义最大权值
const int MAXWEIGHT = 2147483647;
// 定义哈夫曼树的指针
HuffmanNode* hf = NULL;
// 定义哈夫曼树的结点数目
int N = 0;
public:
// 无参构造函数
Huffman();
// 初始化哈夫曼编码
void Initialization();
// 选择权值最小的两个结点
void select(HuffmanNode*& hf, int n, int& s1, int& s2);
// 创建哈夫曼树
void createHuffmanTree(HuffmanNode*& hf, int n);
// 实现哈夫曼编码
char** createHuffmanCode(HuffmanNode*& hf, int n);
// 编码
void Encoding();
// 译码
void Decoding();
// 输出压缩后的代码文件
void Print();
// 输出哈夫曼树的结构
void TreePrinting();
};
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)