c++ - Building an adjacency list graph using link list / vector -
this first go @ building graph , i've decide use ll connect vectors (or nodes or elements, whatever they're called) , each 1 of nodes has vector holds pointers nodes. i've created "addedge" function in graph class seems breaking program. addelement class seems working fine, add , print them out perfectly. when try add edge, breaks. went through debugger , breaks @
while(curr->next != null || curr->val != n)
any ideas why?
#include "element.h" #include "vector" #include "iostream" #include "functional" using namespace std; class graph { public: element *head; graph(int v) { head = null; vector <element*> nodes; } void addelement(int val) { if (head == null) { element *newelement = new element(null, null, val); head = newelement; return ; } element *newelement = new element(null,null, val); element *curr = head; while(curr->next != null) { curr = curr->next; } curr->next = newelement; newelement->prev = curr; return; } void addedge(int n, int edge) { element *e = head; element *curr = head; if(curr = null) { cout<<"there no elements in graph connect"; return; } while(e->next != null || e->val != edge) { cout<<"looking"; e = e->next; } if(e->val != edge) { cout<<"your edge node doesn't exist"; return; } while(curr->next != null || curr->val != n) { cout<<"looking main node"; curr = curr->next; } if(curr->val != n) { cout<<"could not find main node"; return; } curr->edges.push_back(e); cout<<"edge connected"; return; }
node class
#include "vector" #include "iostream" #include "functional" using namespace std; class element { public: element(element* n = null, element *p = null, int num = null) { val = num; next = n; prev = p; } int val; element *prev; element *next; vector<element*> edges; };
Comments
Post a Comment