예제 #1
0
 public void DelEdge(Node <T> v1, Node <T> v2)
 {
     if (!IsNode(v1) || !IsNode(v2))
     {
         Console.WriteLine("不是点");
         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.Next;
             }
         }
         pre.Next = p.Next;
         p        = adjList[GetIndex(v2)].FirstAdj;
         pre      = null;
         while (p != null)
         {
             if (p.Adjvex != GetIndex(v1))
             {
                 pre = p;
                 p   = p.Next;
             }
         }
         pre.Next = p.Next;
     }
 }
예제 #2
0
        public void SetEdge(Node <T> v1, Node <T> v2, int v)
        {
            //v1或v2不是图的顶点或者v1和v2之间存在边
            if (!IsNode(v1) || !IsNode(v2) || IsEdge(v1, v2))
            {
                Console.WriteLine("Node is not belong to Graph!");
                return;
            }
            //权值不对
            if (v != 1)
            {
                Console.WriteLine("Weight is not right!");
                return;
            }
            //处理顶点v1的邻接表
            adjListNode <T> p = new adjListNode <T>(GetIndex(v2))
            {
                Next = adjList[GetIndex(v1)].FirstAdj
            };

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

            //处理顶点v2的邻接表
            p = new adjListNode <T>(GetIndex(v1))
            {
                Next = adjList[GetIndex(v2)].FirstAdj
            };
            adjList[GetIndex(v2)].FirstAdj = p;
        }
예제 #3
0
        //从某个顶点出发进行深度优先遍历
        public void DFSAL(int i)
        {
            visited[i] = 1;
            adjListNode <T> p = adjList[i].FirstAdj;

            while (p != null)
            {
                if (visited[p.Adjvex] == 0)
                {
                    DFSAL(p.Adjvex);
                }
                p = p.Next;
            }
        }
예제 #4
0
        public int GetNumOfEdge()
        {
            int i = 0;

            foreach (VexNode <T> nd in adjList)
            {
                adjListNode <T> p = nd.FirstAdj;
                while (p != null)
                {
                    i++;
                    p = p.Next;
                }
            }
            return(i / 2);
        }
예제 #5
0
        public bool IsEdge(Node <T> v1, Node <T> v2)
        {
            if (!IsNode(v1) || !IsNode(v2))
            {
                Console.WriteLine("不是点");
                return(false);
            }

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

            while (p != null)
            {
                if (p.Adjvex == GetIndex(v2))
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #6
0
 public adjListNode(int vex)
 {
     adjvex = vex;
     next   = null;
 }
예제 #7
0
 //构造器
 public VexNode(Node <T> nd, adjListNode <T> alNode)
 {
     data     = nd;
     firstAdj = alNode;
 }
예제 #8
0
 //构造器
 public VexNode(Node <T> nd)
 {
     data     = nd;
     firstAdj = null;
 }
예제 #9
0
        private adjListNode <T> firstAdj; //邻接表的第1个结点

        //构造器
        public VexNode()
        {
            data     = null;
            firstAdj = null;
        }