//二叉树的二叉链表存储表示
typedef struct Node
{
DataType data;
strct Node * LChild;
struct Node * RChild;
}BiTNode, *LPTREE ;
请编写递归算法,计算二叉树中所有叶子结点数。
int TreeSearch(LPTREE root)
{
if (root)
{
return 0;
}
else if(root->lchild==NULL&&root->rchild==NULL)
{
return 1;
}
else
{
return TreeSearch(root->lchild)+TreeSearch(root->rchild);
}
}
————链表
//单链表的存储表示
typedef struct Node{
ElemType data; //数据域
struct Node* next; //指针域
}Node, *LPNODE ;
设计一算法,统计带表头结点的单链表中等于定值x的元素个数。
int sllcount(LPNODE l,int e)
{
LPNODE p=l->next;
int t=0;
while(p)
{
if(p->data==e)
{
t++;
}
p=p->next;
}
printf("%d",t);
return t;
}
设计一算法,对单链表实现逆置。
//单链表存储结构
typedef struct LNode
{ ElemType data; //数据域
struct Lnode *next; //指针域
}LNode,*LPNODE ;
void sllnizhi(LPNODE l,int n)
{
LPNODE p=l;
l->next=NULL;
LPNODE q;
while(p->next)
{
q=p->next;
q->next=l->next;
l->next=q;
p=p->next;
}
}
3.带头结点的单链表的存储结构为:
typedef struct LNode
{ ElemType data; //数据域
struct LNode *next; //指针域
}LNode, *LPNODE ;
线性表操作说明:
LENGTH(L)
初始条件:线性表L已存在。
操作结果:返回L中数据元素个数。
写一算法,在带头结点的单链表结构上实现线性表操作LENGTH(L)。
int chazhao(LPNODE l)
{
int t=0;
LPNODE p=l->next;
while(p)
{
t++;
p=p->next;
}
return t;
}