Guest User

Untitled

a guest
Oct 21st, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. struct liste
  6. {
  7.      char * data;
  8.      struct liste * next;
  9. };
  10. // don't fucking typedef this shit, do it right
  11. struct liste *head, *tail; //I renamed this to tail, since that's what it is, current sounds confusing
  12. void add(char *); // add your function prototypes, even in small programs. Get into the habit
  13. void clear(struct liste *) // recursive free memory
  14.  
  15. void add(char *chr)
  16. {
  17.      struct liste *tmp = malloc(sizeof(struct liste));
  18.      tmp->data = strdup(chr); // don't forget that this is a c-string, so = is pointer equality
  19.      tmp->next = NULL;
  20.      if (tail == NULL)
  21.      {
  22.           // if the list has no elements yet, then this should be
  23.           // both the head, and our tail.
  24.           tail = head = tmp;
  25.      }
  26.      else
  27.      {
  28.           // the list already has elements
  29.           tail->next = tmp; // create the link in the list
  30.           tail = tmp; // advance our tail position
  31.      }
  32.      printf("word added: %s \t\t head: %s\n",tail->data,head->data);
  33. }
  34. void clear(struct liste *node)
  35. {
  36.      if (node->next != NULL)
  37.           clear(node->next)
  38.      free(node->data);
  39.      free(node);
  40. }
  41.  
  42. int main()
  43. { // always give the brace it's own line...it helps finding missing ones
  44.      /* variable definitions */ // always put these BEFORE any code or initializations, this is C
  45.      FILE *doysa;
  46.      char *c;
  47.      int i, slen;
  48.      /* initialize variables */
  49.      head = tail = NULL; // set up our empty list
  50.      /* program code */
  51.      if ((dosya = fopen("C:/Users/omerf/CLionProjects/untitled/Input1.txt","r")) == NULL) // file failed
  52.      {
  53.           printf("dosya acilamadi\n");
  54.           exit(-1); // i like to use negative failure numbers, -1 is also EOF
  55.      }
  56.      c = malloc(20); // allocate character buffer
  57.      while (!feof(dosya))
  58.      {
  59.           fscanf(dosya,"%s",c);
  60.           slen = strlen(c); // this will speed up your program by reducing memory operations
  61.           for(i = 0; i < slen; ++i)
  62.           {
  63.                if(!isalpha(c[i]))
  64.                     c[i] = '\0'; // try to avoid same line if bodies..
  65.           }
  66.           add(c);
  67.      }
  68.      fclose(dosya);
  69.      /* deallocate memory */
  70.      free(c); // no longer need buffer
  71.      if (head != NULL) // if we have a list with entries
  72.           clear(head); // our recursive function will clean everything up for us nicely.
  73.      return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment