Table of Contents for
Intermediate C Programming

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Intermediate C Programming by Yung-Hsiang Lu Published by CRC Press, 2015
  1. Front Cover
  2. Contents (1/2)
  3. Contents (2/2)
  4. List of Figures (1/2)
  5. List of Figures (2/2)
  6. List of Tables
  7. Foreword
  8. Preface
  9. Author, Reviewers, and Artist
  10. Rules in Software Development
  11. Source Code
  12. I. Computer Storage: Memory and File
  13. 1. Program Execution (1/2)
  14. 1. Program Execution (2/2)
  15. 2. Stack Memory (1/5)
  16. 2. Stack Memory (2/5)
  17. 2. Stack Memory (3/5)
  18. 2. Stack Memory (4/5)
  19. 2. Stack Memory (5/5)
  20. 3. Prevent, Detect, and Remove Bugs (1/2)
  21. 3. Prevent, Detect, and Remove Bugs (2/2)
  22. 4. Pointers (1/6)
  23. 4. Pointers (2/6)
  24. 4. Pointers (3/6)
  25. 4. Pointers (4/6)
  26. 4. Pointers (5/6)
  27. 4. Pointers (6/6)
  28. 5. Writing and Testing Programs (1/4)
  29. 5. Writing and Testing Programs (2/4)
  30. 5. Writing and Testing Programs (3/4)
  31. 5. Writing and Testing Programs (4/4)
  32. 6. Strings (1/3)
  33. 6. Strings (2/3)
  34. 6. Strings (3/3)
  35. 7. Programming Problems and Debugging (1/4)
  36. 7. Programming Problems and Debugging (2/4)
  37. 7. Programming Problems and Debugging (3/4)
  38. 7. Programming Problems and Debugging (4/4)
  39. 8. Heap Memory (1/3)
  40. 8. Heap Memory (2/3)
  41. 8. Heap Memory (3/3)
  42. 9. Programming Problems Using Heap Memory (1/4)
  43. 9. Programming Problems Using Heap Memory (2/4)
  44. 9. Programming Problems Using Heap Memory (3/4)
  45. 9. Programming Problems Using Heap Memory (4/4)
  46. 10. Reading and Writing Files (1/3)
  47. 10. Reading and Writing Files (2/3)
  48. 10. Reading and Writing Files (3/3)
  49. 11. Programming Problems Using File (1/2)
  50. 11. Programming Problems Using File (2/2)
  51. II. Recursion
  52. 12. Recursion (1/4)
  53. 12. Recursion (2/4)
  54. 12. Recursion (3/4)
  55. 12. Recursion (4/4)
  56. 13. Recursive C Functions (1/4)
  57. 13. Recursive C Functions (2/4)
  58. 13. Recursive C Functions (3/4)
  59. 13. Recursive C Functions (4/4)
  60. 14. Integer Partition (1/5)
  61. 14. Integer Partition (2/5)
  62. 14. Integer Partition (3/5)
  63. 14. Integer Partition (4/5)
  64. 14. Integer Partition (5/5)
  65. 15. Programming Problems Using Recursion (1/5)
  66. 15. Programming Problems Using Recursion (2/5)
  67. 15. Programming Problems Using Recursion (3/5)
  68. 15. Programming Problems Using Recursion (4/5)
  69. 15. Programming Problems Using Recursion (5/5)
  70. III. Structure
  71. 16. Programmer-Defined Data Types (1/6)
  72. 16. Programmer-Defined Data Types (2/6)
  73. 16. Programmer-Defined Data Types (3/6)
  74. 16. Programmer-Defined Data Types (4/6)
  75. 16. Programmer-Defined Data Types (5/6)
  76. 16. Programmer-Defined Data Types (6/6)
  77. 17. Programming Problems Using Structure (1/4)
  78. 17. Programming Problems Using Structure (2/4)
  79. 17. Programming Problems Using Structure (3/4)
  80. 17. Programming Problems Using Structure (4/4)
  81. 18. Linked Lists (1/3)
  82. 18. Linked Lists (2/3)
  83. 18. Linked Lists (3/3)
  84. 19. Programming Problems Using Linked List (1/2)
  85. 19. Programming Problems Using Linked List (2/2)
  86. 20. Binary Search Trees (1/4)
  87. 20. Binary Search Trees (2/4)
  88. 20. Binary Search Trees (3/4)
  89. 20. Binary Search Trees (4/4)
  90. 21. Parallel Programming Using Threads (1/5)
  91. 21. Parallel Programming Using Threads (2/5)
  92. 21. Parallel Programming Using Threads (3/5)
  93. 21. Parallel Programming Using Threads (4/5)
  94. 21. Parallel Programming Using Threads (5/5)
  95. IV. Applications
  96. 22. Finding the Exit of a Maze (1/5)
  97. 22. Finding the Exit of a Maze (2/5)
  98. 22. Finding the Exit of a Maze (3/5)
  99. 22. Finding the Exit of a Maze (4/5)
  100. 22. Finding the Exit of a Maze (5/5)
  101. 23. Image Processing (1/3)
  102. 23. Image Processing (2/3)
  103. 23. Image Processing (3/3)
  104. 24. Huffman Compression (1/10)
  105. 24. Huffman Compression (2/10)
  106. 24. Huffman Compression (3/10)
  107. 24. Huffman Compression (4/10)
  108. 24. Huffman Compression (5/10)
  109. 24. Huffman Compression (6/10)
  110. 24. Huffman Compression (7/10)
  111. 24. Huffman Compression (8/10)
  112. 24. Huffman Compression (9/10)
  113. 24. Huffman Compression (10/10)
  114. A. Linux
  115. B. Version Control
  116. C. Integrated Development Environments (IDE) (1/3)
  117. C. Integrated Development Environments (IDE) (2/3)
  118. C. Integrated Development Environments (IDE) (3/3)
