zoukankan      html  css  js  c++  java
  • 链表应用:输出目录下所有文件(夹)(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
    ");
                    return head;
             }
    
            if(strcmp(node->name, temp->name) < 0)
            {
                    printf("insert in head
    ");
                    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("
    ");
    }
    
    int main(int argc, char *argv[])
    {
            linklistp head=NULL, node;
            DIR *dir = NULL;
            if(argc != 2)
            {
                    printf("please inter dir
    ");
                    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, '', 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);
    }
  • 相关阅读:
    洛谷 P2023 BZOJ 1798 [AHOI2009]维护序列
    洛谷 P1640 BZOJ 1854 [SCOI2010]连续攻击游戏
    洛谷 P4300 BZOJ 1266 [AHOI2006]上学路线route
    洛谷 P1886 滑动窗口
    洛谷 P1063 能量项链
    洛谷 P1156 垃圾陷阱
    洛谷 P1854 花店橱窗布置
    洛谷 P3800 Power收集
    洛谷 P2285 BZOJ 1207 [HNOI2004]打鼹鼠
    day31
  • 原文地址:https://www.cnblogs.com/MrRS/p/9018170.html
Copyright © 2011-2022 走看看