알고리즘/자료구조

그래프 - 정점 추가, 간선 추가

Aif 2023. 8. 30. 18:58

연결 리스트와 같이 마지막 노드로 이동하여 추가하는 과정을 거치면 된다.

 

void Graph::Vappend(int data) {
	VORTEX* newVortex = new VORTEX;

	newVortex->Data = data;
	newVortex->Ehead = nullptr;
	newVortex->next = nullptr;

	if (!graph->Vhead)
		graph->Vhead = newVortex;
	else {
		VORTEX* temp = nullptr;
		temp = graph->Vhead;
		while (temp->next) {
			temp = temp->next;
		}
		temp->next = newVortex;
	}
}

void Graph::Eappend(VORTEX* from, VORTEX* to) {
	if (!from || !to) {
		std::cout << "없는 정점입니다...\n";
		return;
	}

	EDGE* newEdge = new EDGE;

	newEdge->from = from;
	newEdge->to = to;
	newEdge->next = nullptr;

	if (!from->Ehead)
		from->Ehead = newEdge;
	else {
		EDGE* temp = nullptr;
		temp = from->Ehead;
		while (temp->next)
			temp = temp->next;
		temp->next = newEdge;
	}
}

VORTEX* Graph::getVortex(int data) {
	if (!graph->Vhead)
		return nullptr;
	VORTEX* temp = nullptr;
	temp = graph->Vhead;
	while (temp) {
		if (temp->Data == data) {
			return temp;
		}
		temp = temp->next;
	}
	return temp;
}

'알고리즘 > 자료구조' 카테고리의 다른 글

최소신장트리 - 프림  (0) 2023.09.26
최소 신장 트리  (0) 2023.09.18
그래프  (0) 2023.08.30
힙 - 삭제  (0) 2023.08.30
힙 - 삽입  (0) 2023.08.30