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

本文目录一览:

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(“\n\t\t第%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(“%d\t%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实验通过

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

给点面子

给分!给分啊

原创文章,作者:YOCR,如若转载,请注明出处:https://www.506064.com/n/131951.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
YOCRYOCR
上一篇 2024-10-03 23:48
下一篇 2024-10-03 23:48

相关推荐

  • AES加密解密算法的C语言实现

    AES(Advanced Encryption Standard)是一种对称加密算法,可用于对数据进行加密和解密。在本篇文章中,我们将介绍C语言中如何实现AES算法,并对实现过程进…

    编程 2025-04-29
  • Python简单数学计算

    本文将从多个方面介绍Python的简单数学计算,包括基础运算符、函数、库以及实际应用场景。 一、基础运算符 Python提供了基础的算术运算符,包括加(+)、减(-)、乘(*)、除…

    编程 2025-04-29
  • 学习Python对学习C语言有帮助吗?

    Python和C语言是两种非常受欢迎的编程语言,在程序开发中都扮演着非常重要的角色。那么,学习Python对学习C语言有帮助吗?答案是肯定的。在本文中,我们将从多个角度探讨Pyth…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29
  • Python被称为胶水语言

    Python作为一种跨平台的解释性高级语言,最大的特点是被称为”胶水语言”。 一、简单易学 Python的语法简单易学,更加人性化,这使得它成为了初学者的入…

    编程 2025-04-29
  • Python海龟代码简单画图

    本文将介绍如何使用Python的海龟库进行简单画图,并提供相关示例代码。 一、基础用法 使用Python的海龟库,我们可以控制一个小海龟在窗口中移动,并利用它的“画笔”在窗口中绘制…

    编程 2025-04-29
  • OpenJudge答案1.6的C语言实现

    本文将从多个方面详细阐述OpenJudge答案1.6在C语言中的实现方法,帮助初学者更好地学习和理解。 一、需求概述 OpenJudge答案1.6的要求是,输入两个整数a和b,输出…

    编程 2025-04-29
  • Python按位运算符和C语言

    本文将从多个方面详细阐述Python按位运算符和C语言的相关内容,并给出相应的代码示例。 一、概述 Python是一种动态的、面向对象的编程语言,其按位运算符是用于按位操作的运算符…

    编程 2025-04-29
  • 台阶走法递归

    台阶走法递归是一个经典的递归问题,在计算机算法中有着广泛的应用。本篇文章将从递归的思想出发,详细分析如何解决这个问题。 一、递归基础知识 递归是指一个函数直接或间接地调用自身。递归…

    编程 2025-04-29
  • MySQL递归函数的用法

    本文将从多个方面对MySQL递归函数的用法做详细的阐述,包括函数的定义、使用方法、示例及注意事项。 一、递归函数的定义 递归函数是指在函数内部调用自身的函数。MySQL提供了CRE…

    编程 2025-04-29

发表回复

登录后才能评论