• 数据结构(C语言)关于树、二叉树、图的基本操作。


    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);
    18         }
    19         else
    20         {
    21             printf("%d
    ",n);
    22         }
    23     }
    24 }
  • 相关阅读:
    lvs_基础理论
    iptables_表和链(Traversing of tables and chains)
    题解-【集训队作业2018】Simple Tree
    题解-CF559C
    题解-[Violet]天使玩偶/SJY摆棋子
    题解-[POI2014]PRZ-Criminals
    题解-CF961G
    题解-CF1392H
    WorldCreator基础流程
    gstreamer-vaapi 之 README
  • 原文地址:https://www.cnblogs.com/Dark-King/p/7989190.html
Copyright © 2020-2023  润新知