fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5.  
  6. //-------------------------------------------------------------------------------------
  7. // CONSTANTS and TYPES
  8. //-------------------------------------------------------------------------------------
  9.  
  10.  
  11.  
  12. struct NODE
  13. {
  14. char *data;
  15. struct NODE *link;
  16. };
  17.  
  18. typedef enum BOOL { false, true } Boolean;
  19. typedef struct NODE Node;
  20.  
  21.  
  22.  
  23.  
  24.  
  25. //-------------------------------------------------------------------------------------
  26. // VARIABLES
  27. //-------------------------------------------------------------------------------------
  28.  
  29. Node *head = NULL;
  30. int size = 0;
  31. int traversal = 0;
  32. //-------------------------------------------------------------------------------------
  33. // PROTOTYPES
  34. //-------------------------------------------------------------------------------------
  35.  
  36. // add an element to the beginning of the linked list
  37. Boolean insert( char *new_string );
  38. // empty the list so that we clear all memory and can start a fresh list
  39. void clearList();
  40. // tells us whether or not the given string is in the list
  41. Boolean search( char *target );
  42. // starts a list traversal by getting the data at top
  43. char * firstItem();
  44. // gets the data at the current traversal node and increments the traversal
  45. char * nextItem();
  46.  
  47. //-------------------------------------------------------------------------------------
  48. // FUNCTIONS
  49. //-------------------------------------------------------------------------------------
  50. void validateNode( Node *node )
  51. {
  52. assert( node != NULL );
  53. assert( node->data != NULL);
  54. }
  55.  
  56. Boolean insert( char *new_string )
  57. {
  58. Node *new;
  59. Boolean isInserted = false;
  60.  
  61. new = ( Node* )malloc( sizeof(Node) );
  62. assert ( new != NULL );
  63.  
  64. assert( new_string != NULL );
  65.  
  66. //if( head == NULL)
  67. // head = ( Node* )malloc( sizeof( Node ) );
  68. new->data = ( char* )malloc( strlen( new_string ) );
  69. new->data = new_string;
  70. //printf("%s\n", new->data);
  71. assert( strcmp( new->data, new_string) == 0 );
  72.  
  73. new->link = head;
  74. validateNode( new );
  75.  
  76. head = new;
  77. validateNode( head );
  78.  
  79. size++;
  80.  
  81. isInserted = true;
  82. //printf("%s\n", head->data);
  83.  
  84. return isInserted;
  85.  
  86. }//insert
  87.  
  88. void clearList()
  89. {
  90. Node *curr = ( Node* )malloc( sizeof( Node ) );
  91. assert ( curr != NULL );
  92.  
  93. Node *next = ( Node* )malloc( sizeof( Node ) );
  94. assert ( next != NULL );
  95.  
  96. curr = head;
  97. validateNode(curr);
  98.  
  99. while ( curr != NULL )
  100. {
  101. next = curr->link;
  102. free(curr);
  103. curr = next;
  104. }
  105. size = 0;
  106. }//clearList
  107.  
  108. Boolean search( char *target )
  109. {
  110.  
  111. Boolean isFound = false;
  112. Node *curr = ( Node* )malloc( sizeof( Node ) );
  113. assert ( curr != NULL );
  114.  
  115. curr = head;
  116. if( head != NULL )
  117. //printf("%d ", strcmp( curr->data, target ) );
  118. //validateNode( curr );
  119.  
  120. while ( curr != NULL && strcmp( curr->data, target ) != 0 )
  121. {
  122. curr = curr->link;
  123. }
  124.  
  125. if ( curr != NULL)
  126. {
  127. validateNode( curr );
  128. if ( strcmp( curr->data, target ) != 0 )
  129. {
  130. isFound = true;
  131. //printf("%s ", curr->data);
  132. }
  133. }
  134.  
  135. return isFound;
  136. }
  137.  
  138. char * firstItem()
  139. {
  140. char * temp = NULL;
  141. //printf("\n1\n");
  142. validateNode( head );
  143.  
  144. if ( head != NULL )
  145. temp = head->data;
  146. //printf("%s", temp);
  147.  
  148. return temp;
  149. }
  150.  
  151. char * nextItem()
  152. {
  153. char* temp = NULL;
  154. Node* curr = ( Node* )malloc( sizeof( Node ) );
  155. assert ( curr != NULL );
  156. curr = head;
  157. //int i = 0;
  158. if ( traversal < size )
  159. {
  160. for( int i = 0; i < traversal && curr != NULL; i++ )
  161. {
  162. curr = curr->link;
  163. }
  164.  
  165. temp = curr->data;
  166. traversal++;
  167. }
  168.  
  169. return temp;
  170. }
  171. // read from standard input, tokenize and insert words into a linked list
  172. // note that we must ensure that there are no duplicates in our list
  173. void loadFile()
  174. {
  175. #define LINE_SIZE 256
  176. char input[LINE_SIZE];
  177. char *token = NULL;
  178. //printf("3");
  179. while ( fgets( input, LINE_SIZE, stdin ) )
  180. {//printf("1");
  181. // parse the data into separate elements
  182. token = strtok( input, " \t\n" );
  183. while ( token )
  184. { //printf("2");
  185. if ( !search( token ) ){
  186. insert( token );
  187.  
  188. }
  189.  
  190. token = strtok( NULL, " \t\n" );
  191. }
  192. }
  193. printf("%s\n", head->data);
  194. }
  195.  
  196. // print the contents of the linked list to standard output
  197. void printConcordance()
  198. {
  199. char *theWord = firstItem();
  200.  
  201. while ( theWord )
  202. {
  203. // printf( "%s\n", theWord );
  204. theWord = nextItem();
  205. }
  206. }
  207.  
  208. void print()
  209. {
  210. printf("%s %u\n", head->data, strlen(head->data));
  211. Node *curr;
  212. curr = ( Node* )malloc( sizeof( Node ) );
  213. curr = head;
  214. validateNode( curr );
  215.  
  216.  
  217. while( curr )
  218. {
  219. // printf("%s\n", curr->data );
  220. curr = curr->link;
  221. }
  222. }
  223.  
  224. int main( int argc, char *argv[] )
  225. {
  226. //printf("%s\n", head->data);
  227. loadFile();
  228. //printf("%s\n", head->data);
  229. //printConcordance();
  230. print();
  231. clearList();
  232.  
  233. return EXIT_SUCCESS;
  234. }
Runtime error #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
Standard output is empty