Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct {
  6. char *word;
  7. int count;
  8. void *next;
  9. } Node;
  10.  
  11. void push(char *word, Node **head) {
  12. if (*head != NULL) {
  13. Node *current = (Node*) *head;
  14. while (current != NULL && strcmp(current->word, word) != 0) {
  15. current = (Node*) current->next;
  16. }
  17.  
  18. if (current != NULL) {
  19. current->count++;
  20. return;
  21. }
  22. }
  23.  
  24. Node *node = (Node*) malloc(sizeof(Node));
  25. node->count = 1;
  26. node->word = malloc(sizeof(char) * strlen(word) + 1);
  27. strcpy(node->word, word);
  28. node->next = NULL;
  29.  
  30. if (*head = NULL) {
  31. (*head) = node;
  32. } else {
  33. Node *place = (Node*) *head;
  34.  
  35. while (place->next != NULL && strcmp(((Node*) place->next)->word, word) < 0) {
  36. place = place->next;
  37. }
  38.  
  39. if (place = *head && strcmp(place->word, word) > 0) {
  40. node->next = *head;
  41. *head = node;
  42. } else {
  43. Node *temp = place->next;
  44. node->next = temp;
  45. place->next = node;
  46. }
  47. }
  48. }
  49.  
  50. int main () {
  51. FILE *fin = fopen("input.txt", "r");
  52. FILE *fout = fopen("output.txt", "w");
  53.  
  54. Node *head = NULL;
  55.  
  56. char word[100];
  57. while (fscanf(fin, "%s", word) != EOF) {
  58. push(word, &head);
  59. }
  60.  
  61. Node *current = head;
  62. while (current != NULL) {
  63. fprintf(fout, "%s: %d\n", current->word, current->count);
  64. current = current->next;
  65. }
  66.  
  67. fclose(fin);
  68. fclose(fout);
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement