Exemple #1
0
        //构造器
        public GraphAdjList(GraphAdjList <T> newGraph)
        {
            adjList = new VexNode <T> [newGraph.GetNumOfVertex()];
            adjListNode <T> pre, p;

            for (int i = 0; i < newGraph.GetNumOfVertex(); ++i)
            {
                adjList[i]           = new VexNode <T>();
                adjList[i].Data      = new Node <T>(newGraph.adjList[i].Data.Data);
                adjList[i].Indegree  = newGraph.adjList[i].Indegree;
                adjList[i].Outdegree = newGraph.adjList[i].Outdegree;

                if (newGraph.adjList[i].FirstAdj == null)
                {
                    adjList[i].FirstAdj = null;
                }
                else
                {
                    adjListNode <T> GraphP = newGraph.adjList[i].FirstAdj;
                    pre = new adjListNode <T>(GraphP.Adjvex, GraphP.Weight);
                    adjList[i].FirstAdj = pre;
                    while (GraphP.Next != null)
                    {
                        p        = new adjListNode <T>(GraphP.Next.Adjvex, GraphP.Next.Weight);
                        pre.Next = p;
                        pre      = p;
                        GraphP   = GraphP.Next;
                    }
                    pre.Next = GraphP.Next;//null
                }
            }
        }
Exemple #2
0
 //构造器
 public GraphAdjList(Node <T> node)
 {
     adjList              = new VexNode <T> [1];
     adjList[0]           = new VexNode <T>();
     adjList[0].Data      = node;
     adjList[0].FirstAdj  = null;
     adjList[0].Indegree  = 0;
     adjList[0].Outdegree = 0;
 }
Exemple #3
0
 //构造器
 public GraphAdjList(Node <T>[] nodes)
 {
     adjList = new VexNode <T> [nodes.Length];
     for (int i = 0; i < nodes.Length; ++i)
     {
         adjList[i]           = new VexNode <T>();
         adjList[i].Data      = nodes[i];
         adjList[i].FirstAdj  = null;
         adjList[i].Indegree  = 0;
         adjList[i].Outdegree = 0;
     }
 }
Exemple #4
0
        //添加顶点
        public bool SetNode(Node <T> oldV)
        {
            //v已经在图中
            if (IsNode(oldV))
            {
                //Console.WriteLine("Node has been belong to Graph!");
                return(false);
            }

            Node <T> v = new Node <T>(oldV.Data);

            VexNode <T>[] oldAdjList = adjList;
            if (oldAdjList == null)
            {
                adjList = new VexNode <T> [1];
            }
            else
            {
                adjList = new VexNode <T> [adjList.Length + 1];
            }

            //原表复制
            if (oldAdjList != null)
            {
                for (int i = 0; i < oldAdjList.Length; i++)
                {
                    adjList[i]           = new VexNode <T>();
                    adjList[i].Data      = oldAdjList[i].Data;
                    adjList[i].FirstAdj  = oldAdjList[i].FirstAdj;
                    adjList[i].Indegree  = oldAdjList[i].Indegree;
                    adjList[i].Outdegree = oldAdjList[i].Outdegree;
                }
            }

            //添加新点
            adjList[adjList.Length - 1]           = new VexNode <T>();
            adjList[adjList.Length - 1].Data      = v;
            adjList[adjList.Length - 1].FirstAdj  = null;
            adjList[adjList.Length - 1].Indegree  = 0;
            adjList[adjList.Length - 1].Outdegree = 0;

            return(true);
        }
Exemple #5
0
        //删除顶点
        public bool DelNode(Node <T> v)
        {
            //v不在图中
            if (!IsNode(v))
            {
                //Console.WriteLine("Node isn't belong to Graph!");
                return(false);
            }

            int index = this.GetIndex(v);

            //修改原表,删除有关v的边
            for (int i = 0; i < adjList.Length; i++)
            {
                if (i == index)
                {
                    continue;
                }

                //从adjlist[i]到v有弧
                if (IsEdge(adjList[i].Data, v))
                {
                    DelEdge(adjList[i].Data, v);
                }
                //从v到adjlist[i]有弧
                if (IsEdge(v, adjList[i].Data))
                {
                    DelEdge(v, adjList[i].Data);
                }
            }

            VexNode <T>[] oldAdjList = adjList;
            adjList = new VexNode <T> [adjList.Length - 1];

            //原表复制
            for (int i = 0; i < index; i++)
            {
                adjList[i]           = new VexNode <T>();
                adjList[i].Data      = oldAdjList[i].Data;
                adjList[i].FirstAdj  = oldAdjList[i].FirstAdj;
                adjList[i].Indegree  = oldAdjList[i].Indegree;
                adjList[i].Outdegree = oldAdjList[i].Outdegree;
                //修改邻接表
                for (adjListNode <T> p = adjList[i].FirstAdj; p != null; p = p.Next)
                {
                    if (p.Adjvex > index)
                    {
                        p.Adjvex--; //当p的索引比删掉点的索引大,其值减一
                    }
                }
            }
            for (int i = index; i < adjList.Length; i++)
            {
                adjList[i]           = new VexNode <T>();
                adjList[i].Data      = oldAdjList[i + 1].Data;
                adjList[i].FirstAdj  = oldAdjList[i + 1].FirstAdj;
                adjList[i].Indegree  = oldAdjList[i + 1].Indegree;
                adjList[i].Outdegree = oldAdjList[i + 1].Outdegree;
                //修改邻接表
                for (adjListNode <T> p = adjList[i].FirstAdj; p != null; p = p.Next)
                {
                    if (p.Adjvex > index)
                    {
                        p.Adjvex--; //当p的索引比删掉点的索引大,其值减一
                    }
                }
            }

            return(true);
        }