图之十字链表

十字链表是有向图的一种链式存储结构;
弧结点:对应于有向图中的弧,结点结构如下:

tailvexheadvexhlinktlinkinfo

其中,尾域(tailvex)和头域(headvex)分别指示弧尾和弧头这两个顶点在图中的位置;链域hlink指向弧头相同的下一条弧,链域tlink指向弧尾相同的下一条弧;info指示与该弧相关的信息,如权重;

顶点结点:对应于有向图中的顶点,结点结构如下:

datafirstinfirstout

其中,数据域(data)存储和顶点相关的信息,如顶点名称等;firstin和firstout两链域分别指示以该顶点为弧头或弧尾的第一个弧结点;
优点:便于找到入度和出度,且建表的时间复杂度较低;

下面给出图的十字链表的Java实现:

package org.sky.graph;

import java.util.Scanner;

/**
 * @Description 利用十字链表表示有向图
 * @author sky
 * @date 2016/12/29
 */
public class OrthogonalListGragh {
    //弧结点,包含弧相关的信息
    @SuppressWarnings("unused")
    private static class ArcBox{
        int tailVex;        //尾域,指示弧尾顶点在图中的位置
        int headVex;        //头域,指示弧头顶点在图中的位置
        ArcBox headLink;    //弧头链域,指示弧头相同的下一条弧
        ArcBox tailLink;    //弧尾链域,指示弧尾相同的下一条弧
        int value;          //与当前弧相关的信息,如权重

        public ArcBox(int tailVex, int headVex, ArcBox headLink,
                ArcBox tailLink, int value) {
            super();
            this.tailVex = tailVex;
            this.headVex = headVex;
            this.headLink = headLink;
            this.tailLink = tailLink;
            this.value = value;
        }

        public int getTailVex() {
            return tailVex;
        }

        public void setTailVex(int tailVex) {
            this.tailVex = tailVex;
        }

        public int getHeadVex() {
            return headVex;
        }

        public void setHeadVex(int headVex) {
            this.headVex = headVex;
        }

        public ArcBox getHeadLink() {
            return headLink;
        }

        public void setHeadLink(ArcBox headLink) {
            this.headLink = headLink;
        }

        public ArcBox getTailLink() {
            return tailLink;
        }

        public void setTailLink(ArcBox tailLink) {
            this.tailLink = tailLink;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }


    }

    @SuppressWarnings("unused")
    private static class VexNode{
        Object data;        //顶点信息
        ArcBox firstIn;     //指向该顶点的第一条入弧
        ArcBox firstOut;    //指向该顶点的第一条出弧

        public VexNode(Object data) {
            super();
            this.data = data;
        }

        public VexNode(Object data, ArcBox firstIn, ArcBox firstOut) {
            super();
            this.data = data;
            this.firstIn = firstIn;
            this.firstOut = firstOut;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public ArcBox getFirstIn() {
            return firstIn;
        }

        public void setFirstIn(ArcBox firstIn) {
            this.firstIn = firstIn;
        }

        public ArcBox getFirstOut() {
            return firstOut;
        }

        public void setFirstOut(ArcBox firstOut) {
            this.firstOut = firstOut;
        }


    }

    private int numVex;     //图的当前顶点数
    private int numArc;     //图的当前弧数

    private VexNode[] vexs; //存储表头结点,通常采用顺序存储结构

    public OrthogonalListGragh(int numVex, int numArc, VexNode[] vexs) {
        super();
        this.numVex = numVex;
        this.numArc = numArc;
        this.vexs = vexs;
    }

    /**
     * @Desciption 采用十字链表存储表示,构造有向图
     */ 
    public void createDG(){   
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        System.out.println("请分别输入图的顶点数及边数");
        numVex = sc.nextInt();
        numArc = sc.nextInt();
        vexs = new VexNode[numVex];
        System.out.println("请分别输入图的各个顶点:");
        for(int v = 0; v < numVex; v++){    //构造表头向量
            vexs[v] = new VexNode(sc.next());
            vexs[v].firstIn = null;
            vexs[v].firstOut = null;
        }
        System.out.println("请输入各个边的起始顶点u、v及其权值value");
        for(int k = 0; k < numArc; k++){    //输入各弧,并构造十字链表
            int v = locateVex(sc.next());   //输入一条弧的始点,即弧尾
            int u = locateVex(sc.next());   //输入一条弧的终点,即弧头
            int value = sc.nextInt();       //输入弧的权重
            ArcBox p = new ArcBox(v, u, vexs[u].firstIn, vexs[v].firstOut, value); //构造弧结点
            vexs[u].firstIn = vexs[v].firstOut = p;     //在顶点结点完成入弧和出弧连头的插入
        }
    }

    /**
     * @Desciption 返回与传入值相对应的定点位置
     */ 
    public int locateVex(Object vex){
        for(int v = 0; v < numVex; v++){
            if(vexs[v].getData().equals(vex)){
                return v;
            }
        }
        return -1;
    }   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值