Huffman Compression 405
(a) (b)
FIGURE 24.6: Continue the procedure shortening the linked list.
// encode the text in the input file4
// save the result in the output file5
// mode : TEXT or BINARY6
// return 0 if cannot read from file or write to file7
// return 1 if success8
int encode ( char * infile , char * outfile , i n t mode ) ;9
#e ndif10
// tree . c1
#in clude " tree .h "2
#in clude < stdio .h >3
#in clude < stdlib .h >4
TreeNode * Tre eNode_ creat e ( char val , int freq )5
{6
TreeNode * tn = malloc ( s i z e o f ( TreeNode )) ;7
tn -> left = NULL ;8
tn -> right = NULL ;9
tn -> value = val ;10
tn -> freq = freq ;11
return tn;12
}13
TreeNode * Tree_m erge ( TreeNode * tn1 , TreeNode * tn2 )14
{15
TreeNode * tn = malloc ( s i z e o f ( TreeNode )) ;16
tn -> left = tn1 ;17
tn -> right = tn2 ;18
tn -> value = 0; // do not care19
tn -> freq = tn1 -> freq + tn2 -> freq ;20
406 Intermediate C Programming
FIGURE 24.7: Now the linked list has only one node. The tree has been built, and it is
in the only remaining list node.
return tn;21
}22
// post - order23
void Tree_pri n t ( T r eeNode * tn , i nt level )24
{25
i f ( tn == NULL )26
{27
return ;28
}29
TreeNode * lc = tn -> left ; // left child30
TreeNode * rc = tn -> right ; // right child31
Tree_pr i nt ( lc , level + 1) ;32
Tree_pr i nt ( rc , level + 1) ;33
int depth ;34
for ( depth = 0; depth < level ; depth ++)35
{36
printf (" ") ;37
}38
printf (" freq = %d " , tn -> freq );39
i f (( lc == NULL ) && ( rc == NULL ) )40
{41
// a leaf node42
printf (" value = %d , %c " , tn -> value , tn -> value ) ;43
}44
printf (" \n" );45
}46
Huffman Compression 407
// list . c1
#in clude " list .h "2
#in clude " freq .h "3
#in clude < stdlib .h >4
ListNode * Lis tNode_ creat e ( TreeNode * tn)5
{6
ListNode * ln = malloc ( s i z e o f ( ListNode )) ;7
ln -> next = NULL ;8
ln -> tnptr = tn ;9
return ln;10
}11
// head may be NULL12
// ln must not be NULL13
// ln -> next must be NULL14
ListNode * List _ insert ( List N ode * head , L i stNode * ln )15
{16
i f ( head == NULL )17
{18
return ln;19
}20
i f ( ln == NULL )21
{22
printf (" ERROR ! ln is NULL \n ");23
}24
i f (( ln -> next ) != NULL )25
{26
printf (" ERROR ! ln -> next is not NULL \n ");27
}28
int freq1 = ( head -> tnptr ) -> freq ;29
int freq2 = ( ln -> tnptr ) -> freq ;30
i f ( freq1 > freq2 )31
{32
// ln should be the first node33
ln -> next = head ;34
return ln;35
}36
// ln should be after head37
head -> next = List_i nsert ( head -> next , ln );38
return head ;39
}40
// fr e quencies must be sorted41
ListNode * List_b uild ( CharFreq * fre q uencies )42
{43
// find the first index whose freque n cy is nonzero44
int ind = 0;45
while ( freq uencies [ ind ]. freq == 0)46
{47
ind ++;48
}49
i f ( ind == NUMLETTER )50
{51
408 Intermediate C Programming
// no letter appears52
return NULL ;53
}54
// create a linked list , each node points to a tree node55
ListNode * head = NULL ;56
while ( ind < NUMLETTER )57
{58
TreeNode * tn =59
Tree Node_c reate ( freque ncies [ ind ]. value ,60
freque ncies [ ind ]. freq );61
ListNode * ln = L istNod e_crea te ( tn) ;62
head = List_i nsert ( head , ln );63
ind ++;64
}65
return head ;66
}67
void List_pri n t ( L i stNode * head )68
{69
i f ( head == NULL )70
{71
return ;72
}73
Tree_pr i nt ( head -> tnptr , 0) ;74
List_pr i nt ( head -> next );75
}76
The following function implements the concept depicted earlier.
// encode . c1
#in clude " encode .h "2
#in clude " constant .h "3
#in clude " freq . h"4
#in clude " list . h"5
#in clude < stdio .h >6
#in clude < strings .h >7
#in clude < stdlib .h >8
int encode ( char * infile , char * outfile , i n t mode )9
{10
CharFreq frequen cies [ NUMLET T ER ];11
// set the array elements to zero12
bzero ( frequencies , s i z e o f ( CharFreq ) * NUM L ETTER ) ;13
i f ( c o untFre quency ( infile , fr e quencies ) == 0)14
{15
return 0;16
}17
// p r intFre quency ( frequ encies );18
sortF requenc y ( frequ e ncies );19
// p r intFre quency ( frequ encies );20
ListNode * head = L ist_build ( freque n cies ) ;21
i f ( head == NULL )22
{23
// the article is empty24
Huffman Compression 409
return 0;25
}26
// merge the top two list nodes until only one list node27
while (( head -> next ) != NULL )28
{29
List_pr i nt ( head ); printf (" - --- --- --- -\ n") ;30
ListNode * second = head -> next ;31
// second must not be NULL , otherwise , will not enter32
ListNode * third = second -> next ;33
// third may be NULL34
// get the tree nodes of the first two list nodes35
TreeNode * tn1 = head -> tnptr ;36
TreeNode * tn2 = second -> tnptr ;37
// remove the first two nodes38
free ( head );39
free ( second );40
head = third ;41
TreeNode * mrg = Tree_me r ge ( tn1 , tn2 ) ;42
ListNode * ln = L istNod e_crea te ( mrg );43
head = List_i nsert ( head , ln );44
}45
List_pr i nt ( head );46
return 1;47
}48
Line 46 prints the linked list, and it should have only one list node. Otherwise, the
function should continue inside while. Calling Tree print prints the tree nodes using a
post-order traversal. Below is the output. Note that it matches Fig. 24.7.
freq = 73 value = 104, ’h’
freq = 46 value = 100, ’d’
freq = 23 value = 112, ’p’
freq = 7 value = 84, ’T’
freq = 1 value = 69, ’E’
freq = 2 value = 78, ’N’
freq = 3
freq = 7 value = 71, ’G’
freq = 10
freq = 17
freq = 19 value = 103, ’g’
freq = 36
freq = 59
freq = 105
freq = 178
Fig. 24.8 shows the list of tree nodes as displayed in the debugging program DDD. DDD
can help you visualize how the list nodes and the tree nodes change as the program makes
progress. Fig. 24.9 shows the tree in DDD after the tree has been completely built. It is the
same tree as Fig. 24.7. The visualization function in DDD can help you see the nodes of the
tree.