Advertisement
Guest User

asdadf

a guest
Feb 11th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. class Graph {
  2.  private:  
  3.     int VertexCount;  
  4.     List<Vertex> VertexList;
  5.  public:  
  6.     Graph(int VC) {    
  7.         VertexCount = VC;    
  8.         VertexList = List<Vertex>();   }  
  9.     void AddVertex(Vertex* Add) {    
  10.         if(!FindVertex(Add->ID)) {      
  11.            VertexList.Add(Add);     }   }  
  12.     void AddEdge(Vertex* A, Vertex* B, bool TwoWay = 0) {    
  13.         Edge* New = new Edge(A, B);    
  14.         A->Neighbours.Add(New);    
  15.         if(TwoWay) {      
  16.            Edge* NewB = new Edge(B, A);      
  17.            B->Neighbours.Add(NewB);     }   }  
  18.     void RemoveVertex(Vertex* R) {    
  19.         VertexList.Reset();    
  20.         while(Vertex* c = VertexList.current) {      
  21.            c->Neighbours.Reset();      
  22.            while(Edge* n = c->Neighbours.current) {        
  23.               if(n->From == R || n->To == R) {                 
  24.                 c->Neighbours.Remove(n);         }        
  25.               c->Neighbours.Next();       }      
  26.            VertexList.Next();     }   }  
  27.     void RemoveEdge(Vertex* A, Vertex* B, bool TwoWay = 0) {    
  28.         A->Neighbours.Reset();    
  29.         while(Edge* n = A->Neighbours.current) {      
  30.            if(n->To == B) {        
  31.              A->Neighbours.Remove(n);       }      
  32.            A->Neighbours.Next();     }    
  33.         if(TwoWay) {      
  34.            B->Neighbours.Reset();      
  35.            while(Edge* n = B->Neighbours.current) {        
  36.              if(n->To == A) {      
  37.                B->Neighbours.Remove(n);  }        
  38.              B->Neighbours.Next();       }     }   }  
  39.     Vertex* FindVertex(int ID) {    
  40.         Vertex* Prev = NULL;    
  41.         Vertex* Tmp = First;    
  42.         while(Tmp) {      
  43.            if(Tmp->ID == ID) return Tmp;
  44.            Prev = Tmp;      
  45.            Tmp = Tmp->Next;     }    
  46.         return NULL;   }
  47. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement