Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct {
- char *word;
- int count;
- void *next;
- } Node;
- void push(char *word, Node **head) {
- if (*head != NULL) {
- Node *current = (Node*) *head;
- while (current != NULL && strcmp(current->word, word) != 0) {
- current = (Node*) current->next;
- }
- if (current != NULL) {
- current->count++;
- return;
- }
- }
- Node *node = (Node*) malloc(sizeof(Node));
- node->count = 1;
- node->word = malloc(sizeof(char) * strlen(word) + 1);
- strcpy(node->word, word);
- node->next = NULL;
- if (*head = NULL) {
- (*head) = node;
- } else {
- Node *place = (Node*) *head;
- while (place->next != NULL && strcmp(((Node*) place->next)->word, word) < 0) {
- place = place->next;
- }
- if (place = *head && strcmp(place->word, word) > 0) {
- node->next = *head;
- *head = node;
- } else {
- Node *temp = place->next;
- node->next = temp;
- place->next = node;
- }
- }
- }
- int main () {
- FILE *fin = fopen("input.txt", "r");
- FILE *fout = fopen("output.txt", "w");
- Node *head = NULL;
- char word[100];
- while (fscanf(fin, "%s", word) != EOF) {
- push(word, &head);
- }
- Node *current = head;
- while (current != NULL) {
- fprintf(fout, "%s: %d\n", current->word, current->count);
- current = current->next;
- }
- fclose(fin);
- fclose(fout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement