#include<stdio.h> struct HufNode ...{ char ID; int weight; struct HufNode *lchild; struct HufNode *rchild; struct HufNode *parent; }; FILE *rfp,*wfp; /**//*从文件读入数据并将之初始化成升序链表*/ struct HufNode *ini_melodic_tab() ...{ struct HufNode *NonceNode =NULL; struct HufNode *Tab =NULL; struct HufNode *Creater =NULL; while(1) ...{ Creater=(struct HufNode *)malloc(sizeof(struct HufNode)); Creater->parent=NULL; /**//*在这里parent作为链表元素的前驱使用*/ Creater->lchild=NULL; Creater->rchild=NULL; while((Creater->ID=getc(rfp))==’ ’); /**//*寻找下一个节点数据的起始位置*/ if(Creater->ID==’#’)break;/**//* # 为文件的结束标志*/ fscanf(rfp,\"%d\",&Creater->weight); if(Tab == NULL)/**//*创造第一个节点*/ ...{ Tab=NonceNode=Creater; continue; } NonceNode=Tab;/**//*NonceNode复位*/ if(NonceNode->weight > Creater->weight)/**//*读入节点比链表首节点小的处理*/ ...{ Creater->parent=NonceNode; Tab=Creater; [Page] } else ...{ struct HufNode *next; next=NonceNode->parent; while(1) /**//*寻找读入节点的插入位置*/ ...{ if(next==NULL)break; if((NonceNode->weight <= Creater->weight) && (next->weight >= Creater->weight))break; NonceNode=next; next=next->parent; } NonceNode->parent=Creater; Creater->parent=next; } } return Tab; } /**//*构造霍夫曼树*/ struct HufNode *createhuf() ...{ struct HufNode *HufTree =NULL; struct HufNode *add =NULL; struct HufNode *Tab =NULL; struct HufNode *Paren =NULL; Tab=ini_melodic_tab(); add=Tab; Tab=Tab->parent; HufTree=Tab; Tab=Tab->parent; while(1)/**//*将链表中的元素逐个读入构造成霍夫曼树*/ |