⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 9 structures -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 4 页
字号:
data<BR>&nbsp;&nbsp;&nbsp;&nbsp;pt-&gt;next=HeadPt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
link into 
existing<BR>&nbsp;&nbsp;&nbsp;&nbsp;HeadPt=pt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return(1);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return(0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
out of memory<BR>};</CODE></P></DIR>
<ADDRESS>Listing 9-9: Code to add a node at the beginning of a linear linked 
list</ADDRESS>
<P><FONT face="Times New Roman,Times">In order to search the list we start at 
the <B>HeadPt</B>, and stop when the pointer becomes <B>null</B>. The routine 
<B>Search</B> will return a pointer to the node if found, and it will return a 
null-pointer if the data is not found.</FONT></P>
<DIR>
<P><CODE>nodeType *Search(unsigned short info){ nodeType 
*pt;<BR>&nbsp;&nbsp;pt=HeadPt;<BR>&nbsp;&nbsp;while(pt){<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(pt-&gt;data==info)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
(pt);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pt=pt-&gt;next;&nbsp;&nbsp;&nbsp;// link to 
next<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return(pt);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
not found<BR>};</CODE></P></DIR>
<ADDRESS>Listing 9-10: Code to find a node in a linear linked list</ADDRESS>
<P><FONT face="Times New Roman,Times">To count the number of elements, we again 
start at the <B>HeadPt</B>, and stop when the pointer becomes <B>null</B>. The 
routine <B>Count</B> will return the number of elements in the list.</FONT></P>
<DIR>
<P><CODE>unsigned short Count(void){ nodeType *pt;<BR>&nbsp;&nbsp;unsigned short 
cnt;<BR>&nbsp;&nbsp;cnt=0;<BR>&nbsp;&nbsp;pt=HeadPt;<BR>&nbsp;&nbsp;while(pt){<BR>&nbsp;&nbsp;&nbsp;&nbsp;cnt++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pt=pt-&gt;next;&nbsp;&nbsp;&nbsp;// 
link to next<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return(cnt);<BR>};</CODE></P></DIR>
<ADDRESS>Listing 9-11: Code to count the number of nodes in a linear linked 
list</ADDRESS>
<P><FONT face="Times New Roman,Times">If we wanted to maintain a sorted list, 
then we can insert new data at the proper place, in between data elements 
smaller and larger than the one we are inserting. In the following figure we are 
inserting the element 250 in between elements 200 and 300. </FONT></P>
<P><IMG height=257 src="Chapter 9 Structures -- Valvano.files/link2.gif" 
width=430></P>
<ADDRESS>Figure 9-4: Inserting a node in sorted order</ADDRESS>
<P><FONT face="Times New Roman,Times">In case 1, the list is initially empty, 
and this new element is the first and only one. In case 2, the new element is 
inserted at the front of the list because it has the smallest data value. Case 3 
is the general case depicted in the above figure. In this situation, the new 
element is placed in between <B>firstPt</B> and <B>secondPt</B>. In case 4, the 
new element is placed at the end of the list because it has the largest data 
value.</FONT></P>
<DIR>
<P><CODE>#include &lt;STDLIB.H&gt;;<BR>int InsertData(unsigned short info){ 
<BR>nodeType 
*firstPt,*secondPt,*newPt;<BR>&nbsp;&nbsp;newPt=malloc(sizeof(nodeType));&nbsp;&nbsp;// 
create a new 
entry<BR>&nbsp;&nbsp;if(newPt){<BR>&nbsp;&nbsp;&nbsp;&nbsp;newPt-&gt;data=info;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
store data<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(HeadPt==0){&nbsp;&nbsp;&nbsp;// case 
1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newPt-&gt;next=HeadPt;&nbsp;&nbsp;&nbsp;// 
only 
element<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeadPt=newPt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(info&lt;=HeadPt-&gt;data){&nbsp;// 
case 
2<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newPt-&gt;next=HeadPt;&nbsp;&nbsp;&nbsp;// 
first element in 
list<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeadPt=newPt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;firstPt=HeadPt;&nbsp;&nbsp;&nbsp;// 
search from 
beginning<BR>&nbsp;&nbsp;&nbsp;&nbsp;secondPt=HeadPt-&gt;next;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;while(secondPt){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(info&lt;=secondPt-&gt;data){&nbsp;// 
case 
3<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newPt-&gt;next=secondPt;&nbsp;&nbsp;&nbsp;// 
insert element 
here<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstPt-&gt;next=newPt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstPt=secondPt;&nbsp;&nbsp;&nbsp;// 
search 
next<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secondPt=secondPt-&gt;next;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;newPt-&gt;next=secondPt;&nbsp;&nbsp;&nbsp;// 
case 4, insert at 
end<BR>&nbsp;&nbsp;&nbsp;&nbsp;firstPt-&gt;next=newPt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;return(1);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return(0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
out of memory<BR>};</CODE></P></DIR>
<ADDRESS>Listing 9-12: Code to insert a node in a sorted linear linked 
list</ADDRESS>
<P><FONT face="Times New Roman,Times">The following function will search and 
remove a node from the linked list. Case 1 is the situation in which an attempt 
is made to remove an element from an empty list. The return value of zero 
signifies the attempt failed. In case 2, the first element is removed. In this 
situation the <B>HeadPt</B> must be updated to now point to the second element. 
It is possible the second element does not exist, because the list orginally had 
only one element. This is OK because in this situation <B>HeadPt</B> will be set 
to null signifying the list is now empty. Case 3 is the general situation in 
which the element at <B>secondPt</B> is removed. The element before, 
<B>firstPt</B>, is now linked to the element after. Case 4 is the situation 
where the element that was requested to be removed did not exist. In this case, 
the return value of zero signifies the request failed.</FONT></P>
<DIR>
<P><CODE>#include &lt;STDLIB.H&gt;;<BR>int Remove(unsigned short info){ 
<BR>nodeType *firstPt,*secondPt;<BR>&nbsp;&nbsp;if(HeadPt==0)&nbsp;&nbsp;// case 
1<BR>&nbsp;&nbsp;&nbsp;&nbsp;return(0);&nbsp;&nbsp;&nbsp;// empty 
list<BR>&nbsp;&nbsp;firstPt=HeadPt;<BR>&nbsp;&nbsp;secondPt=HeadPt-&gt;next;<BR>&nbsp;&nbsp;if(info==HeadPt-&gt;data){&nbsp;&nbsp;// 
case 2<BR>&nbsp;&nbsp;&nbsp;&nbsp;HeadPt=secondPt;&nbsp;// remove first element 
in list<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(firstPt);&nbsp;&nbsp;&nbsp;// return 
unneeded memory to 
heap<BR>&nbsp;&nbsp;&nbsp;&nbsp;return(1);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;while(secondPt){<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(secondPt-&gt;data==info){&nbsp;&nbsp;// 
case 
3<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstPt-&gt;next=secondPt-&gt;next;&nbsp;// 
remove this 
one<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(secondPt);&nbsp;&nbsp;&nbsp;// 
return unneeded memory to 
heap<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;firstPt=secondPt;&nbsp;&nbsp;&nbsp;// 
search 
next<BR>&nbsp;&nbsp;&nbsp;&nbsp;secondPt=secondPt-&gt;next;&nbsp;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return(0);&nbsp;&nbsp;&nbsp;&nbsp;// 
case 4, not found<BR>};</CODE></P></DIR>
<ADDRESS>Listing 9-13: Code to remove a node from a sorted linear linked 
list</ADDRESS>
<P><B><I><FONT face=Helvetica,Arial><A name=HUFFMAN></A>Example of a Huffman 
Code</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">When information is stored or transmitted 
there is a fixed cost for each bit. Data compression and decompression provide a 
means to reduce this cost without loss of information. If the sending computer 
compresses a message before transmission and the receiving computer decompresses 
it at the destination, the effective bandwidth is increased. In particular, this 
example introduces a way to process bit streams using Huffman encoding and 
decoding. A typical application is illustrated by the following flow 
diagram.</FONT></P>
<P><IMG height=151 src="Chapter 9 Structures -- Valvano.files/huff1.gif" 
width=408></P>
<ADDRESS>Figure 9-5: Data flow diagram showing a typical application of Huffman 
encoding and decoding</ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">The Huffman code is similar to the Morse 
code in that they both use short patterns for letters that occur more 
frequently. In regular ASCII, all characters are encoded with the same number of 
bits (8). Conversely, with the Huffman code, we assign codes where the number of 
bits to encode each letter varies. In this way, we can use short codes for 
letter like "e s i a t o n" (that have a higher probability of occurrence) and 
long codes for seldom used consonants like "j q w z" (that have a lower 
probability of occurrence). To illustrate the encode-decode operations, consider 
the following Huffman code for the letters M,I,P,S. S is encoded as "0", I as 
"10", P as "110" and M as "111". We can store a Huffman code as a binary 
tree.</FONT></P>
<P><IMG height=120 src="Chapter 9 Structures -- Valvano.files/huff2.gif" 
width=130></P>
<ADDRESS>Figure 9-6: Huffman code for the letters S I P M</ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">If "MISSISSIPPI" were to be stored in 
ASCII, it would require 10 bytes or 80 bits. With this simple Huffman code, the 
same string can be stored in 21 bits. </FONT></P>
<P><IMG height=60 src="Chapter 9 Structures -- Valvano.files/huff3.gif" 
width=191></P>
<ADDRESS>Figure 9-7: Huffman encoding for MISSISSIPPI</ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">Of course, this Huffman code can only 
handle 4 letters, while the ASCII code has 128 possibilities, so it is not fair 
to claim we have an 80 to 21 bit savings. Nevertheless, for information that has 
a wide range of individual probabilities of occurrence, a Huffman code will be 
efficient. In the following implementation the functions <B>BitPut() </B>and 
<B>BitGet()</B> are called to save and recover binary data. The implementations 
of these two functions were presented back in </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap2/chap2.htm#BITFIFO">Chapter 
2</A><FONT face="Times New Roman,Times">.</FONT></P>
<P>&nbsp;</P>
<DIR>
<P><CODE>const&nbsp;struct&nbsp;Node{<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;Letter0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;ASCII&nbsp;code&nbsp;if&nbsp;binary&nbsp;0<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;Letter1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;ASCII&nbsp;code&nbsp;if&nbsp;binary&nbsp;1<BR>//&nbsp;Letter1&nbsp;is&nbsp;NULL(0)&nbsp;if&nbsp;Link&nbsp;is&nbsp;pointer&nbsp;to&nbsp;another&nbsp;node<BR>&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;Node&nbsp;*Link;};&nbsp;&nbsp;//&nbsp;binary&nbsp;tree&nbsp;pointer&nbsp;<BR>typedef&nbsp;const&nbsp;struct&nbsp;Node&nbsp;NodeType;<BR>typedef&nbsp;NodeType&nbsp;*&nbsp;NodePtr;<BR>//&nbsp;Huffman&nbsp;tree<BR>NodeType&nbsp;twentysixth=&nbsp;{'Q','Z',0};<BR>NodeType&nbsp;twentyfifth=&nbsp;{'X',0,&amp;twentysixth};&nbsp;<BR>NodeType&nbsp;twentyfourth={'G',0,&amp;twentyfifth};&nbsp;<BR>NodeType&nbsp;twentythird=&nbsp;{'J',0,&amp;twentyfourth};&nbsp;<BR>NodeType&nbsp;twentysecond={'W',0,&amp;twentythird};&nbsp;<BR>NodeType&nbsp;twentyfirst=&nbsp;{'V',0,&amp;twentysecond};&nbsp;<BR>NodeType&nbsp;twentyth=&nbsp;&nbsp;&nbsp;&nbsp;{'H',0,&amp;twentyfirst};&nbsp;<BR>NodeType&nbsp;ninteenth=&nbsp;&nbsp;&nbsp;{'F',0,&amp;twentyth};&nbsp;<BR>NodeType&nbsp;eighteenth=&nbsp;&nbsp;{'B',0,&amp;ninteenth};&nbsp;<BR>NodeType&nbsp;seventeenth=&nbsp;{'K',0,&amp;eighteenth};&nbsp;<BR>NodeType&nbsp;sixteenth=&nbsp;&nbsp;&nbsp;{'D',0,&amp;seventeenth};&nbsp;<BR>NodeType&nbsp;fifteenth=&nbsp;&nbsp;&nbsp;{'P',0,&amp;sixteenth};&nbsp;<BR>NodeType&nbsp;fourteenth=&nbsp;&nbsp;{'M',0,&amp;fifteenth};&nbsp;<BR>NodeType&nbsp;thirteenth=&nbsp;&nbsp;{'Y',0,&amp;fourteenth};&nbsp;<BR>NodeType&nbsp;twelfth=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'L',0,&amp;thirteenth};&nbsp;<BR>NodeType&nbsp;eleventh=&nbsp;&nbsp;&nbsp;&nbsp;{'U',0,&amp;twelfth};&nbsp;<BR>NodeType&nbsp;tenth=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'R',0,&amp;eleventh};&nbsp;<BR>NodeType&nbsp;ninth=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'C',0,&amp;tenth};&nbsp;<BR>NodeType&nbsp;eighth=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'O',0,&amp;ninth};&nbsp;<BR>NodeType&nbsp;seventh=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'&nbsp;',0,&amp;eighth};&nbsp;<BR>NodeType&nbsp;sixth=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'N',0,&amp;seventh};&nbsp;<BR>NodeType&nbsp;fifth=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'I',0,&amp;sixth};&nbsp;<BR>NodeType&nbsp;fourth=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'S',0,&amp;fifth};&nbsp;<BR>NodeType&nbsp;third=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'T',0,&amp;fourth};&nbsp;<BR>NodeType&nbsp;second=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'A',0,&amp;third};&nbsp;<BR>NodeType&nbsp;root=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{'E',0,&amp;second};<BR>//********encode***************&nbsp;<BR>//&nbsp;convert&nbsp;ASCII&nbsp;string&nbsp;to&nbsp;Huffman&nbsp;bit&nbsp;sequence<BR>//&nbsp;returns&nbsp;bit&nbsp;count&nbsp;if&nbsp;OK<BR>//&nbsp;returns&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;BitFifo&nbsp;Full<BR>//&nbsp;returns&nbsp;0xFFFF&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;illegal&nbsp;character<BR>int&nbsp;encode(char&nbsp;*sPt){&nbsp;&nbsp;//&nbsp;null-terminated&nbsp;ASCII&nbsp;string<BR>&nbsp;int&nbsp;NotFound;&nbsp;char&nbsp;data;<BR>&nbsp;int&nbsp;BitCount=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;number&nbsp;of&nbsp;bits&nbsp;created<BR>&nbsp;NodePtr&nbsp;hpt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;pointer&nbsp;into&nbsp;Huffman&nbsp;tree<BR>&nbsp;while&nbsp;(data=(*sPt)){<BR>&nbsp;&nbsp;&nbsp;sPt++;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;next&nbsp;character<BR>&nbsp;&nbsp;&nbsp;hpt=&amp;root;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;start&nbsp;search&nbsp;at&nbsp;root<BR>&nbsp;&nbsp;&nbsp;NotFound=1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;changes&nbsp;to&nbsp;0&nbsp;when&nbsp;found<BR>&nbsp;&nbsp;&nbsp;while(NotFound){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;((hpt-&gt;Letter0)==data){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!BitPut(0))&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(0);&nbsp;&nbsp;&nbsp;//&nbsp;data&nbsp;structure&nbsp;full<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BitCount++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotFound=0;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!BitPut(1))&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(0);&nbsp;&nbsp;&nbsp;//&nbsp;data&nbsp;structure&nbsp;full<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BitCount++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;((hpt-&gt;Letter1)==data)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NotFound=0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;doesn't&nbsp;match&nbsp;either&nbsp;Letter0&nbsp;or&nbsp;Letter1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hpt=hpt-&gt;Link;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(hpt==0)&nbsp;return&nbsp;(0xFFFF);&nbsp;//&nbsp;illegal,&nbsp;end&nbsp;of&nbsp;tree?<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return&nbsp;BitCount;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;}<BR>//********decode***************&nbsp;<BR>//&nbsp;convert&nbsp;Huffman&nbsp;bit&nbsp;sequence&nbsp;to&nbsp;ASCII<BR>//&nbsp;will&nbsp;remove&nbsp;from&nbsp;the&nbsp;BitFifo&nbsp;until&nbsp;it&nbsp;is&nbsp;empty<BR>//&nbsp;returns&nbsp;character&nbsp;count<BR>int&nbsp;decode(char&nbsp;*sPt){&nbsp;&nbsp;//&nbsp;null-terminated&nbsp;ASCII&nbsp;string<BR>&nbsp;int&nbsp;CharCount=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;number&nbsp;of&nbsp;ASCII&nbsp;characters&nbsp;created<BR>&nbsp;int&nbsp;NotFound;&nbsp;unsigned&nbsp;int&nbsp;data;<BR>&nbsp;NodePtr&nbsp;hpt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;pointer&nbsp;into&nbsp;Huffman&nbsp;tree<BR>&nbsp;hpt=&amp;root;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;start&nbsp;search&nbsp;at&nbsp;root<BR>&nbsp;while&nbsp;(BitGet(&amp;data)){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;if&nbsp;(data==0){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(*sPt)=&nbsp;(hpt-&gt;Letter0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sPt++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CharCount++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hpt=&amp;root;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;start&nbsp;over&nbsp;and&nbsp;search&nbsp;at&nbsp;root<BR>&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;//data&nbsp;is&nbsp;1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((hpt-&gt;Link)==0){&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(*sPt)=&nbsp;(hpt-&gt;Letter1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sPt++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CharCount++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hpt=&amp;root;}&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;start&nbsp;over&nbsp;and&nbsp;search&nbsp;at&nbsp;root<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;doesn't&nbsp;match&nbsp;either&nbsp;Letter0&nbsp;or&nbsp;Letter1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hpt=hpt-&gt;Link;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;(*sPt)=0;&nbsp;&nbsp;//&nbsp;null&nbsp;terminated<BR>&nbsp;&nbsp;&nbsp;return&nbsp;CharCount;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 9-14: A Huffman code 
implementation</FONT></ADDRESS>
<P>&nbsp;</P>
<DIR>
<DIR>
<P>&nbsp;</P></DIR></DIR>
<P><FONT face="Times New Roman,Times">Go to <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm">Chapter 10 on 
Functions</A> Return to <A 
href="http://www.ece.utexas.edu/~valvano/embed/toc1.htm">Table of Contents</A> 
</FONT></P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -