#include<iostream>
#include<stdio.h>
#include<fstream>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define Status int
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 10
#define MAX_LINE 10
#define MAX_CHAR 200
using namespace std;
typedef struct{
int weight, parent, lchild, rchild;
char data;
}ElemType;
typedef struct{
ElemType * elem;
int length;
int listsize;
}Sqlist;
typedef char * * HuffmanCode;
typedef struct{
char *base;
char *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &s){//初始化栈函数。
s.base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
if(!s.base)
exit(OVERFLOW);
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return OK;
}
Status push(SqStack &s, char e){//入栈函数。
if(s.top - s.base >= s.stacksize){
s.base = (char *)realloc(s.base,
(s.stacksize + STACK_INIT_SIZE) * sizeof(char));
if(!s.base)
exit(OVERFLOW);
s.top = s.base + s.stacksize;
s.stacksize += STACK_INIT_SIZE;
}
*s.top++ = e;
return OK;
}
Status pop(SqStack &s, char &e){//出栈函数。
if(s.top == s.base)
return ERROR;
e = * --s.top;
return OK;
}
Status emptystack(SqStack s){//判定栈空的函数。
if(s.base == s.top)
return OK;
else
return ERROR;
}
void select(Sqlist HT, int i, int &s1, int &s2, static bool *flag){
//选择两个未被访问的最大的数组下标分别赋给s1, s2。
int n = (HT.listsize + 1)/2;
int weight, xiabiao;
//求第一个最大值。
for(int i = 0; i < HT.listsize; i++){
if(flag[i] != true){
weight = HT.elem[i].weight;
xiabiao = i;
break;
}
}
for(int j = 1; j < i; j++){
if(HT.elem[j].weight < weight && flag[j] == false){
weight = HT.elem[j].weight;
xiabiao = j;
}
}
s1 = xiabiao;
flag[s1] = true;
//求第二个最大值。
for(int i = 0; i < HT.listsize; i++){
if(flag[i] != true){
weight = HT.elem[
数据结构实验4---用HuffmanTree进行编码、译码
最新推荐文章于 2020-12-09 16:03:39 发布