首页 > 编程知识 正文

非递归c语言图,简单的递归例子c语言

时间:2023-12-29 13:16:33 阅读:329776 作者:UZKH

本文目录一览:

C语言汉诺塔问题非递归解法代码求大神讲解

int game2()要改为int main()后才可编译运行:

#includestdio.h

#includestdlib.h

#define CSZL 10

#define FPZL 10

typedef struct hanoi

{

int n;

char x,y,z;

}hanoi;

typedef struct Stack //定义栈结点

{

hanoi *base,*top;

int stacksize;

}Stack;

int InitStack(Stack *S)

{

S-base=(hanoi *)malloc(CSZL*sizeof(hanoi)); //申请栈空间

if(!S-base) //若申请不成功返回失败信息

return 0;

S-top=S-base; //置为空栈

S-stacksize=CSZL; //栈结点数

return 1;

}

int PushStack(Stack *S,int n,char x,char y,char z) //入栈

{

if(S-top-S-base==S-stacksize) //若栈已满

{

S-base=(hanoi *)realloc(S-base,(S-stacksize+FPZL)*sizeof(hanoi)); //补充申请,扩充栈空间

if(!S-base)   //若申请不成功返回失败信息

return 0;

S-top=S-base+S-stacksize; //重置栈顶指针

S-stacksize+=FPZL; //重置栈空间尺寸

}

S-top-n=n; //新结点信息存入栈顶结点

S-top-x=x;

S-top-y=y;

S-top-z=z;

S-top++; //栈中元素加1

return 1;

}

int PopStack(Stack *S,int *n,char *x,char *y,char *z) //出栈

{

if(S-top==S-base) //若栈已空

return 0; //返回出错信息

else

{

S-top--; //栈顶指针减1

*n=S-top-n; //返回出栈元素信息

*x=S-top-x;

*y=S-top-y;

*z=S-top-z;

return 1;

}

}

int EmptyStack(Stack *S) //判定是否空栈

{

if(S-base==S-top)

return 1;

else

return 0;

}

int i=1;

void Move(char x,char z) //打印移动盘子的信息

{

printf("ntt第%d步,%c--%c",i++,x,z);

}

int main() /* 非递归法 */

{

int n;

char x,y,z;

Stack *S;

system("cls"); /*清屏指令*/

S=(Stack *)malloc(sizeof(Stack)); //申请栈空间

if(!S)

return 0;

if(!InitStack(S)) //初始化栈

return 0;

printf("请输入汉诺塔的初始盘子数量以及轴的名称:");

scanf("%dt%c%c%c",n,x,y,z);

if(!PushStack(S,n,x,y,z)) //要移动的盘子数及各轴名称入栈

return 0;

while(!EmptyStack(S)) //当栈非空时循环

{

if(!PopStack(S,n,x,y,z)) //若空栈则退出循环,否则出栈一个任务

break;

if(n==1) //若只有一个盘子,直接移动

{

Move(x,z);

}

else //有1个以上的盘子时入栈(注意栈的工作特点,是后进先出,所以最先做的事要最后入栈)

{

if(!PushStack(S,n-1,y,x,z)) //将上层的n-1个盘子从y移向z

break;

if(!PushStack(S,1,x,y,z)) //将底层的盘子从x移向z

break;

if(!PushStack(S,n-1,x,z,y)) //将上层的n-1个盘子从x移向y

break;

}

}

free(S-base);

S-base=NULL;

S-top=NULL;

S-stacksize=0;

return 0;

}

C语言,如何用非递归方法输出二叉树的根到所有叶子路径?

用栈来实现非递归的后序遍历,每遍历到一个叶子时,从栈顶到栈底即为该叶子从双亲开始直到根的所有祖先结点,也就是从叶子到根的路径

求C语言快排非递归算法解析。非递归。。

//快排非递归算法

void merge(int a[], int low, int center, int high){//这里的merge与教科书上有不同。我们用两个数组L[],R[]来存储a[]需要合并的两段

int i = 0;

int j = 0;

int k = 0;

int count = 0;

if(low = high) return;

int m = center - low + 1;

int n = high - center;

int *L = (int *)malloc(sizeof(int)*SCALE);

int *R = (int *)malloc(sizeof(int)*SCALE);

if(!L || !R){

printf("归并排序merge()内存分配故障!");

exit(0);

}

for( count=0; count=m; count++){

L[count] = a[low+count];

}

for( int count=0; count=n; count++){

R[count] = a[low+count+m];

}

for(i=0,j=0,k=low; imjn; ++k){

if( L[i] = R[j] ){

a[k] = L[i++];

}

else{

a[k] = R[j++];

}

}

while(i m){

a[k++] = L[i++];

}

while(j n){

a[k++] = R[j++];

}

free(L);

free(R);

}

求C语言汉诺塔非递归算法

#include#define MAXSTACK 10 /* 栈的最大深度 */int c = 1; /* 一个全局变量,表示目前移动的步数 */struct hanoi { /* 存储汉诺塔的结构,包括盘的数目和三个盘的名称 */

int n;

char x, y, z;

};void move(char x, int n, char y) /* 移动函数,表示把某个盘从某根针移动到另一根针 */

{

printf("%d. Move disk %d from %c to %cn", c++, n, x, y);

}void hanoi(int n, char x, char y, char z) /* 汉诺塔的递归算法 */

{

if (1 == n)

move(x, 1, z);

else {

hanoi(n - 1, x, z, y);

move(x, n, z);

hanoi(n - 1, y, x, z);

}

}void push(struct hanoi *p, int top, char x, char y, char z,int n)

{

p[top+1].n = n - 1;

p[top+1].x = x;

p[top+1].y = y;

p[top+1].z = z;

}void unreverse_hanoi(struct hanoi *p) /* 汉诺塔的非递归算法 */

{

int top = 0;while (top = 0) {

while (p[top].n 1) { /* 向左走到尽头 */

push(p, top, p[top].x, p[top].z, p[top].y, p[top].n);

top++;

}

if (p[top].n == 1) { /* 叶子结点 */

move(p[top].x, 1, p[top].z);

top--;

}

if (top = 0) { /* 向右走一步 */

move(p[top].x, p[top].n, p[top].z);

top--;

push(p, top, p[top+1].y, p[top+1].x, p[top+1].z, p[top+1].n);

top++;

}

}

}int main(void)

{

int i;

printf("reverse program:n");

hanoi(3, 'x', 'y', 'z');

printf("unreverse program:n");

struct hanoi p[MAXSTACK];

c = 1;

p[0].n = 3;

p[0].x = 'x', p[0].y = 'y', p[0].z = 'z';

unreverse_hanoi(p);return 0;

}

C语言中递归函数是,非递归函数是?能否举例子?

直接或间接调用自已的函数就是递归函数,否则为非递归函数。如:

unsigned fun(unsigned x){

    if(x==1 || x==0)

        return 1;

    return x*fun(x-1);

}

这个函数的体中出现了调用自己的语句fun(x-1);,所以是递归函数。

二叉树中序遍历非递归算法(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实验通过

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

给点面子

给分!给分啊

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。