链表应用:输出目录下所有文件(夹)(Ubuntu16.04)
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> #include <dirent.h> typedef struct linklist { char *name; struct linklist *next; }linknode, *linklistp; linklistp insert_local(linklistp head, const linklistp node) { linklistp temp, perv; assert(node); temp = head; if(temp == NULL) { head = node; printf("creat linklist\r\n"); return head; } if(strcmp(node->name, temp->name) < 0) { printf("insert in head\r\n"); node->next = temp; head = node; return head; } perv = head; temp = temp->next; while(temp) { if(strcmp(node->name, temp->name) > 0) { perv = temp; temp = temp->next; } else break; } node->next = temp; perv->next = node; return head; } void output(linklistp head) { linklistp temp; temp = head; assert(temp); while(temp) { printf("%s ", temp->name); temp = temp->next; } printf("\n"); } int main(int argc, char *argv[]) { linklistp head=NULL, node; DIR *dir = NULL; if(argc != 2) { printf("please inter dir\r\n"); exit(EXIT_FAILURE); } dir = opendir(argv[1]); while((dp = readdir(dir)) != NULL) { if(dp->d_name[0] == '.') continue; node = (linklistp)malloc(sizeof(node)); node->next = NULL; node->name = (char *)malloc(sizeof(dp->d_name) + 1); memset(node->name, '\0', sizeof(dp->d_name) + 1); strncpy(node->name, dp->d_name, sizeof(dp->d_name)); head = insert_local(head, node); output(head); getchar(); } //output(head); }