【发布时间】:2020-08-23 16:36:14
【问题描述】:
我每次添加顶点时都会尝试对其进行排序,因此我想将添加的顶点与前一个顶点进行比较,将较高的顶点放在左侧,直到获得头部顶点指针。
我遇到的问题是,我必须对图形进行排序的函数在某个顶点上运行了一点,然后在某个时间点停止,但我无法弄清楚问题出在哪里,而编译器没有t 返回任何错误。
这是我在添加新顶点时进行排序的函数。
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
}
}
【问题讨论】:
-
您是否尝试使用调试器调试您的代码?
-
我试图用代码中的一些打印来调试它,但我找不到问题。
-
这看起来很奇怪
pVertex->previous->previous = pVertex; pVertex->previous = pVertex->previous->previous;。相当于pVertex->previous->previous = pVertex; pVertex->previous = pVertex;
标签: c++ sorting pointers graph vertex