ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자료구조...77
    일지 2021. 4. 19. 11:03

    ArrayGraph 삽입 메서드)

    노드와 에지를 삽입하는 함수를 구현한다.

     

    ArrayGraph.cpp

    /// <summary>
    /// ArrayGraph에 노드를 추가한다.
    /// </summary>
    void ArrayGraph::InsertNode()
    {
    	if (m_nodeCount < m_nodeCapacity)
    	{
    		m_nodeCount++;
    	}
    }
    
    /// <summary>
    /// ArrayGraph에 from -> to 인 간선을 추가한다.
    /// </summary>
    /// <param name="from">시작 노드</param>
    /// <param name="to">끝 노드</param>
    void ArrayGraph::InsertEdge(int from, int to)
    {
    	if (from < 0 || from >= m_nodeCount || to < 0 || to >= m_nodeCount)
    	{
    		return;
    	}
    
    	m_graph[from][to]++;
    }

     

    main.cpp

    #include <iostream>
    #include "Graph/ArrayGraph.h"
    
    // 생성한 자료구조 테스트용 메인
    int main()
    {
    	ArrayGraph graph;
    
    	graph.InsertNode();
    	graph.InsertNode();
    	graph.PrintInfo();
    
    	graph.InsertEdge(0, 1);
    	graph.PrintInfo();
    
    	graph.InsertEdge(1, 1);
    	graph.PrintInfo();
    }

     

    실행결과

    ----------------------
    Capacity: 5
    Count: 2
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  0
    ----------------------
    
    ----------------------
    Capacity: 5
    Count: 2
     0  1  0  0  0
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  0
    ----------------------
    
    ----------------------
    Capacity: 5
    Count: 2
     0  1  0  0  0
     0  1  0  0  0
     0  0  0  0  0
     0  0  0  0  0
     0  0  0  0  0
    ----------------------

    댓글

Designed by Tistory.