您的位置:首页 > 其它

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

2012-07-13 09:10 281 查看
/**********************************************************************************
读取目录中的文件名,放到循环双链表中,按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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: