Java实现十字链表度的计算

 //计算度
    public static int countDegreeByCrossLink(struct[] graph, char v, int n){//graph[]为给定定点顺序表,v为所有计算的顶点,n为定点个数
        int i ;
        arc p = null;//弧边
        int inDegree = 0, outDegree = 0;//初始化入度出度为0
        //找到指定的顶点
        for( i = 0; i < n; i++){
            if(graph[i].data == v){
                break;
            }
        }
        //没找到顶点
        if(i == n){
            return -1;
        }
        //p为以v为弧头的第一个弧边
        p = graph[i].firstin;
        while(p != null){
            outDegree ++;
            p = p.hlink;//以v为弧头的另一个出弧边
        }
        p = graph[i].firstout;//以v为弧尾的第一个弧边
        while(p != null){
            inDegree ++;
            p = p.tlink;//以v为弧尾的另一个弧边
        }
        return (inDegree + outDegree);
    }

猜你喜欢

转载自blog.csdn.net/weixin_43408956/article/details/89432878