Почему мой код просто дает 0, а затем вылетает
#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
using namespace std;
#define SIZE 15
struct Node {
int data;
int path;
int weight;
bool visted;
int key;
Node* next;
} *hashArray[SIZE];;
// Data structure to store graph edges
struct Edge {
int src, dest, weight;
};
class Graph
{
// Function to allocate new node of Adjacency List
Node* AdjNode(int dest, Node* head, int dist)
{
Node* temp = new Node;
temp->data = dest;
temp->path = dist;
temp->visted = false;
// point new node to current head
temp->next = head;
return temp;
}
int numOfNodes; // number of nodes in the graph
int numOfAdj = 0;
public:
// An array of pointers to Node to represent
// adjacency list
Node **head;
Graph(Edge edges[], int n, int numOfNodes)
{
head = new Node*[numOfNodes]();
this->numOfNodes = numOfNodes;
// initialize head pointer for all vertices
for (int i = 0; i < numOfNodes; i++)
head[i] = nullptr;
// add edges to the directed graph
for (int i = 0; i < n; i++)
{
int src = edges[i].src;
int dest = edges[i].dest;
int weight = edges[i].weight;
// insert in the beginning
Node* newNode = AdjNode(dest, head[src], weight);
// point head pointer to new node
head[src] = newNode;
}
}
// Destructor
~Graph() {
for (int i = 0; i < numOfNodes; i++)
delete[] head[i];
delete[] head;
}
} ;
int *dist = new int[1];
int total;
int add(Node* temp)
{
while (temp != nullptr)
{
temp = temp->next;
}
return total;
}
Node *temp = new Node;
int CalculateDistance(Node *src, Node *dest, Graph graph) {
Graph n = graph;
temp = src;
for (int i = add(src); 0 < i; i--) { //stop at last
temp = src;
temp->visted = true;
while (graph.head[i]->visted != true) {
dist[temp->data] = temp->weight + temp->weight;
total += *dist;
if (temp == dest)
continue;
if (temp->visted != true) {
CalculateDistance(temp, dest, n);
}
}
}
return total;
}
int charToInt(char c)
{
int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
return arr[c - '0'];
}
int strToInt(std::string name) {
int x = 0;
for (int i = 0; i < name.length(); i++) {
x += (int)name[i];
}
return x;
}
int hashCode(int key) {
return key % SIZE;
}
struct Node *search(int key) {
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while (hashArray[hashIndex] != NULL) {
if (hashArray[hashIndex]->key == key && hashArray[hashIndex]->key != -1)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
void insert(int data, int key) {
struct Node *item = new Node;
item->data = data;
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while (hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
int main() {
Edge edges[] =
{
{ 1,2, 4.5 },{ 1,6, 4.3 },{ 2,1, 4.5 },{ 2,3, 3.5 },{ 2,7, 3 },{ 3,2, 3.5 },{ 3,4, 3 },{ 4,3, 3 },{ 4,7, 2 },
{ 5,6, 4 },{ 6,5, 4 },{ 6,7, 2 },{ 6,8, 3.5 },{ 7,4, 2 },{ 7,6, 2 },{ 7,8, 2.5 },{ 8,6, 3.5 },{ 8,7, 2.5 }
};
/*
1-Oxford Circus
2-Tottenham Court Road
3-Holborn
4-Covent Garden
5-Green Park
6-Piccadilly Circus
7-Leicester Square
8-Charing Cross
9-Station
10-Temple
11-Embankment
*/
// Number of vertices in the graph
const int N = 9;
// calculate number of edges
int n = sizeof(edges) / sizeof(edges[0]);
// construct graphfdh
Graph graph(edges, n, N);
cout << CalculateDistance(graph.head[0], graph.head[2], graph);
return 0;
}
Моя цель - хешировать данные и давать расстояние между 2 вершинами, но я не знаю, почему он останавливается на 0. ЭтоНенаправленный график, где я использую список ссылок для хранения узлов и массивов для прохождения данных.Не уверен, где я получаю это неправильно.Я проверил, было ли это с моим distanceCalculator, но эта функция не пройдет через первую строку