链表c语言程序计算结点个数(试写一算法 统计单链表中元素个数 数据结构一塌糊涂 用C语言写算法 求解)

本文目录
- 试写一算法 统计单链表中元素个数 数据结构一塌糊涂 用C语言写算法 求解
- C语言:设计一个统计单链表中数据值为D的结点个数
- 用C语言编写:建立一棵以二叉链表结构存储的二叉树,并对其进行遍历求该二叉树中的结点个数等操作
- 如何用类c语言计算带头结点的单链表中的节点个数
- 用C语言定义二叉树的二叉链表存储结构,完成二叉树的建立,先序中序后序遍历的操作,求所有叶子结点总数
- C语言,计算链表中元素节点个个数
试写一算法 统计单链表中元素个数 数据结构一塌糊涂 用C语言写算法 求解
见下列代码,size()函数返回元素个数。这里省略l了插入和删除
#include"iostream.h"
struct Node
{
int Data;
Node*next;
};
class list
{
Node*head;
public:
list(){head=NULL;}
int size();
};
int list::size()
{
int i=0;
Node*current=head;
while(current!=NULL)
{
i++;
current=current-》next;
return i;
}
}
C语言:设计一个统计单链表中数据值为D的结点个数
int count(LinkList head, Data D) {
int count = 0;
Node *node = head;
while(node!= NULL) {
if(node-》data==D)
count++;
node = node-》next;
}
return count;
}
用C语言编写:建立一棵以二叉链表结构存储的二叉树,并对其进行遍历求该二叉树中的结点个数等操作
存储结构
typedef struct {
int weight;
int parent, lchild, rchild;
} HTNode ,*HuffmanTree; // 动态分配数组存储huffman树
算法设计
void createHuffmantree(){
ht=(HuffmanTree)malloc(m+1)*sizeof(HTNode);// 动态分配数组存储huffman树,0号单元未用
// m:huffman 树中的结点数(m=2*n-1)
for (i=1;i《=m;++i)
ht-》rch=0;
for (i=1;i《=n;++i)
ht:n个叶子的权值
for (i=n+1;i《=m,++i) { //建哈夫曼树
select(i-1),s1,s2); //在ht(1《=k《=i-1)中选择两个双亲域为零而权值取最小的结点 :s1和s2
ht.parent=i;
ht.lch=s1;
ht.rch=s2;
ht.weight ;
};
}
如何用类c语言计算带头结点的单链表中的节点个数
#include《stdio.h》
#include《stdlib.h》
typedef struct node
{
int data;
struct node *next;
}node;
void count(node* l)//计算节点个数,输出所有值
{
int n = 0;
node* p = l-》next;
while(p)
{
printf("%d ",p-》data);
p = p-》next;
n++;
}
printf("\n%d\n",n);
}
int main()
{
int e;
//头节点
node *head,*p,*q;
head = (node*)malloc(sizeof(node));
head-》next = NULL;
p = head;
printf("输入元素,回车结束:");
do{
scanf("%d",&e);
q = (node*)malloc(sizeof(node));
q-》data = e;
q-》next = NULL;
p-》next = q;
p = q;
}while(getchar()!=’\n’);
count(head);
return 0;
}
用C语言定义二叉树的二叉链表存储结构,完成二叉树的建立,先序中序后序遍历的操作,求所有叶子结点总数
#include《stdio.h》
#include《malloc.h》
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *lchild,*rchild;
}LNode,*TLNode;
void create(TLNode * Tree){ //创建
ElemType e;
scanf("%d",&e);
if(e==0)
*Tree=NULL;
else{
(*Tree)=(TLNode)malloc(sizeof(LNode));
(*Tree)-》data=e;
printf("input %d lchild: ",e);
create(&(*Tree)-》lchild);
printf("input %d rchild: ",e);
create(&(*Tree)-》rchild);
}
}
void print1(TLNode Tree){ //先序遍历
if(Tree!=NULL){
printf("%d-",Tree-》data);
print1(Tree-》lchild);
print1(Tree-》rchild);
}
}
void print2(TLNode Tree){ //中序遍历
if(Tree!=NULL){
print2(Tree-》lchild);
printf("%d-",Tree-》data);
print2(Tree-》rchild);
}
}
void print3(TLNode Tree){ //后序遍历
if(Tree!=NULL){
print3(Tree-》lchild);
print3(Tree-》rchild);
printf("%d-",Tree-》data);
}
}
int leaf=0; //求叶子节点数
int depth(TLNode Tree){ //深度
int s1,s2;
if(Tree==NULL)
return 0;
else{
s1=depth(Tree-》lchild);
s2=depth(Tree-》rchild);
if(s1==0 && s2==0) leaf++;
return (s1》s2?s1:s2)+1;
}
}
int Cnode(TLNode Tree){ //总结点
int s1,s2;
if(Tree==NULL)
return 0;
else{
s1=Cnode(Tree-》lchild);
s2=Cnode(Tree-》rchild);
return s1+s2+1;
}
}
void main(){
TLNode Tree;
printf("input 根节点: ");
create(&Tree);
printf("先序遍历:");
print1(Tree);
printf("中序遍历");
print2(Tree);
printf("后序遍历");
print3(Tree);
printf("\n深 度:%d \n",depth(Tree));
printf("总结点数:%d \n",Cnode(Tree));
printf("叶子结点数:%d\n",leaf);
}
C语言,计算链表中元素节点个个数
楼主你好~
很简单哦
int
count
=
0;
while(p
!=
NULL)
{
p
=
p-》next;
count++;
}
这个count就是最终的结点个数(包括头结点)~
请追问~

更多文章:
ios开发技术(想问下做ios平台的软件开发,需要那些基础知识(ios软件开发需要学什么))
2025年10月15日 07:30
struts2框架编程(如何学习 struts2 框架对于android编程,有没有什么建议)
2026年6月2日 21:30
cssposition(css的position的属性有哪些)
2025年8月26日 06:45
一对一直播源码开发,即时通讯技术实现有哪几种选择?求QQ 智能 自动聊天 机器人 易语言源码 !最好是能在QQ群里用的 ,能自动
2025年11月30日 18:00
二郎神杨戬动漫电影(电影《新神榜:杨戬》曝先导海报,这部电影讲述的什么故事)
2025年9月2日 08:30
calendarprovider能删除吗(华为手机荣耀7哪些东西可以删除吗)
2026年3月27日 14:45













