[英]How can I sort a graph implemented with adjacency list in C++?
I try to sort it every time that I add a vertex, so I want to compare the added vertex with the previous vertex, putting the higher one in the left, until I get the head vertex pointer.每次添加顶点时,我都会尝试对其进行排序,因此我想将添加的顶点与前一个顶点进行比较,将较高的顶点放在左侧,直到获得头部顶点指针。
The problem that I have is that the function that I have to sort the graph some vertex, works a little bit and after it stop in some point but I can't figure it out where the problem is and the compiler doesn't return any error.我遇到的问题是 function 我必须对图进行排序一些顶点,工作一点点,然后在某个点停止,但我不知道问题出在哪里,编译器不返回任何错误。
This is my function to sort any time that I add a new vertex.这是我的 function 在我添加新顶点时进行排序。
void Graph::sortGraph(Vertex *pVertex){
/*
* Sort a vertex when it is insert into the graph
* Sorts it from highest to lowest
*/
while(pVertex->previous != NULL){ //Stops when the Vertex is sorted
if(pVertex->power > pVertex->previous->power){ // To order the graph from highest to lowest
if(pVertex->previous->previous == NULL){//If it is in position 1 and to
graphHead = pVertex;
pVertex->previous->next = pVertex->next;
pVertex->next->previous = pVertex->previous;
pVertex->next = pVertex->previous;
pVertex->previous->previous = pVertex;
pVertex->previous = pVertex->previous->previous;
}
else{ //in any other positions
pVertex->previous->previous->next == pVertex;
pVertex->next = pVertex->previous;
pVertex->previous->previous = pVertex;
pVertex->previous->next = pVertex->next;
pVertex->previous = pVertex->previous->previous;
}
}
pVertex = pVertex->previous; //go backwards because it have to compare the added vertex and the last one
}
} }
This seems wrong:这似乎是错误的:
pVertex->previous->previous = pVertex;
pVertex->previous = pVertex->previous->previous;
you set pVertex->previous->previous to pVertex, then you set pVertex->previous to pVertex->previous->previous that is pVertex itself, so pVertex->previous is pVertex it seems that you create a loop, but maybe I miss something.您将 pVertex->previous->previous 设置为 pVertex,然后将 pVertex->previous 设置为 pVertex->previous->previous 即 pVertex 本身,所以 pVertex->previous 是 pVertex 似乎您创建了一个循环,但也许我错过一些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.