文件检索(C循环双链表实现)

来源:互联网 发布:统计学书籍推荐 知乎 编辑:程序博客网 时间:2024/04/29 17:41
/**********************************************************************************读取目录中的文件名,放到循环双链表中,按1查看前一个,按2查看后一个,按3退出。**********************************************************************************/#include<stdio.h>#include<stdlib.h>#include<string.h>#include"unistd.h"#include"sys/types.h"#include"fcntl.h"#include"dirent.h"struct DuLNode{    char num[100];    struct DuLNode *prior,*next;};struct DuLNode *head=NULL;struct DuLNode *tmp=NULL;cre_list() //初始化{    head = (struct DuLNode *)malloc(sizeof(struct DuLNode));    head->prior = head;    head->next = head;}add_DuLNode(char *num) //插入结点{    if(head->num == 0)    {strcpy(head->num,num);    }else{struct DuLNode *ptr = (struct DuLNode *)malloc(sizeof(struct DuLNode));strcpy(ptr->num,num);    ptr->next=head->next;        head->next=ptr;    ptr->prior=head;    ptr->next->prior=ptr;}}display_DuLNode() //遍历链表{    struct DuLNode *p = head;    do    {        printf("%s\n",p->num);        p = p->next;    }while(p!=head);}display_next_DuLNode() //{   // struct DuLNode *p = head;tmp= tmp->next;    printf("%s\n",tmp->num);}display_prior_DuLNode() //{    //struct DuLNode *p = head;tmp= tmp->prior;    printf("%s\n",tmp->num);}int main(int argc, char * argv[]){    int i=0;    cre_list();DIR *dp;struct dirent *dirp;if (argc != 2) {printf("Usage: ls directory_name");}//打开指定的目录if ((dp = opendir(argv[1])) == NULL){printf("can't open %s\n", argv[1]);exit(1);}//遍历目录while ((dirp = readdir(dp)) != NULL){    if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)    {        continue;    }add_DuLNode(dirp->d_name);}//关闭目录closedir(dp);    display_DuLNode();tmp=head;    while(1)    {            printf("1.prior 2.next 3.exit:");        scanf("%d",&i);    switch(i)    {     case 1:         { display_prior_DuLNode();     break;     }     case 2:     { display_next_DuLNode();     break;     }     case 3:     {     return 0;     }     default:     {     continue;     }     }    }    return 0;}

原创粉丝点击