Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.2KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "utlist.h"
  5. #define BUFLEN 20
  6. typedef struct el {
  7. char bname[BUFLEN];
  8. struct el *next, *prev;
  9. } el;
  10. static int namecmp(void *_a, void *_b)
  11. {
  12. el *a = (el*)_a;
  13. el *b = (el*)_b;
  14. return strcmp(a->bname,b->bname);
  15. }
  16. int main()
  17. {
  18. el *name, *elt, *tmp, etmp;
  19. el *head = NULL; /* important- initialize to NULL! */
  20. char linebuf[BUFLEN];
  21. FILE *file;
  22. file = fopen( "test11.dat", "r" );
  23. if (file == NULL) {
  24. perror("can't open: ");
  25. exit(-1);
  26. }
  27. while (fgets(linebuf,BUFLEN,file) != NULL) {
  28. name = (el*)malloc(sizeof(el));
  29. if (name == NULL) {
  30. exit(-1);
  31. }
  32. strcpy(name->bname, linebuf);
  33. DL_APPEND(head, name);
  34. }
  35. DL_SORT(head, namecmp);
  36. DL_FOREACH(head,elt) {
  37. printf("%s", elt->bname);
  38. }
  39. memcpy(etmp.bname, "WES\n", 5UL);
  40. DL_SEARCH(head,elt,&etmp,namecmp);
  41. if (elt != NULL) {
  42. printf("found %s\n", elt->bname);
  43. }
  44. /* now delete each element, use the safe iterator */
  45. DL_FOREACH_SAFE(head,elt,tmp) {
  46. DL_DELETE(head,elt);
  47. }
  48. fclose(file);
  49. return 0;
  50. }