Doubly linked list implemented in C language using utlist

大家讲道理
Release: 2016-11-11 13:39:05
Original
2445 people have browsed it

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utlist.h"
  
#define BUFLEN 20
  
typedef struct el {
    char bname[BUFLEN];
    struct el *next, *prev;
} el;
  
int namecmp(el *a, el *b) {
    return strcmp(a->bname,b->bname);
}
  
el *head = NULL; /* important- initialize to NULL! */
  
int main(int argc, char *argv[]) {
    el *name, *elt, *tmp, etmp;
  
    char linebuf[BUFLEN];
    int count;
    FILE *file;
  
    if ( (file = fopen( "test11.dat", "r" )) == NULL ) {
        perror("can&#39;t open: ");
        exit(-1);
    }
  
    while (fgets(linebuf,BUFLEN,file) != NULL) {
        if ( (name = (el*)malloc(sizeof(el))) == NULL) exit(-1);
        strncpy(name->bname,linebuf,BUFLEN);
        DL_APPEND(head, name);
    }
    DL_SORT(head, namecmp);
    DL_FOREACH(head,elt) printf("%s", elt->bname);
    DL_COUNT(head, elt, count);
    printf("%d number of elements in list\n", count);
  
    memcpy(&etmp.bname, "WES\n", 5);
    DL_SEARCH(head,elt,&etmp,namecmp);
    if (elt) printf("found %s\n", elt->bname);
  
    /* now delete each element, use the safe iterator */
    DL_FOREACH_SAFE(head,elt,tmp) {
      DL_DELETE(head,elt);
    }
  
    fclose(file);
  
    return 0;
}
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!