c++ - Building directed weighted graph and shortest path algorithm -
hey have graph in c++ described code. problem set in university.
#include <iostream> using namespace std; const int n = 10; struct elem { char key; elem *next; } *g[n]; void init(elem *g[n]) { (int = 0; < n; i++) g[i] = null; } int search_node(char c, elem *g[n]) { (int = 0; < n; i++) if (g[i]) if (g[i]->key == c) return 1; return 0; } int search_arc(char from, char to, elem *g[n]) { if (search_node(from, g) && search_node(to, g)) { int = 0; while (g[i] == null || g[i]->key != from) i++; elem *p = g[i]; while (p->key != && p->next) if (p->key == to) return 1; } return 0; } void add_node(char c, elem *g[n]) { if (search_node(c, g)) cout << "node exists.\n"; int = 0; while (g[i] && (i < n)) i++; if (g[i] == null) { g[i] = new elem; g[i]->key = c; g[i]->next = null; } else { cout << "maximum nodes reached.\n"; } } void add_arc(char from, char to, elem *g[n]) { if (search_arc(from, to, g)) cout << "arc exists.\n"; else { if (!search_node(from, g)) add_node(from, g); if (!search_node(to, g)) add_node(to, g); int = 0; while (g[i] == null || g[i]->key != from) i++; elem *p = new elem; p->key = to; p->next = g[i]->next; g[i]->next = p; } } void del_node(char c, elem *g[n]) { if (search_node(c, g)) { int = 0; while (g[i] == null || g[i]->key != c) i++; elem *p, *q = null; while (g[i]) { p = g[i]; g[i] = g[i]->next; delete p; } (int = 0; < n; i++) if (g[i]) { p = g[i]; while (p->key != c && p->next) { q = p; p = p->next; } if (p->key == c) { q->next = p->next; delete p; } } } else cout << "\nthe node not in graph!" << endl; } void print(elem *g[n]) { (int = 0; < n; i++) if (g[i]) { elem *p = g[i]; while (p) { cout << p->key << "\t"; p = p->next; } cout << endl; } }
my graph structure has nodes have next elements arcs first elem in graph array node , next elements arcs.
i have modify , expand code , create directed weighted graph , implement shortest path algorithm. must not use classes! know should not allowed.
Comments
Post a Comment