本科实验报告 课程名称: 数据结构 实验项目: 树形结构 实验地点: 迎西校区逸夫楼3 0 2 专业班级:软件 1 1 0 9 学号: 2 0 1 1 0 0 4 8 7 2 学生姓名: 栗永春 指导教师: 牛之贤 年 月 日 树形结构 一、实验目的和要求 目的与要求 二、实验内容和原理 三、主要仪器设备 四、操作方法与实验步骤 列出调试通过的源程序。 习题 1: /********************************************************************* *1. 编写递归算法,计算二叉树中叶子结点的数目。 * *********************************************************************/ #include #include int count = 0; struct node{ char info; struct node *llink,*rlink; }; typedef struct node NODE; NODE *creat(){ char x; NODE *p; scanf("%c",&x); printf("%c",x); if(x!='.'){ p=(NODE *)malloc(sizeof(NODE)); p->info=x; p->llink=creat(); p->rlink=creat(); } else p=NULL; return p; } void run(NODE *t){ if(t){ run(t->llink); run(t->rlink); printf("%c",t->info); if( ((t->llink) == NULL) && ((t->rlink) == NULL)) count ++; } } void main() { NODE *T; printf("PLease input a tree:\n"); T=creat(); printf("\n"); if(!T) printf("This is a empty binary tree."); else { printf("The result of post travese is:\n "); run(T); } printf("总共有叶子节点数%d", count ); printf("\n"); } 习题 2: /********************************************************************* * 用单链表ha 存储多项式A(x )=a0+a1x1+a2x2+…+anxn(其中 aI 为非零系 * * 数),用单链表hb 存储多项式B(x )=b0+b1x1+b2x2+…+bmxm(其中 bj 为 * * 非零系数),要求计算 C(x )= A(x )+B(x ),结果存到单链表hc 中 * * 。试写出程序。 * *********************************************************************/ #include #include typedef struct dxs{ int a; struct dxs *next; }Dxs, *Dxss; //函数声明部分 void Structure( Dxss head, int n ); //多项式录入 void Show( Dxss head ); //显示多项式 void Add( Dxss he...