일지
자료구조...84
niamdank
2021. 4. 30. 08:23
ArrayListGraph 기능 메서드)
그래프에서 사용하는 기능 메서드를 구현한다.
ArrayListGraph.cpp
/// <summary>
/// 지정된 인덱스의 노드에 진입하는 차수를 반환한다.
/// </summary>
/// <param name="num">노드의 번호</param>
size_t ArrayListGraph::GetDegreeIn(int num)
{
if (!ContainsNode(num))
{
return -1;
}
int count{ 0 };
GraphHeader* curHead{ m_graphHeaders };
while (curHead != nullptr)
{
GraphNode* curNode{ curHead->m_data };
while (curNode != nullptr)
{
if (curNode->m_idx == num)
{
count++;
}
curNode = curNode->m_next;
}
curHead = curHead->m_next;
}
return count;
}
/// <summary>
/// 지정된 인덱스의 노드에서 진출하는 차수를 반환한다.
/// </summary>
/// <param name="num">노드의 번호</param>
size_t ArrayListGraph::GetDegreeOut(int num)
{
GraphHeader* targetHeader{ GetTargetHeader(num) };
if (targetHeader == nullptr)
{
return -1;
}
int count{ 0 };
GraphNode* targetNode = targetHeader->m_data;
while (targetNode != nullptr)
{
targetNode = targetNode->m_next;
count++;
}
return count;
}
main.cpp
#include <iostream>
#include "Graph/ArrayListGraph.h"
// 생성한 자료구조 테스트용 메인
int main()
{
ArrayListGraph graph;
graph.InsertNode(1);
graph.InsertNode(2);
graph.InsertNode(3);
graph.InsertNode(4);
graph.InsertEdge(1, 2);
graph.InsertEdge(1, 4);
graph.InsertEdge(2, 3);
graph.InsertEdge(2, 4);
graph.InsertEdge(3, 4);
graph.PrintInfo();
std::cout << graph.GetDegreeIn(1) << ' ' << graph.GetDegreeOut(1) << '\n';
graph.RemoveEdge(1, 4);
graph.PrintInfo();
std::cout << graph.GetDegreeIn(1) << ' ' << graph.GetDegreeOut(1) << '\n';
graph.RemoveNode(2);
graph.PrintInfo();
}
실행결과
----------------------
Count: 4
4 : NULL
3 : 4
2 : 4 3
1 : 4 2
----------------------
0 2
----------------------
Count: 4
4 : NULL
3 : 4
2 : 4 3
1 : 2
----------------------
0 1
----------------------
Count: 3
4 : NULL
3 : 4
1 : NULL
----------------------