非遞歸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/zh-tw/n/131951.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
YOCR的頭像YOCR
上一篇 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

發表回復

登錄後才能評論