c语言遍历目录非递归

如何在linux系统中用C语言编程实现以非递归的方式查询指定目录下所有子目录的全部文件并保存文件名?

把迭代得到的非文件文件夹项,即子目录保存到一个stack中。

随后逐个弹出栈顶元素并迭代之,就实现了以非递归方式遍历文件夹。

C语言前序遍历输入后序非递归遍历输出算法

//输入先序扩展序列: 6 5 0 0 7 0 0

//后序遍历序列: 5 7 6

//       6

//     /    \

//    5      7

//   / \    / \

//  0   0  0   0

//输入先序扩展序列: 4 2 1 0 0 3 0 0 5 0 6 0 0

//后序遍历序列: 1 3 2 6 5 4

//

//            4

//        /        \

//       2          5

//     /    \      / \

//    1      3    0   6

//   / \    / \      / \

//  0   0  0   0    0   0

#includestdio.h

#includestdlib.h

#define max 100    //20  //栈的大小

struct bitree      //二叉树的结构体

{

    int data;

    struct bitree *llink; //左分支

    struct bitree *rlink; //右分支

};

struct stack_post //栈的结构体 [用于后序遍历]

{

    struct bitree *bt; //指向二叉树

    int leftOrRight;   //当前节点是哪个分支,1=左分支 0=右分支

};

struct stack_post sk[max];  //全局变量:栈(用于后序遍历)

//void build(struct bitree *n)

//创建二叉树: 先序扩展序列 + 递归法

void build(struct bitree **n)

{

    //注:struct bitree **n 是指针的指针

    int ch;

    scanf(“%d”,ch);

    if(ch==0)

    {

        *n=NULL;

    }

    else

    {

        *n=(struct bitree *)malloc(sizeof(struct bitree));

        if(*n == NULL)

        {

            printf(“\nMemory overflow!\n”);

            exit(1);

        }

        (*n)-data=ch;

        build(((*n)-llink));

        build(((*n)-rlink));

    }

    //原代码

    /*

    n=(struct bitree *)malloc(sizeof(struct bitree));

    scanf(“%d”,ch);

    if(ch==0)

        n=NULL;

    else

    {

        if(!(n=(struct bitree *)malloc(sizeof(struct bitree))))

        {

            return;

        }

        n-data=ch;

        build(n-llink);

        build(n-rlink);

    }

    */

}

//后序遍历序列(非递归)

void Post_Order(struct bitree *n)

{

    struct bitree *p=NULL;

    ////struct stack_post sk[max];  //全局变量:栈(用于后序遍历)

    int top=0;       //栈顶为0, top=1用于存放第1个数据

    int leftOrRight; //当前节点是哪个分支,1=左分支 0=右分支

    struct bitree *tmpTree;

    if(n == NULL) return;

    p = n;           //当前二叉树的节点

    leftOrRight = 1;  //1=左分支(默认从左分支开始遍历)

    while(p != NULL)

    {

        if(p-llink != NULL) //有左分支,当前节点入栈

        {

            top++;

            sk[top].bt=p;

            sk[top].leftOrRight=leftOrRight;

            p = p-llink;

            leftOrRight = 1;

        }

        else if(p-rlink != NULL) //有右分支,当前节点入栈

        {

            top++;

            sk[top].bt=p;

            sk[top].leftOrRight=leftOrRight;

            p = p-rlink;

            leftOrRight = 0;

        }

        else //该节点是”叶子”

        {

            printf(“%d “,p-data);

            while(1) //处于”回溯”状态

            {

                tmpTree=sk[top].bt; //看[栈顶]

                if(tmpTree==NULL) //[栈]已经没有数据

                {

                    p=NULL; //整个遍历过程结束,退出循环

                    break;

                }

                if(leftOrRight == 1) //处于”左分支”回溯

                {

                    if(tmpTree-rlink == NULL)//[栈顶]的节点没有右分支,出栈,打印

                    {

                        p=sk[top].bt;

                        leftOrRight=sk[top].leftOrRight;

                        top–;

                        printf(“%d “,p-data);

                        //仍然处于”回溯”状态

                    }

                    else //[栈顶]的节点有右分支,不出栈,右分支成为当前节点

                    {

                        p=tmpTree-rlink;

                        leftOrRight=0;    //设置当前处于右分支

                        break; //退出”回溯”状态

                    }

                }

                else  //处于”右分支”回溯

                {

                    //[栈]有节点,出栈,打印

                    p=sk[top].bt;

                    leftOrRight=sk[top].leftOrRight;

                    top–;

                    printf(“%d “,p-data);

                    //仍然处于”回溯”状态

                }

            } //while(1)结束

        } //if(p-llink != NULL)else结束

    } //while(p != NULL)结束

    //栈顶top=0,说明[栈]已经没有数据

    printf(“\n核对,栈顶 top = %d\n”,top);

}

