简体   繁体   English

如何对 C++ 中使用邻接表实现的图进行排序?

[英]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.

相关问题 如何找到用c ++实现的有向图的所有路径作为邻接表? - How to find all paths for a directed graph implemented in c++ as adjacency list? 将边添加到使用C ++中的链接列表实现的有向图(邻接列表)中 - Add edge to a directed graph (Adjacency list) implemented using Linked List in C++ 如何在C ++中找到图的邻接表的大小? - How to find the size of the adjacency list of the graph in c++? 如何将Erlang数据结构转换为C ++邻接列表(图形)? - How do I convert an Erlang data structure into a C++ adjacency list (graph)? 在C ++中为有向图创建邻接列表 - Making an adjacency list in C++ for a directed graph 尝试在C ++中创建带有邻接表的图 - Trying to create a graph with adjacency list in C++ 在 C++ 中将邻接列表转换为有向无环图的邻接矩阵 - Converting an Adjacency List to a an Adjacency Matrix for a Directed Acyclic Graph in C++ 如何在C ++中的模板化图形类中使用向量创建邻接矩阵? - How can I create an adjacency matrix using vectors in a templated graph class in c++? 考虑到遍历的路径每次都是相同的,如何在C ++中实现有向图而不使用邻接表? - How do I implement a directed graph in C++ without using an adjacency list, given that the path of traversal will be the same every time? 邻接表C ++ - Adjacency list c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM