• 链表应用:输出目录下所有文件(夹)(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);
    }
  • 相关阅读:
    Shiro学习(19)动态URL权限限制
    Shiro学习(18)并发人数限制
    Shiro学习(17)OAuth2集成
    Shiro学习(16)综合实例
    Shiro学习(15)单点登录
    Shiro学习(14)SSL
    项目三:ssm仓库管理系统
    项目二:企业级java电商网站开发(服务端)
    项目一:ssm超市订单管理系统
    @ResponseBody注解
  • 原文地址:https://www.cnblogs.com/MrRS/p/9018170.html
Copyright © 2020-2023  润新知