本文目錄一覽:
- 1、C語言漢諾塔問題非遞歸解法代碼求大神講解
- 2、C語言,如何用非遞歸方法輸出二叉樹的根到所有葉子路徑?
- 3、求C語言快排非遞歸演算法解析。非遞歸。。
- 4、求C語言漢諾塔非遞歸演算法
- 5、C語言中遞歸函數是,非遞歸函數是?能否舉例子?
- 6、二叉樹中序遍歷非遞歸演算法(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