include<stdio.h>

include<stdlib.h>

typedef struct HT { char ch; unsigned weight; struct HT lchild,rchild; int parent; int flage; } HuffmanTree,*HuffmanTiTree;

void print(HuffmanTiTree *Ht,int num) { int i; HuffmanTree *T; for(i = 1; i <= num; i ++) { T = *(Ht + i); printf(" %d ",T->weight);
} free(Ht); }

int INinit(HuffmanTiTree Ht,void print(HuffmanTiTree *,int )) { HuffmanTree *T; int N; //预估最大权值个数 int n; //可能的权值个数 int M; //真实权值个数 unsigned weight; char Ch; int i=1; printf("请输入权值个数或者可能的最大值:\n"); scanf("%d",&n); N = 3 * n; if(!(Ht = (HuffmanTiTree)malloc(N * sizeof(HuffmanTiTree)))) exit(0); printf("请输入权值与其字符(如:2,a 当输入权值-1表示结束):\n"); while(1) { if(!(T = (HuffmanTree*)malloc(sizeof(HuffmanTree)))) exit(0); scanf("%d,%c",&weight,&Ch); if(weight == -1) //输入建数组 { free(T); Ht[i] = NULL; break;
} T->weight = weight; T->ch = Ch; T->lchild = NULL; T->rchild = NULL; T->flage = 1; T->parent = -1; Ht[i] = T; i ++; } M = i; print(Ht,M); return M; }

int main() { int N; HuffmanTiTree *HT; N =INinit(HT,print); printf("%d",N); }


0个回答