首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >js实现图的构造

js实现图的构造

作者头像
theanarkh
发布于 2019-03-06 10:58:59
发布于 2019-03-06 10:58:59
2.6K0
举报
文章被收录于专栏:原创分享原创分享
代码语言:javascript
复制
function Edge(data) {
        this.src = data[0];
        this.des = data[1];
        this.weight = data[2];
        this.next = null;
    }

    function Node(val) {
        this.val = val;
        this.next = null;
    }
    var graph = {};

    function makeGraph() {
        var data = [[0,1,2],[1,2,3],[3,4,11],[2,4,21],[4,3,12],[4,5,111]];
        for (var i = 0; i < data.length; i++) {
            var current = data[i];
            var edge = new Edge(current);
            if (graph[current[0]]) {
                edge.next = graph[current[0]].next;
                graph[current[0]].next = edge;
            } else {
                graph[current[0]] = new Node(current[0]);
                graph[current[0]].next = edge;
                edge.next = null;
            }
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-02-25,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档