//有错误

//原代码,后序遍历序列(非递归)

/*

void postorder(struct bitree *n)

{

    struct bitree *p,*q;

    int i;

    p=(struct bitree *)malloc(sizeof(struct bitree));

    p=n;

    q=(struct bitree *)malloc(sizeof(struct bitree));

    struct bitree *s[max];

    for(i=0;imax;i++)

    {

        s[i]=(struct bitree *)malloc(sizeof(struct bitree));

    }

    int top=-1;

    do

    {

        while(p!=NULL)

        {

            if(p-rlink!=NULL)

            {

                top++;

                s[top]=p-rlink;

            }

            p=p-llink;;

        }

        p=s[top];

        top–;

        if(p-rlink==NULL||p-rlink==q)

        {

            printf(“%d “,p-data);

            q=p;

            p=NULL;

        }

        else

            p=p-rlink;

    }while(p!=NULL||top!=-1);

}

*/

//后序遍历序列(递归) [用于对比测试]

void postTest(struct bitree *n)

{

    if(n!=NULL)

    {

        postTest(n-llink);

        postTest(n-rlink);

        printf(“%d “,n-data);

    }

}

int main()

{

    struct bitree *n;

    //原代码n=(struct bitree *)malloc(sizeof(struct bitree));

    printf(“Input the preorder-numbers of the tree(‘0’ for NULL):\n”);

    //原代码build(n);

    build(n);

    printf(“\nPostorder is below: “);

    //原代码postorder(n); //后序遍历序列(非递归)

    Post_Order(n);        //后序遍历序列(非递归)

    printf(“\n后序遍历序列(递归): “);

    postTest(n); //用于对比测试

    printf(“\n”);

    return 0;

}

二叉树先序非递归遍历C语言算法

#include “stdio.h”

#include “stdlib.h”

#define STACK_INIT_SIZE 10 //栈的初始长度

#define STACKINCREMENT 5 //栈的追加长度

typedef struct bitree{

char data;

struct bitree *lchild,*rchild;

}bitree; //二叉树结点定义

typedef struct {

bitree **base;

bitree **top;

int stacksize;

}sqstack; // 链栈结点定义top栈顶 base栈底 且栈元素是指向二叉树结点的二级指针

//建立一个空栈

int initstack(sqstack *s)

{s-base=(bitree *)malloc(STACK_INIT_SIZE*sizeof(bitree)); //栈底指向开辟空间

if(!s-base) exit(1); //抛出异常

s-top=s-base; //栈顶=栈尾 表示栈空

s-stacksize=STACK_INIT_SIZE; //栈长度为开辟空间大小

return 1;

}

//进栈

int push(sqstack *s,bitree *e)

{if(s-top-s-base=s-stacksize) //如果栈满 追加开辟空间

{s-base=(bitree *)realloc (s-base,(s-stacksize+STACKINCREMENT)* sizeof(bitree));

if(!s-base) exit(1); //抛出异常

s-top=s-base+s-stacksize; //感觉这一句没用

s-stacksize+=STACKINCREMENT;}

*(s-top)=e;s-top++; //进栈 栈顶后移

return 1;

}

//出栈

int pop(sqstack *s,bitree **e)

{if(s-top==s-base) return 0; //栈空 返回0

–s-top;*e=*(s-top); //栈顶前移 取出栈顶元素给e

return 1;}

//取栈顶

int gettop(sqstack *s,bitree **e) //去栈顶元素 注意top指向的是栈顶的后一个

{if(s-top==s-base) return 0; //所以 s-top-1

*e=*(s-top-1);

return 1;

}

/*————————非递归—–先序建立二叉树———————————-*/

bitree *createprebitree()

