Мне нужно знать, есть ли способ сделать сортировку по количеству ребер в списке узлов графа. Вот что я пробовал, но из-за направлений памяти края не правильные. Спасибо!
Пример: CHAMANISMO-> 1-> PROPICIACION
PROPICIACION-> 2-> SACERDOTE
Финальная версия: PROPICIACION-> 2-> SACERDOTE
CHAMANISMO-> 1-> CHAMANISMO, но оно должно быть CHAMANISMO-> 1-> PROPICIACION
код:
#ifndef GRAPH_H_
#define GRAPH_H_
#include <string>
#include <iostream>
using namespace std;
class Edge;
class NodeGraph{
string word;//nombre del vertice o nodo
NodeGraph *next;
Edge *ady;//puntero hacia la primera arista del nodo
friend class Graph;
public:
NodeGraph(){
word = "";
next = NULL;
ady = NULL;
}
};
//es cada arista
class Edge{
NodeGraph *ady;//puntero al nodo de llegada
Edge *next;
int timeNodeGraphs; //es el peso
friend class Graph;
public:
Edge(){
ady = NULL;
next = NULL;
timeNodeGraphs = 0;
}
};
//es el grafo principal
class Graph{
NodeGraph *first = new NodeGraph();
//NodeGraph *last = new NodeGraph(); /////////////////agregado
public:
Graph(){
first = NULL;
//last = NULL; /////////////////agregado
}
void insert_NodeGraph(string);
void link_Edge(NodeGraph*, NodeGraph*);
bool search_NodeGraph(string);
NodeGraph* get_NodeGraph(string); //regresa la direccion del vertice
void node_Sort();
Edge* get_Edges(NodeGraph*);
};
//Inserta un nodo
void Graph::insert_NodeGraph(string words){
NodeGraph *nuevo = new NodeGraph(); //crea el nodo a insertar
nuevo->word = words;
nuevo->next = NULL;
nuevo->ady=NULL;
if (search_NodeGraph(words) or words == "") {return;}
if(first==NULL){
first = nuevo;
}
else{
NodeGraph *aux = first; //auxiliar
while(aux->next!=NULL){ //recorre hasta llegar al ultimo
aux = aux->next;
}
aux->next = nuevo; //crea el ultimo nodo
}
}
//realiza las relaciones
void Graph::link_Edge(NodeGraph *origin, NodeGraph *desti){
Edge *newEdge = new Edge(); //crea un nuevo arco
newEdge->timeNodeGraphs = 1;
newEdge->next = NULL;
newEdge->ady = NULL;
Edge *aux = origin->ady; //auxiliar
if(origin->ady == NULL){ //en caso de ser nulo el primer arco
origin->ady = newEdge;
newEdge->ady = desti;
}
else if(aux->ady->word == desti->word){
aux->timeNodeGraphs = aux->timeNodeGraphs + 1;
}
else{
while(aux->next != NULL){ //recorre todos los arcos
if(aux->ady->word == desti->word){ //en caso que el arco ya exista
aux->timeNodeGraphs = aux->timeNodeGraphs + 1; //se le suma al valor
return;
}
aux = aux->next;
}
if(aux->ady->word == desti->word){ //en caso que el arco ya exista
aux->timeNodeGraphs = aux->timeNodeGraphs + 1; //se le suma al valor
return;
}
aux->next = newEdge; //asigna el nuevo arco
newEdge->ady = desti; //asigna el destino
}
}
//cuenta los arcos que hay y suma su peso
int Graph::Edge_number(NodeGraph* word){
int number_Edges = 0;
Edge* first = word->ady;
while (first != NULL){
number_Edges = first->timeNodeGraphs + number_Edges;
first = first->next;
}
return number_Edges;
}
//Hace un sort de los nodos
void Graph::node_Sort(){
NodeGraph* aux = first;
NodeGraph* nextN;
NodeGraph* aux2;
Edge* adyE;
string words;
while(aux->next != NULL){
nextN = aux->next;
while(nextN != NULL){
if(Edge_number(aux) < Edge_number(nextN) && aux->next != nextN){
adyE = nextN->ady;
aux2 = nextN;
words = nextN->word;
nextN->word = aux->word;
nextN->ady = aux->ady;
nextN = aux;
aux->word = words;
aux->ady = adyE;
aux = aux2;
}
nextN = nextN->next;
}
aux = aux->next;
}
}
#endif /* GRAPH_H_ */