作业7——递归

1. 递归实验–阶乘
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

int JieCheng(int n)

{

/* 代码填在这里 */
    if(n == 1)    return n;
    if(n > 15)
    {
        printf("无法计算\n");
        return 0;
    }
    return n*JieCheng(n - 1);
}

 

int main()

{

    int a;

    int n;

    a=0;

    printf("Input n:\n");

    scanf("%d",&n);

    a = JieCheng(n);

    printf("%d的阶乘为%d",n,a);

}

2.用递归方法创建单链表-副本
#include <stdio.h>   
#include <stdlib.h>   
#include <malloc.h>

#define ERROR 0
#define OK 1

typedef int ElemType;   
typedef int Status;

// definition of node structure of singly linked list
typedef struct L_node 
{
    ElemType data;           // data field 
    struct L_node *next;     // pointer field
} LNode, *LinkedList;

// Initialize a singly linked list L with head node
Status InitList_L(LinkedList *L) 
{
    // Allocate memory for head node
	(*L) = (LinkedList)malloc(sizeof(LNode));
	if (!(*L)) return ERROR;
	(*L)->next = NULL;
	return OK; 
}

// 输入若干整数(以输入-1作为结束条件,-1不包含在单链表中),用递归方法创建一个不带头结点的单链表。
Status CreateList_L(LinkedList L) 
{
    ElemType input;
	scanf("%d", &input);
	if (input == -1) {
        // Set the next pointer of the last node to NULL
		L->next = NULL;
		return OK;
	}
    // Allocate memory for the next node
	L->next = (LinkedList)malloc(sizeof(LNode));
	if (!L->next) return ERROR;
    // Assign input value to the data field of the next node
	L->next->data = input;
    // Recursively create the next node
	CreateList_L(L->next);
	return OK;
}

// Print the elements in a list
void LinkedListPrint(LinkedList L) 
{
    // Base case: If the next node is NULL, return
	if (!L->next) return;
    // Print the data of the next node
	printf("%d ", L->next->data); 
    // Recursively print the rest of the list
	LinkedListPrint(L->next);
}

int main() 
{   
	LinkedList La;
	
    // Initialize the linked list
	InitList_L(&La);
	
    // Create the linked list using recursion
	CreateList_L(La);
	
    // Print the linked list
	LinkedListPrint(La);
	
	return 0;
}

3. 递归实验–汉诺塔
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int step = 0;

//移动函数
void move(int n, char *A, char *B)
{
    printf("将%d从%c移动到%c.\n", n, *A, *B);
    step++;
    return;
}

//将n层汉诺塔从A移动到C,B作为辅助
void Hanoi(int n, char *A, char *B, char *C)
{
   if (n == 1)    move(n, A, C); // 只有一层时直接从 A 移动到 C
   else 
   {
        Hanoi(n - 1, A, C, B); // 将 n-1 层汉诺塔从 A 移动到 B,以 C 为辅助
        move(n, A, C); // 将第 n 层从 A 移动到 C
        Hanoi(n - 1, B, A, C); // 将 n-1 层汉诺塔从 B 移动到 C,以 A 为辅助
    }
}

int main()
{
    int n;
    char axle[3] = {'A', 'B', 'C'};

    printf("输入层数n:");
    scanf("%d", &n);

    Hanoi(n, axle, axle + 1, axle + 2);

    printf("移完%d层汉诺塔所用总步数为%d\n", n, step);
    return 0;
}

4. 递归实现单链表的反转
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define ERROR 0
#define OK 1

typedef struct L_Node
{
	int data;
	struct L_Node *next;
}LNode,*LinkList;

void InitList_L(LinkList* L)
{
	(*L) = (LinkList)malloc(sizeof(LNode));
	if(!(*L))	return ;
	(*L)->next = NULL;
}

void CreateList_L(LinkList L)
{
	int input;
	if(scanf("%d",&input) == EOF)	
	{
		L->next = NULL;
		return ;
	}	
	L->next = (LinkList)malloc(sizeof(LNode));
	if(!L->next)	return ;
	L->next->data = input;
	
	CreateList_L(L->next);
}

void reverse_List(LinkList L)
{
    if (L == NULL || L->next == NULL || L->next->next == NULL) {
        return;
    }

    // 递归反转除头结点之外的部分
    reverse_List(L->next);

    // 找到反转后的尾节点
    LNode *tail = L->next;
    while (tail->next != NULL) 
	{
        tail = tail->next;
    }

    // 将头结点后的节点(反转后的尾节点)移到头结点之前
    tail->next = L->next;
    L->next = L->next->next;
    tail->next->next = NULL;
}


void LinkListPrint(LinkList L)
{
	if(!L->next)	return ;
	printf("%d ",L->next->data);
	LinkListPrint(L->next);
}


int main()

{

    int n;
	
	scanf("%d",&n);
	
	LinkList La;
	
	InitList_L(&La);
	
	CreateList_L(La);
	
	reverse_List(La);
	
	LinkListPrint(La);

    return 0;

}
5. 递归实现求链表中的最大整数
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

typedef struct	L_Node
{
	int data;
	struct L_Node *next; 
}LNode,*LinkList;

void InitList_L(LinkList *L)
{
	(*L) = (LinkList)malloc(sizeof(LNode));
	if(!(*L))	return ;
	(*L)->next = NULL;
}

void CreateList_L(LinkList L,int n)
{
	LNode *p = L; 
	
	int arr,i;
	for(i = 0;i < n;i ++)
	{
		scanf("%d",&arr); 
		LNode *r ;
		r = (LinkList)malloc(sizeof(LNode));
		if(!r)	return ;
		r->data = arr;
		r->next = NULL;
		p->next = r;
		p = p->next;
	 } 
}

int find_max(LinkList L)
{
	if(!L|| !L->next)	return 0;
	if(!L->next->next)	return L->next->data;
	return L->next->data >= find_max(L->next) ? L->next->data : find_max(L->next);
}


int main(int argc, char *argv[]) 
{
	int n;
	scanf("%d",&n);
	
	LinkList L;
	
	InitList_L(&L);
	
	CreateList_L(L,n);
	
	printf("%d",find_max(L));

	return 0;
}
6. 递归实现求链表的结点个数
#include <stdio.h>
#include <stdlib.h>

typedef struct L_Node
{
	int data;
	struct L_Node *next;
}LNode,*LinkList;

void InitList_L(LinkList *L)
{
	(*L) = (LinkList)malloc(sizeof(LNode));
	if(!(*L))	return ;
	(*L)->next = NULL; 
}

void CreateList_L(LinkList L,int n) 
{
	int i ,arr;
	LNode *p = L;
	for(i = 0;i < n; i ++)
	{
		scanf("%d",&arr);
		LNode *r;
		r = (LinkList)malloc(sizeof(LNode));
		r->data = arr;
		r->next = NULL;
		p->next = r;
		p = p->next;
	}
}

int Count_Node(LinkList L)
{
	if(!L->next)	return 0;
	return 1 + Count_Node(L->next);	
} 


int main(int argc, char *argv[]) 
{
	int n;
	scanf("%d",&n);
	
	LinkList L;
	
	InitList_L(&L);
	
	CreateList_L(L,n);
	
	printf("%d",Count_Node(L));
	
	return 0;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值