Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. /*
  2. * ConcordLL.c
  3. * COMP 2160 Programming Practices
  4. *
  5. * Assignment 2 sample solution
  6. */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include "Table.h"
  14.  
  15. //-------------------------------------------------------------------------------------
  16. // CONSTANTS and TYPES
  17. //-------------------------------------------------------------------------------------
  18.  
  19. typedef enum BOOL { false, true } Boolean;
  20.  
  21. typedef struct NODE Node;
  22. struct NODE{
  23. char *item;
  24. Node *next;
  25. };
  26.  
  27. struct TABLE{
  28. Node *head;
  29. // used to track where we are for the list traversal methods
  30. Node *traverseNode;
  31. // used to track testing values
  32. int numNodes;
  33. int numTraversals;
  34. };
  35.  
  36.  
  37. //-------------------------------------------------------------------------------------
  38. // VARIABLES
  39. //-------------------------------------------------------------------------------------
  40.  
  41.  
  42. //-------------------------------------------------------------------------------------
  43. // PROTOTYPES
  44. //-------------------------------------------------------------------------------------
  45.  
  46. //-------------------------------------------------------------------------------------
  47. // FUNCTIONS
  48. //-------------------------------------------------------------------------------------
  49.  
  50. // read from standard input, tokenize and insert words into a linked list
  51. // note that we must ensure that there are no duplicates in our list
  52. void loadFile(){
  53. #define LINE_SIZE 256
  54. char input[LINE_SIZE];
  55. char *token = NULL;
  56.  
  57. while ( fgets( input, LINE_SIZE, stdin ) ) {
  58. // parse the data into separate elements
  59. token = strtok( input, " \t\n" );
  60. while ( token ){
  61. if ( !search( token ) )
  62. insert( token );
  63.  
  64. token = strtok( NULL, " \t\n" );
  65. }// while ( token )
  66. }// while( fgets...
  67. }// loadFile
  68.  
  69. // print the contents of the linked list to standard output
  70. void printConcordance(){
  71. char *theWord = firstItem();
  72.  
  73. while ( theWord ) {
  74. printf( "%s\n", theWord );
  75. theWord = nextItem();
  76. }
  77. }// printConcordance
  78.  
  79. int main( int argc, char *argv[] ){
  80. loadFile();
  81. printConcordance();
  82. clearTable();
  83.  
  84. return EXIT_SUCCESS;
  85. }// main
  86.  
  87. //-------------------------------------------------------------------------------------
  88. // Linked List Implementation
  89. //-------------------------------------------------------------------------------------
  90.  
  91. void validateTable(Table *table){
  92. if ( table->numNodes == 0 )
  93. assert( table->head == NULL );
  94. else if ( table->numNodes == 1 )
  95. assert( table->head->next == NULL );
  96. else // numNodes > 1
  97. assert( table->head != NULL && table->head->next != NULL );
  98. // how far should we really go???
  99. }// validateTable
  100.  
  101. // add an element to the beginning of the linked list
  102. Boolean insert( char *new_string, Table *table ){
  103. Boolean rc = false;
  104. Node *newNode = NULL;
  105.  
  106. assert( new_string != NULL );
  107. if ( new_string ){
  108. newNode = (Node *)malloc( sizeof( Node ) );
  109. assert( newNode != NULL );
  110.  
  111. if ( newNode ){
  112. // note that we need to have space for the string as well!
  113. newNode->item = strdup(new_string);
  114. assert( newNode->item != NULL );
  115.  
  116. if ( newNode->item ){
  117. newNode->next = table->head;
  118. table->head = newNode;
  119. table->numNodes++;
  120.  
  121. rc = true;
  122.  
  123. // make sure the list is stable
  124. validateTable();
  125. } else {
  126. free( newNode );
  127. }// if ( newNode->item )
  128. }// if ( newNode )
  129. }// if( new_string )
  130.  
  131. return rc;
  132. }// insert
  133.  
  134. void delete(Table *table)
  135. {
  136. Node *curr = table->head;
  137.  
  138. if( table->head != NULL)
  139. {
  140. table->head = table->head->next;
  141. free( curr->item );
  142. free( curr );
  143.  
  144. table->numNodes--;
  145. }
  146. }
  147. // empty the list so that we clear all memory and can start a fresh list
  148. void clearTable(Table *table){
  149. Node *curr = table->head;
  150.  
  151. while ( table->head != NULL ){
  152. table->head = table->head->next;
  153. free( curr->item );
  154. free( curr );
  155. curr = table->head;
  156.  
  157. table->numNodes--;
  158. }// while
  159.  
  160. validateTable();
  161. }// clearTable
  162.  
  163.  
  164. // tells us whether or not the given string is in the list
  165. Boolean search( char *target, Table *table ){
  166. Boolean found = false;
  167. char *curr = firstItem();
  168. int search_count = 0; // how far in the list do we go?
  169.  
  170. assert( target != NULL );
  171. if ( target != NULL ) {
  172. while ( curr != NULL && !found ){
  173. if ( strcmp( target, curr ) == 0 ){
  174. found = true;
  175.  
  176. // make sure we're still in our list...
  177. assert( search_count <= table->numNodes );
  178. } else {
  179. curr = nextItem();
  180. search_count++;
  181. }// else
  182. }// while
  183. }// if( target != NULL )
  184.  
  185. // if it's not found then we should be at the end of the list
  186. assert( found || (search_count == table->numNodes) );
  187.  
  188. return found;
  189. }// search
  190.  
  191.  
  192. // starts a list traversal by getting the data at head
  193. char * firstItem(Table *table){
  194. char *the_item = NULL;
  195.  
  196. table->traverseNode = table->head;
  197. if ( table->traverseNode != NULL ) {
  198. the_item = table->traverseNode->item;
  199. assert( the_item != NULL );
  200.  
  201. table->numTraversals = 1;
  202. // make sure we're still in our list...
  203. assert( table->numTraversals <= table->numNodes );
  204. }// if
  205.  
  206. // this isn't the safest (caller can modify the string),
  207. // but we don't have to worry about extra memory mgmt...
  208. return the_item;
  209. }// firstItem
  210.  
  211.  
  212. // increment the traversal and get the item at the current traversal node
  213. char * nextItem(Table *table){
  214. char *the_item = NULL;
  215.  
  216. // try to advance the traversal first
  217. if ( table->traverseNode != NULL ) {
  218. table->traverseNode = table->traverseNode->next;
  219. table->numTraversals++;
  220. }
  221. // if we are still in the list, get the item
  222. if ( table->traverseNode != NULL ) {
  223. the_item = table->traverseNode->item;
  224. assert( the_item != NULL );
  225. // make sure we're still in our list...
  226. assert( table->numTraversals <= table->numNodes );
  227. }
  228.  
  229. // this isn't the safest (caller can modify the string),
  230. // but we don't have to worry about extra memory mgmt...
  231. return the_item;
  232. }// nextItem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement