示例#1
0
    public int GetNumOfEdge()
    {
        int i = 0;

        foreach (var nd in adjList)
        {
            adjListNode <T> p = nd.FirstAdj;
            while (p != null)
            {
                i++;
                p = p.NextAdj;
            }
        }

        return(i / 2);
    }
示例#2
0
    private void DFS(int i)
    {
        visited[i] = true;

        Debug.Log(adjList[i].Data.Data);

        adjListNode <T> p = adjList[i].FirstAdj;

        while (p != null)
        {
            if (visited[p.Adjvex] == false)
            {
                DFS(p.Adjvex);
            }

            p = p.NextAdj;
        }
    }
示例#3
0
    //邻接表广度搜索
    public void BFSTravel()
    {
        int tmp;

        Queue q = new Queue();

        for (int i = 0; i < GetNumOfVertex(); i++)
        {
            visited[i] = false;
        }

        for (int i = 0; i < GetNumOfVertex(); i++)
        {
            if (!visited[i])
            {
                visited[i] = true;

                Debug.Log(adjList[i].Data.Data);

                q.Enqueue(i);

                while (q.Count > 0)
                {
                    tmp = (int)q.Dequeue();

                    adjListNode <T> p = adjList[tmp].FirstAdj;

                    while (p != null)
                    {
                        if (!visited[p.Adjvex])
                        {
                            visited[p.Adjvex] = true;

                            Debug.Log(adjList[p.Adjvex].Data.Data);

                            q.Enqueue(p.Adjvex);
                        }

                        p = p.NextAdj;
                    }
                }
            }
        }
    }
示例#4
0
    public int GetEdge(Node <T> v1, Node <T> v2)
    {
        if (!IsNode(v1) || !IsNode(v2))
        {
            return(0);
        }

        adjListNode <T> p = adjList[GetIndex(v1)].FirstAdj;

        while (p != null)
        {
            if (p.Adjvex == GetIndex(v2))
            {
                return(p.Info);
            }

            p = p.NextAdj;
        }

        return(0);
    }
示例#5
0
    public int GetEdge(int index1, int index2)
    {
        if (!IsNode(this.GetNode(index1)) || !IsNode(this.GetNode(index2)))
        {
            return(0);
        }

        adjListNode <T> p = adjList[index1].FirstAdj;

        while (p != null)
        {
            if (p.Adjvex == index2)
            {
                return(p.Info);
            }

            p = p.NextAdj;
        }

        return(0);
    }
示例#6
0
    public bool IsEdge(Node <T> v1, Node <T> v2)
    {
        if (!IsNode(v1) || !IsNode(v2))
        {
            return(false);
        }

        adjListNode <T> p = adjList[GetIndex(v1)].FirstAdj;

        while (p != null)
        {
            if (p.Adjvex == GetIndex(v2))
            {
                return(true);
            }

            p = p.NextAdj;
        }

        return(false);
    }
示例#7
0
    public void DelEdge(Node <T> v1, Node <T> v2)
    {
        if (!IsNode(v1) || !IsNode(v2))
        {
            return;
        }

        if (IsEdge(v1, v2))
        {
            adjListNode <T> p   = adjList[GetIndex(v1)].FirstAdj;
            adjListNode <T> pre = null;
            while (p != null)
            {
                if (p.Adjvex != GetIndex(v2))
                {
                    pre = p;
                    p   = p.NextAdj;
                }
            }

            pre.NextAdj = p.NextAdj;

            p = adjList[GetIndex(v2)].FirstAdj;

            pre = null;

            while (p != null)
            {
                if (p.Adjvex != GetIndex(v1))
                {
                    pre = p;
                    p   = p.NextAdj;
                }
            }

            pre.NextAdj = p.NextAdj;
        }
    }
示例#8
0
    public void SetEdge(Node <T> v1, Node <T> v2, int v)
    {
        if (!IsNode(v1) || !IsNode(v2) || IsEdge(v1, v2))
        {
            return;
        }

        if (v == 0)
        {
            return;
        }

        adjListNode <T> p = new adjListNode <T>(GetIndex(v2), v);

        if (adjList[GetIndex(v1)].FirstAdj == null)
        {
            adjList[GetIndex(v1)].FirstAdj = p;
        }

        else
        {
            p.NextAdj = adjList[GetIndex(v1)].FirstAdj;
            adjList[GetIndex(v1)].FirstAdj = p;
        }

        p = new adjListNode <T>(GetIndex(v1), v);

        if (adjList[GetIndex(v2)].FirstAdj == null)
        {
            adjList[GetIndex(v2)].FirstAdj = p;
        }

        else
        {
            p.NextAdj = adjList[GetIndex(v2)].FirstAdj;
            adjList[GetIndex(v2)].FirstAdj = p;
        }
    }
示例#9
0
 public VexNode(Node <T> nd, adjListNode <T> alNode)
 {
     data     = nd;
     firstadj = alNode;
 }
示例#10
0
 public VexNode(Node <T> nd)
 {
     data     = nd;
     firstadj = null;
 }
示例#11
0
 public adjListNode(int adjvex, int info)
 {
     this.adjvex = adjvex;
     this.info   = info;
     nextadj     = null;
 }
示例#12
0
 public adjListNode(int adjvex)
 {
     this.adjvex = adjvex;
     nextadj     = null;
 }