[C++] C语言使用utlist实现的双向链表 →→→→→进入此内容的聊天室

来自 , 2019-04-11, 写在 C++, 查看 137 次.
URL http://www.code666.cn/view/7cb1f2f2
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "utlist.h"
  5.  
  6. #define BUFLEN 20
  7.  
  8. typedef struct el {
  9.     char bname[BUFLEN];
  10.     struct el *next, *prev;
  11. } el;
  12.  
  13. int namecmp(el *a, el *b) {
  14.     return strcmp(a->bname,b->bname);
  15. }
  16.  
  17. el *head = NULL; /* important- initialize to NULL! */
  18.  
  19. int main(int argc, char *argv[]) {
  20.     el *name, *elt, *tmp, etmp;
  21.  
  22.     char linebuf[BUFLEN];
  23.     int count;
  24.     FILE *file;
  25.  
  26.     if ( (file = fopen( "test11.dat", "r" )) == NULL ) {
  27.         perror("can't open: ");
  28.         exit(-1);
  29.     }
  30.  
  31.     while (fgets(linebuf,BUFLEN,file) != NULL) {
  32.         if ( (name = (el*)malloc(sizeof(el))) == NULL) exit(-1);
  33.         strncpy(name->bname,linebuf,BUFLEN);
  34.         DL_APPEND(head, name);
  35.     }
  36.     DL_SORT(head, namecmp);
  37.     DL_FOREACH(head,elt) printf("%s", elt->bname);
  38.     DL_COUNT(head, elt, count);
  39.     printf("%d number of elements in list\n", count);
  40.  
  41.     memcpy(&etmp.bname, "WES\n", 5);
  42.     DL_SEARCH(head,elt,&etmp,namecmp);
  43.     if (elt) printf("found %s\n", elt->bname);
  44.  
  45.     /* now delete each element, use the safe iterator */
  46.     DL_FOREACH_SAFE(head,elt,tmp) {
  47.       DL_DELETE(head,elt);
  48.     }
  49.  
  50.     fclose(file);
  51.  
  52.     return 0;
  53. }
  54. //cpp/8957

回复 "C语言使用utlist实现的双向链表"

这儿你可以回复上面这条便签

captcha