设为首页 加入收藏

TOP

数据结构(C语言)关于树、二叉树、图的基本操作。
2018-10-21 18:11:10 】 浏览:205
Tags:数据结构 语言 关于 基本操作

1) 编写算法函数int equal(tree t1, tree t2),判断两棵给定的树是否等价;

 

 1  int equal(tree t1,tree t2)
 2  {
 3     int k;
 4         if(t1==NULL&&t2==NULL)
 5         return TRUE;
 6     else if(t1!=NULL&&t2==NULL||t1==NULL&&t2!=NULL)
 7              {
 8               return FALSE;
 9          }
10           else if(t1->data!=t2->data)
11           {
12               return FALSE;
13           }
14           for(k=0;k<m;k++)
15           {
16               equal(t1->child[k],t2->child[k]);
17               if(equal(t1->child[k],t2->child[k])==FALSE)
18               {
19                   return FALSE;
20               }
21               else
22                   return TRUE;
23           }
24   }    

 

2) 编写算法函数void preorder(bintree t)实现二叉树t的非递归前序遍历;

 

 1 void preorder1(bintree t)
 2 {
 3     seqstack s;
 4     init(&s);
 5     while(t||!empty(&s))
 6     {
 7         if(t)
 8         {
 9             printf("%c",t->data);
10             push(&s,t);
11             t=t->lchild;
12         }
13         else if(!empty(&s))
14         {
15             t=pop(&s);
16             t=t->rchild;
17         }
18     }

 

3)编写算法函数degree(LinkedGraph g)输出以邻接表为存储结构的无向图的各顶点的度。

 1 void degree(LinkedGraph g)
 2 {
 3     int k;
 4     int n;
 5     EdgeNode *p;
 6     for(k=0;k<g.n;k++)
 7     {
 8         p=g.adjlist[k].FirstEdge;
 9         n=0;
10         while(p!=NULL)
11         {
12             n++;
13             p=p->next;
14         }
15         if(k==0)
16         {
17             printf("%d\n",n);
18         }
19         else
20         {
21             printf("%d\n",n);
22         }
23     }
24 }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言第七次博客作业--一二维数组 下一篇C指针和数组

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目