Java学习第37天:十字链表

概念:

十字链表是为了便于求得图中顶点的度(出度和入度)而提出来的。它是综合邻接表和逆邻接表形式的一种链式存储结构。

在十字链表存储结构中,有向图中的顶点的结构如下所示:

 其中data表示顶点的具体数据信息,而firstIn则表示指向以该顶点为弧头的第一个弧节点。而firstOut则表示指向以该顶点为弧尾的第一个弧节点。为了表示有向图中所有的顶点,采用一个顶点数组存储每一个结点,如下图所示。

 

从十字链表的数据结构来看,每一个顶点对应两个链表:以该顶点为弧尾的弧结点所组成的链表以及以该顶点为弧头的弧结点所组成的链表。

如下图所示的一个有向图:

 

代码如下:

package day37;

public class OrthogonalList {
	class OrthogonalNode {

        //行数
        int row;

        //列数
        int column;

        //出节点
        OrthogonalNode nextOut;

        //入节点
        OrthogonalNode nextIn;

        public OrthogonalNode(int row, int column) {
            this.row = row;
            this.column = column;
            nextIn = null;
            nextOut = null;
        }
    }

    //节点的数量
    int numNodes;

    //每行的起始节点
    OrthogonalNode[] headers;

    public OrthogonalList(int[][] paraMatrix) {
        numNodes = paraMatrix.length;

        //第一步初始化。头节点中数据是无特殊意义的
        OrthogonalNode tpPreNode, tpNode;

        headers = new OrthogonalNode[numNodes];

        //第二步链接出节点
        for (int i = 0; i < numNodes; i++) {
            headers[i] = new OrthogonalNode(i, -1);
            tpPreNode = headers[i];
            for (int j = 0; j < numNodes; j++) {
                if (paraMatrix[i][j] == 0) {
                    continue;
                }

                tpNode = new OrthogonalNode(i, j);

                tpPreNode.nextOut = tpNode;
                tpPreNode = tpNode;
            }
        }

        //第三步链接入节点
        OrthogonalNode[] tpColumnNodes = new OrthogonalNode[numNodes];
        for (int i = 0; i < numNodes; i++) {
            tpColumnNodes[i] = headers[i];
        }

        for (int i = 0; i < numNodes; i++) {
            tpNode = headers[i].nextOut;
            while(tpNode!=null){
                tpColumnNodes[tpNode.column].nextIn = tpNode;
                tpColumnNodes[tpNode.column] = tpNode;

                tpNode = tpNode.nextOut;
            }
        }
    }


    public String toString() {
        String resultString = "Out arcs: \n";

        OrthogonalNode tempNode;
        for (int i = 0; i < numNodes; i++) {
            tempNode = headers[i].nextOut;

            while (tempNode != null) {
                resultString += " (" + tempNode.row + ", " + tempNode.column + ")";
                tempNode = tempNode.nextOut;
            }
            resultString += "\r\n";
        }

        resultString += "\r\nIn arcs: \n";

        for (int i = 0; i < numNodes; i++) {
            tempNode = headers[i].nextIn;

            while (tempNode != null) {
                resultString += " (" + tempNode.row + ", " + tempNode.column + ")";
                tempNode = tempNode.nextIn;
            }
            resultString += "\r\n";
        }

        return resultString;
    }

    public static void main(String args[]) {
        int[][] tempMatrix = { { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 1, 0, 0, 0 }, { 0, 1, 1, 0 } };
        OrthogonalList tempList = new OrthogonalList(tempMatrix);
        System.out.println("The data are:\r\n" + tempList);
    }
}

 运行结果:

The data are:
Out arcs: 
 (0, 1)
 (1, 3)
 (2, 0)
 (3, 1) (3, 2)

In arcs: 
 (2, 0)
 (0, 1) (3, 1)
 (3, 2)
 (1, 3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值