Huffman Compression 433
// push a tree node into the list264
TreeNode * tn = T reeNod e_crea te ( value , 0) ;265
ListNode * ln = L istNod e_crea te ( tn) ;266
head = List_i nsert ( head , ln , STACK );267
}268
e l s e269
{270
i f ( head == NULL )271
{272
printf (" ERROR , head should not be NULL n") ;273
}274
i f (( head -> next ) == NULL )275
{276
// the tree has been comp l etely built277
done = 1;278
}279
e l s e280
{281
head = Merg e ListNo d e ( head , DEC O DEMODE );282
}283
}284
}285
// un n ecessary to read the rema i ning unused bits286
TreeNode * root = head -> tnptr ;287
// the linked list is no longer needed288
free ( head );289
return root ;290
}291
int decode ( char * infile , char * outfile )292
{293
FILE * infptr = fopen ( infile , "r ") ;294
i f ( infptr == NULL )295
{296
return 0;297
}298
TreeNode * root = r eadHeader ( infptr ) ;299
Tree_pr i nt ( root , 0);300
// read the number of c h aracters301
unsigned int numChar = 0;302
fread (& numChar , s i z e o f ( unsigned in t ), 1 , infptr );303
printf (" numChar = % dn " , numChar ) ;304
// read ’ n ’305
unsigned char newline ;306
fread (& newline , s i z e o f ( unsigned char ) , 1, infptr ) ;307
i f ( newline != ’n ’)308
{309
printf (" ERROR ! n") ;310
}311
unsigned char whichbit = 0;312
unsigned char onebit = 0;313
unsigned char curbyte = 0;314