{char ch;bitree *ht,*p,*q;

sqstack *s;

s=malloc(sizeof(bitree)); //加上这一句为s 初始化开辟空间

ch=getchar();

if(ch!=’#’ch!=’\n’) /* 输入二叉树先序顺序 是以完全二叉树的先序顺序

不是完全二叉树的把没有的结点以#表示 */

{ht=(bitree *)malloc(sizeof(bitree));

ht-data=ch;

ht-lchild=ht-rchild=NULL;

p=ht;

initstack(s);

push(s,ht); //根节点进栈

while((ch=getchar())!=’\n’) // 算

{if(ch!=’#’) {q=(bitree *)malloc(sizeof(bitree)); // 法

q-data=ch; //

if(p==*(s-top-1)) p-lchild=q; // 核

else p-rchild=q; //

push(s,q);p=q; // 心

} //

else {if(p==*(s-top-1)) p-lchild=NULL; // 的

else p-rchild=NULL; //

pop(s,p);} // 步

//

} // 骤

return ht;

}

else return NULL;

}

/*————————–递归———先序建立二叉树——————————-*/

void CreateBiTree(bitree **T) {

//按先序次序输入二叉树中的结点的值(一个字符),空格字符表示空树,

//构造二叉链表表示二叉树

char ch;

scanf(“%c”,ch);

if(ch==’#’) *T=NULL;

else{

*T=(bitree * )malloc(sizeof(bitree));

if(!*T) exit(1);

(*T)-data=ch; //生成根结点

CreateBiTree((*T)-lchild); //构造左子树

CreateBiTree((*T)-rchild); //构造右子树

}

}

/*————————–非递归——-中序建立二叉树——————————-*/

/*————————–递归———中序建立二叉树——————————-*/

/*————————–非递归——-后序建立二叉树——————————-*/

/*————————–递归———后序建立二叉树——————————-*/

/*———————–非递归——先序输出二叉树——————————*/

void preordertraverse(bitree *h)

{sqstack m;

initstack(m);

while(h||m.base!=m.top)

{if(h) {push(m,h);printf(“%c”,h-data);h=h-lchild;}

else{pop(m,h);

h=h-rchild;}

}

}

/*————————非递归—–中序输出二叉树—————————-*/

void inordertraverse(bitree *h)

{sqstack m;

initstack(m);

while(h||m.base!=m.top)

{if(h) {push(m,h);h=h-lchild;}

else {

pop(m,h);

printf(“%c”,h-data);

h=h-rchild;

}

}

}

/*———————非递归—-后序遍历二叉树———————————-*/

void postordertraverse(bitree *h)

{

sqstack m;

initstack(m);

while(h||m.base!=m.top)

{if(h) {

push(m,h);

h=h-lchild;}

else {

bitree *r; //使用r结点表示访问了右子树 代替标志域

gettop(m,h);

if(h-rchildh-rchild!=r)

{h=h-rchild;

push(m,h);

h=h-lchild;}

else{pop(m,h);

printf(“%c”,h-data);

r=h;h=NULL;}

}

}

}

//层次遍历二叉树 用队列 哈哈以后做

/*——————————-主过程——————————-*/

int main()

{bitree *ht;

printf(“先序非递归建立一个二叉树:”);

if((ht=createprebitree())!=NULL) //非递归建立

//CreateBiTree(ht);

//if(ht!=NULL) //递归建立

{

printf(“先序遍历输出二叉树:”);

preordertraverse(ht);

putchar(‘\n’);

printf(“中序遍历输出二叉树:”);

inordertraverse(ht);

putchar(‘\n’);

printf(“后序遍历输出二叉树:”);

postordertraverse(ht);

putchar(‘\n’);

}

else printf(“空二叉树\n”);

}

c语言遍历目录非递归

二叉树中序遍历非递归算法(c语言实现)

#include “stdio.h”

#include “stdlib.h”

#include “string.h”

#define null 0

struct node

{

char data;

struct node *lchild;

struct node *rchild;

};

//先序,中序 建树

struct node *create(char *pre,char *ord,int n)

{

struct node * head;

int ordsit;

head=null;

if(n=0)

{

return null;

}

else

{

head=(struct node *)malloc(sizeof(struct node));

head-data=*pre;

head-lchild=head-rchild=null;

ordsit=0;

while(ord[ordsit]!=*pre)

{

ordsit++;

}

head-lchild=create(pre+1,ord,ordsit);

head-rchild=create (pre+ordsit+1,ord+ordsit+1,n-ordsit-1);

return head;

}

}

//中序递归遍历

void inorder(struct node *head)

{

if(!head)

return;

else

{

inorder(head-lchild );

printf(“%c”,head-data );

inorder(head-rchild );

}

}

//中序非递归遍历

void inorder1(struct node *head)

{

struct node *p;

struct node *stack[20];

int top=0;

p=head;

while(p||top!=0)

{

while (p)

{

stack[top++]=p;

p=p-lchild ;

}

p=stack[–top];

printf(“%c “,p-data );

p=p-rchild ;

}

}

//主函数

int main()

{

struct node * head;

char pre[30],ord[30];

int n;

gets(pre);

gets(ord);

n=strlen(pre);

head=create(pre,ord,n);

inorder(head);

printf(“\n”);

inorder1(head);

printf(“\n”);

}

//测试事例;

/*

-+a*b%cd/ef

a+b*c%d-e/f

*/

几个月前自己编写,原版

vc++ 6.0实验通过

怎么样,老板,第一次上百度知道,好激动

给点面子

给分!给分啊

先序遍历的非递归算法。用c语言写。

void PreOrderUnrec(Bitree t)

{

SqStack s;

StackInit(s);

p=t;

while (p!=null || !StackEmpty(s))

{

while (p!=null) //遍历左子树

{

visite(p-data);

push(s,p);

p=p-lchild;

}//endwhile

if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历

{

p=pop(s);

p=p-rchild;

}//endif

}//endwhile

}//PreOrderUnrec

层次序的非递归遍历算法的实现代码(C语言)..

是二叉链表的层次遍历吧?

#include stdlib.h

//定义动态分配空间的二叉链表类型

typedef struct BiTNode{

 char data;

 struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

//层次遍历输出二叉链表的所有结点的值

void LevelOrderTraverse(BiTree T){

 typedef struct PNode{

  BiTNode *ptr;struct PNode *next;

 }PNode;//定义链表结点类型,链表的数据域存放二叉链表结点的指针

 PNode *R,*L,*p;

 if(T){

  R=(PNode*)malloc(sizeof(PNode));//建立第一个链表结点

  if(!R){

   printf(“\nLevelOrderTraverse Error: Failed To MALLOC Memory!”);

   return;

  }

  R-ptr=T;R-next=NULL;//R指向尾结点,尾结点指针域必须为空

  L=R;p=L;//L指向链表头结点

  while(p){//p结点的数据域指向当前处理的二叉链表结点

   printf(“%c”,p-ptr-data);

   if(p-ptr-lchild){//如果有左孩子,添加一个结点

    R-next=(PNode*)malloc(sizeof(PNode));

    if(!(R-next)){

     printf(“\nLevelOrderTraverse Error: Failed To MALLOC Memory!”);

     return;

    }

    R=R-next;R-ptr=p-ptr-lchild;R-next=NULL;//R重新指向尾结点,尾结点的数据域为当前处理的二叉链表结点的左孩子指针

   }

   if(p-ptr-rchild){//如果有右孩子,添加一个结点

    R-next=(PNode*)malloc(sizeof(PNode));

    if(!(R-next)){

     printf(“\nLevelOrderTraverse Error: Failed To MALLOC Memory!”);

     return;

    }

    R=R-next;R-ptr=p-ptr-rchild;R-next=NULL;

   }

   L=p-next;//L指向下一个链表结点,准备释放p结点

   free(p);//释放p结点

   p=L;//p重新指向链表头结点

  }

 }

}

这里使用了队列结构来存储某个结点的孩子结点。

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月26日 02:40:26
下一篇 2024年3月26日 02:51:43

相关推荐

  • c语言改写模式,c语言实现修改功能

    c语言程序修改? 1、这个程序有4个错误,我都加粗了,第一个是m没有赋初值,第二个是while表达式中的ch=getchar()需要括号括起来,第三个是m=m*10+ch-0中的0也需要用单引号括起来,第四个是第2个while中为m!=0。 2、define容易造成误会,因为不符合一般的编程习惯,false 0, true 1;scanf放在你的那个地方是达…

    2024年5月23日
    4100
  • c语言控制代码的换码序列,c语言交换代码

    求C语言编程大神解答一下下面这个编程代码? k==5,用5去除125余0,所以r=125%5中r为0。由于!0为1,所以执行while循环体:先打印出5(k的值),再n=n/k==125/5=25;由于251则再打印出*号。这一循环结果输出是5*。 下面是我的代码,三个函数分别对应三个问题。 在实现基本要求的前提下,拓展了可以从键盘输入的功能,以下为各题代码…

    2024年5月23日
    5800
  • c语言扫描io脚状态,c语言端口扫描

    求51单片机的上升沿和下降沿C语言检测程序列子,端口就是普通IO口。 上升沿触发是当信号有上升沿时的开关动作,当电位由低变高而触发输出变化的就叫上升沿触发。也就是当测到的信号电位是从低到高也就是上升时就触发,叫做上升沿触发。 单片机怎么计算1s内下降沿的个数的C语言程序或者计算两个下降沿的时间(检测脉冲频率)计算1s内下降沿的个数方法是,一个定时器设置定时1…

    2024年5月23日
    4500
  • c语言mallloc使用的简单介绍

    C语言中使用malloc必须加#includemallo.h? 1、在C语言中使用malloc函数进行动态内存分配。malloc的全称是memory allocation,中文叫动态内存分配。原型:extern void malloc(unsigned int num_bytes);功能:分配长度为num_bytes字节的内存块。 2、你可以看一下C语言那本…

    2024年5月23日
    4500
  • c语言三位小数,C语言三位小数

    怎样用C++语言输出精确到小数点后三位的数? 1、用C++语言输出精确到小数点后三位的数,可以参考下面给出的代码:coutsetiosflags(ios:fixed)setprecision(3)。其中 setiosflags中set是设置的意思。ios是iostream的缩写,即输入输出流。flags是标志的意思。 2、要精确到小数点后若干位,则数据类型为…

    2024年5月23日
    7500
  • c语言21点游戏,二十一点游戏代码c语言

    如何使用C语言编写简单小游戏? 1、数学知识:长方形的面积S=a*b 长方形周长L=2*(a+b)其中a b分别为长方形的宽和高。算法分析:长方形面积及周长均依赖于宽和高,所以先要输入宽高值,然后根据公式计算,输出结果即可。 2、/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++0、turbo…

    2024年5月23日
    6500
  • c语言当中的null,C语言当中的符号

    C/C++中,NULL和null的区别是什么? nul 和 null要看编译器,不同的编译器有所区别。 所以C或者C++中都使用一个特殊定义NULL表示无效值,其本质就是未定义具体数据类型的0值。 null是是什么都没有的意思。在java中表示空对象。 本意是“空的;元素只有零的”意思。计算机中通常表示空值,无结果,或是空集合。\x0d\x0a在ASCII码…

    2024年5月23日
    4700
  • 包含c语言对txt文件命名的词条

    如何在C语言编程里面修改源文件名字 如果你是在WINDOWS的话,简单了,随便用个编辑器,比如记事本,然后写c源程序,保存到你想要保存的位置。如果你在DOS下,可以用edit,写好以后,按alt键,选择文件菜单,然后保存。 用open打开文件,注意操作模式使用“修改”或者“添加” 用write或者fprintf向文件中写入你的内容。 用close关闭文件。 …

    2024年5月23日
    5000
  • 学c语言编程,学c语言编程用什么软件

    编程开发必须要学C语言吗? 1、要学习。编程开发的学习内容主要包括c语言、python和c+语言。C语言作为一种简单灵活的高级编程语言,它是一个面向过程的语言,一般是作为计算机专业的基础入门语言课程。 2、C语言。对于刚接触编程的人来说,先学习C语言是非常重要的。C语言可以说是是计算机编程语言的鼻祖,其他的编程语言几乎全是由C语言变化衍生出来的。 3、不需要…

    2024年5月23日
    3500
  • c语言用string定义字符串,c语言中用string类型来处理字符串类型

    C++怎样定义定义字符串 1、第一是字符数组来表示字符串。用下面的语句声明:char a[10];C语言中字符数组与字符串的唯一区别是字符串末尾有一个结束符\0,而字符数组不需要。 2、在C中定义字符串有下列几种形式:字符串常量,char数组,char指针 字符串常量 即:位于一对双括号中的任何字符。双引号里的字符加上编译器自动提供的结束标志\0字符,作为 …

    2024年5月23日
    4500

发表回复

登录后才能评论



关注微信