Advertisement
Guest User

Untitled

a guest
Mar 26th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <stdbool.h>
  5.  
  6.  
  7.  
  8. struct Node{
  9. char *word;
  10. struct Node * next;
  11. };
  12.  
  13.  
  14. /**Node getLast()
  15. * Returns the last element of the Noe list
  16. */
  17. struct Node* getLast(struct Node *node){
  18. struct Node *temp = node;
  19. while(temp->next != NULL){
  20. temp = temp->next;
  21. }
  22. return temp;
  23. }
  24.  
  25. /**Node getItemAt(Struct Node *node, int i)
  26. * returns the item i items after the passed node.
  27. * If list ends before ith element, returns the end of the list.
  28. */
  29. struct Node* getItemAt(struct Node *node, int i){
  30. struct Node *temp = node;
  31. for(;i>0;i--){
  32. if(temp->next == NULL){
  33. break;
  34. }
  35. temp = temp->next;
  36. }
  37.  
  38. return temp;
  39. }
  40.  
  41. /**void insertNode(Struct Node *location, Struct Node *node)
  42. * Inserts node behind the location node.
  43. * if location is null, points location to node
  44. */
  45. void insertNode(struct Node *location, struct Node *node){
  46. if(location == NULL){
  47. location = node;
  48. }else{
  49. node->next = location->next;
  50. location->next = node;
  51. }
  52. }
  53.  
  54. /**void insertCorrectPosition(Struct Node *start, Struct Node *node)
  55. * Inserts a node into the list at the correct location based on
  56. * the alphabetical positioning of strings.
  57. */
  58. struct Node* insertCorrectPosition(struct Node *start, struct Node *node){
  59. struct Node *cur = start;
  60. struct Node *last;
  61. bool isFront = true;
  62. while(cur != NULL && strcmp(cur->word, node->word) <=0){
  63. last = cur;
  64. cur = cur->next;
  65. isFront = false;
  66. }
  67. //insertNode(last, node);
  68. node->next = cur;
  69. last->next = node;
  70.  
  71. if(isFront){
  72. return node;
  73. }
  74. return start;
  75. }
  76.  
  77.  
  78. /** printWords(struct Node *node)
  79. * prints the word array from each node from passed to end of the list.
  80. */
  81. void printWords(struct Node *node){
  82. struct Node *temp = node;
  83. int i = 0;
  84.  
  85. while(temp->next != NULL){
  86. printf("%d:\t%s\n", ++i, temp->word);
  87. temp = temp->next;
  88. }
  89. printf("%d:\t%s\n", ++i, temp->word);
  90. }
  91.  
  92. int getCount(){
  93. bool correct = false;
  94. int currentGuess = 50;
  95. int userChoice;
  96. int range = 50;
  97. while(!correct){
  98. printf("Current Guess: %d\n", currentGuess);
  99. printf("Is the current guess...\n");
  100. printf("0 - Higher\n");
  101. printf("1 - Lower\n");
  102. printf("2 - Correct\n");
  103. scanf("%d", &userChoice);
  104.  
  105. if(range > 2)
  106. range /= 2;
  107. else
  108. range = 1;
  109.  
  110. if(userChoice == 2)
  111. return currentGuess;
  112. else if(userChoice == 1){
  113. currentGuess -= range;
  114. }
  115. else if(userChoice == 0){
  116. currentGuess += range;
  117. }
  118.  
  119. }
  120. }
  121.  
  122. /** void freeList(struct Node *first)
  123. * goes through the list freeing all nodes and strings.
  124. */
  125. void freeList(struct Node *first){
  126. struct Node* temp;
  127. while(first != NULL){
  128. temp = first;
  129. first = first->next;
  130. free(temp->word);
  131. free(temp);
  132. }
  133. }
  134.  
  135. /** char* getWord()
  136. * requests for and returns a word entered from the console.
  137. * Safely allows for arbitrarily large input.
  138. */
  139. char* getWord(){
  140. unsigned int maxLength = 32;
  141. unsigned int curLength = 0;
  142.  
  143. char *word = malloc(maxLength);
  144. curLength = maxLength;
  145.  
  146. printf("Please enter your word. (NO WHITESPACE)\n");
  147. if(word != NULL){
  148. int c = EOF;
  149. unsigned int i = 0;
  150.  
  151. //flush input
  152. // fseek(stdin,0,SEEK_END);
  153.  
  154. while((c=getchar()) != '\n' && c != EOF){
  155. word[i++] = (char) c;
  156.  
  157. //If at max size, reallocate size.
  158. if(i == curLength){
  159. curLength = i+maxLength+1;
  160. word = realloc(word, curLength);
  161. }
  162. }
  163.  
  164. word[i] = '\0';
  165.  
  166. }else{
  167. printf("ERROR: word was null in getWord function.\n");
  168. }
  169.  
  170. return word;
  171. }
  172. int main()
  173. {
  174. int userNumber = getCount();
  175.  
  176. int c;
  177. while((c=getchar()) != '\n' && c != EOF){} //FLUSHSHHHSHSHS
  178.  
  179. printf("Your number: %d\n", userNumber );
  180. struct Node *start = (struct Node*)malloc(sizeof(struct Node));
  181. start->word = getWord();
  182. start->next = NULL;
  183. int i;
  184.  
  185. for(i=1; i<userNumber; i++){
  186. struct Node *node = (struct Node*)malloc(sizeof(struct Node));
  187. node->word = getWord();
  188. node->next = NULL;
  189.  
  190. //insertNode(getLast(start), node);
  191. start = insertCorrectPosition(start, node);
  192. }
  193.  
  194. printWords(start);
  195.  
  196. freeList(start);
  197.  
  198.  
  199. return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement