链表中元素的个数递归函数C语言
时间: 2024-01-28 18:05:48 浏览: 30
下面是链表中元素个数的递归函数的C语言实现:
```
int countList(struct ListNode* head) {
if(head == NULL) {
return 0;
} else {
return 1 + countList(head->next);
}
}
```
其中,`ListNode`为链表节点的结构体,包含`val`和`next`两个成员变量,`val`表示节点的值,`next`表示指向下一个节点的指针。`head`为链表头节点的指针,递归函数的作用是计算链表中节点的个数。如果链表为空,则返回0,否则返回1加上后面节点的个数。
相关问题
用数据结构c语言编程,构造二叉链表存储二叉树T,三种方式递归遍历T,递归计算T的深度,递归统计T中结点的个数,递归统计T中叶子结点的个数的具体算法
非常感谢您的提问!这是一个关于数据结构和算法的问题。我很乐意帮助您回答这个问题。
首先,让我们来了解一下二叉树和二叉链表的概念。
二叉树是一种树形结构,它的每个节点最多有两个子节点。
二叉链表是一种利用链式存储结构表示二叉树的方法,它的每个节点包括三个字段,分别是数据域、指向左子树的指针和指向右子树的指针。
构造二叉链表存储二叉树T的算法如下:
```c
typedef struct TreeNode *BiTree;
struct TreeNode {
int val;
BiTree left;
BiTree right;
};
BiTree createTree(void) {
int val;
scanf("%d", &val);
if (val == 0) {
return NULL; // 0 表示空节点
}
BiTree T = (BiTree) malloc(sizeof(struct TreeNode));
T->val = val;
T->left = createTree();
T->right = createTree();
return T;
}
```
其中,`createTree()` 函数会从标准输入中逐层递归读入每个节点的值,直至遇到0,表示递归结束。此时,该节点的值为空,并且其指针域均为空。
接下来,让我们来看看如何递归遍历T、计算T的深度、统计T中结点的个数以及统计T中叶子结点的个数。
先来看中序遍历、前序遍历和后序遍历的算法:
```c
// 中序遍历
void inorder(BiTree T) {
if (!T) {
return;
}
inorder(T->left);
printf("%d ", T->val);
inorder(T->right);
}
// 前序遍历
void preorder(BiTree T) {
if (!T) {
return;
}
printf("%d ", T->val);
preorder(T->left);
preorder(T->right);
}
// 后序遍历
void postorder(BiTree T) {
if (!T) {
return;
}
postorder(T->left);
postorder(T->right);
printf("%d ", T->val);
}
```
以上三个函数分别实现了中序遍历、前序遍历和后序遍历的递归算法。其中,`T` 表示当前子树的根节点。
计算T的深度的算法如下:
```c
int depth(BiTree T) {
if (!T) {
return 0;
}
int left_depth = depth(T->left);
int right_depth = depth(T->right);
return left_depth > right_depth ? left_depth + 1 : right_depth + 1;
}
```
统计T中结点的个数的算法如下:
```c
int countNodes(BiTree T) {
if (!T) {
return 0;
}
return countNodes(T->left) + countNodes(T->right) + 1;
}
```
统计T中叶子结点的个数的算法如下:
```c
int countLeaves(BiTree T) {
if (!T) {
return 0;
}
if (!T->left && !T->right) {
return 1;
}
return countLeaves(T->left) + countLeaves(T->right);
}
```
以上四个函数分别实现了递归遍历T、计算T的深度、统计T中结点的个数以及统计T中叶子结点的个数的算法。
希望以上回答对您有帮助。如果您有其他问题,欢迎继续提问。
c语言 构造利用二叉链表存储表示的二叉树 利用先序遍历创建一个二叉树 递归计算二叉树的深度和叶子结点的个数。
好的,下面是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 二叉树的结构体
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
// 先序遍历创建二叉树
void CreateBiTree(BiTree *T) {
char c;
scanf("%c", &c);
if (c == ' ') {
*T = NULL;
} else {
*T = (BiTree)malloc(sizeof(BiTNode));
(*T)->data = c;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
// 递归计算二叉树深度
int GetDepth(BiTree T) {
if (!T) {
return 0;
} else {
int left_depth = GetDepth(T->lchild);
int right_depth = GetDepth(T->rchild);
return (left_depth > right_depth ? left_depth : right_depth) + 1;
}
}
// 递归计算叶子节点个数
int GetLeafCount(BiTree T) {
if (!T) {
return 0;
} else if (!T->lchild && !T->rchild) {
return 1;
} else {
return GetLeafCount(T->lchild) + GetLeafCount(T->rchild);
}
}
int main() {
BiTree T;
printf("请按先序遍历的顺序输入二叉树(空格表示空节点):\n");
CreateBiTree(&T);
printf("二叉树的深度为:%d\n", GetDepth(T));
printf("叶子节点的个数为:%d\n", GetLeafCount(T));
return 0;
}
```
以上代码中,`BiTNode`是二叉树的结构体,包括数据域`data`和左右孩子指针`lchild`和`rchild`。`CreateBiTree`函数用于先序遍历创建二叉树。`GetDepth`函数用于递归计算二叉树的深度。`GetLeafCount`函数用于递归计算叶子节点个数。在`main`函数中,先调用`CreateBiTree`函数创建二叉树,然后调用`GetDepth`和`GetLeafCount`函数计算深度和叶子节点个数并输出结果。