implement a undirected graph use adjacent list

来源:互联网 发布:国家数据安全标准 编辑:程序博客网 时间:2024/05/21 17:24

I had wire a undirectded graph use adjacent matrix,have a  look ^_^  ...

http://blog.csdn.net/Non_Recursive/archive/2008/11/21/3345183.aspx

 

and this a undirected graph represent by adjacent list..

  1. //AdjListGraph.h 
  2. #include<iostream> 
  3. using namespace std;
  4. const int maxVertex = 1000;   //infinity number 
  5. const int maxWeight = 10000;   //infinity number 
  6. const int defaultVertices = 30;   //default the biggest number of vertices (=n) 
  7. template<typename T,typename E>
  8. struct Edge   //define Edge node 
  9. {
  10.     int dest;     //the other vertex of edge 
  11.     E cost;       //the weight of edge 
  12.     Edge<T,E>* link;   //the next pointer link to edge 
  13.     Edge(){};
  14.     Edge(int num,E weight) : dest(num),cost(weight),link(NULL){}
  15.     bool operator !=(Edge<T,E>& R)const
  16.     {
  17.         return (dest != R.dest) ? true : false;
  18.     }
  19. };
  20. template<typename T,typename E>
  21. struct Vertex            //define vertex 
  22. {
  23.     T data;   //name of vertex 
  24.     Edge<T,E> *head;  //Edge List's head pointer 
  25. };
  26. template<typename T,typename E>
  27. class AdjListGraph
  28. {
  29.     Vertex<T,E>* NodeTable;      //node table 
  30.     int maxVertices;
  31.     int numEdges,numVertices;    //current number of edge and vertex 
  32.     T *VerticesList;     //vertex table 
  33.     //E **Edge;           //Adjacent matrix 
  34.     int GetVertexPos(const T vertex)
  35.     {
  36.         for (int i=0;i<numVertices;i++)
  37.            if (NodeTable[i].data == vertex) return i;
  38.         return -1;
  39.     }
  40.     friend istream& operator >>(istream& in,AdjListGraph<T,E>& G);
  41.     friend ostream& operator <<(ostream& out,AdjListGraph<T,E>& G);
  42. public:
  43.     AdjListGraph(int sz=defaultVertices);
  44.     ~AdjListGraph();
  45.     T GetValue(int i)
  46.     {
  47.         return (i>=0 && i<numVertices) ? NodeTable[i].data : 0;
  48.     }
  49.     E GetWeight(int v1,int v2);
  50.     bool InsertVertex(const T& vertex);
  51.     bool RemoveVertex(int v);
  52.     bool InsertEdge(int v1,int v2,E cost);
  53.     bool RemoveEdge(int v1,int v2);
  54.     int GetFirstNeighbor(int v);
  55.     int GetNextNeighbor(int v,int w);
  56. };
  57. //AdjListGraph.cpp 
  58. #include "AdjListGraph.h" 
  59. template<typename T,typename E>
  60. AdjListGraph<T,E>::AdjListGraph(int sz)
  61. {
  62.     maxVertices = sz,numVertices = numEdges = 0;
  63.     NodeTable = new Vertex<T,E>[maxVertices];  //create node table array 
  64.     if (NodeTable == NULL) exit(1);  //allocation error 
  65.     for(int i=0;i<maxVertices;i++)
  66.        NodeTable[i].head = NULL;
  67. }
  68. template<typename T,typename E>
  69. AdjListGraph<T,E>::~AdjListGraph()
  70. {
  71.     for (int i=0;i<numVertices;i++)
  72.     {
  73.         Edge<T,E>* p = NodeTable[i].head;  //find the head node 
  74.         while (p != NULL)
  75.         {
  76.             NodeTable[i].head = p->link;  //delete first node 
  77.             delete p;
  78.             p = NodeTable[i].head;
  79.         }
  80.     }
  81.     delete []NodeTable;   //delete vertex table array 
  82. }
  83. template<typename T,typename E>
  84. int AdjListGraph<T,E>::GetFirstNeighbor(int v)
  85. {
  86.     if (v != -1)
  87.     {
  88.         Edge<T,E>* p = NodeTable[v].head;   //the first edge node 
  89.         if (p != NULL) return p->dest;   //return the first adjacent node 
  90.     }
  91.     return -1;   //there is not first adjacent node 
  92. }
  93. template<typename T,typename E>
  94. int AdjListGraph<T,E>::GetNextNeighbor(int v,int w)
  95. {
  96.     //find w's adjacent node,which 'w' is a adjacent node of 'v' 
  97.     if (v != -1)
  98.     {
  99.         Edge<T,E>* p = NodeTable[v].head;  //对应于边链表第一个边结点... 
  100.         while (p != NULL && p->dest != w)  //try to find v's adjacent node 'w' 
  101.             p = p->link;
  102.         if (p != NULL && p->link != NULL)
  103.             return p->link->dest;   //return nest adjacent node 
  104.     }
  105.     return -1;    //there is not "next adjacent node" 
  106. }
  107. template<typename T,typename E>
  108. E AdjListGraph<T,E>::GetWeight(int v1,int v2)
  109. {
  110.     if ((v1 != -1) && (v2 != -1))
  111.     {
  112.         Edge<T,E>* p = NodeTable[v1].head;  //first edge related to 'v1' 
  113.         while (p!=NULL && p->dest!=v2) p = p->link;
  114.         if (p != NULL) return p->cost;
  115.     }
  116.     return 0;
  117. }
  118. template<typename T,typename E>
  119. bool AdjListGraph<T,E>::InsertVertex(const T& vertex)
  120. {
  121.     if (numVertices == maxVertex) return false;   //node table have full 
  122.     NodeTable[numVertices++].data = vertex;
  123.     return true;
  124. }
  125. template<typename T,typename E>
  126. bool AdjListGraph<T,E>::RemoveVertex(int v)
  127. {
  128.     if (numVertices==1 || v<0 || v>=maxVertices) return false//empty table or ... 
  129.     Edge<T,E>* p,*s,*t;
  130.     while (NodeTable[v].head != NULL)
  131.     {
  132.         p = NodeTable[v].head;
  133.         int k = p->dest;
  134.         s = NodeTable[k].head ; //p->dest 
  135.         t = NULL;
  136.         while (s!=NULL && s->dest!=v)
  137.         {
  138.             t = s;
  139.             s = s->link;
  140.         }
  141.         if (s != NULL)
  142.         {
  143.             if (t ==NULL) NodeTable[k].head = s->link;
  144.             else t->link = s->link;
  145.             delete s;
  146.         }
  147.         NodeTable[v].head = p->link;
  148.         delete p;
  149.         numEdges--;
  150.     }
  151.     numVertices--;
  152.     NodeTable[v].data = NodeTable[numVertices].data;   //fill 
  153.     p = NodeTable[v].head = NodeTable[numVertices].head;
  154.     while (p!=NULL)
  155.     {
  156.         s = NodeTable[p->dest].head;
  157.         while(s!=NULL)
  158.             if (s->dest == numVertices)
  159.             {
  160.                 s->dest = v;
  161.                 break;
  162.             }
  163.             else s = s->link;
  164.     }
  165.     return true;
  166. }
  167. template<typename T,typename E>
  168. bool AdjListGraph<T,E>::InsertEdge(int v1,int v2,E weight)
  169. {
  170.     if ((v1>=0 && v1<numVertices) && (v2>=0 && v2<numVertices))
  171.     {
  172.         Edge<T,E>* q,*p = NodeTable[v1].head;
  173.         while (p!=NULL && p->dest!=v2) p = p->link;
  174.         if (p == NULL) return false;  // 
  175.         p = new Edge<T,E>;
  176.         q = new Edge<T,E>;
  177.         p->dest = v2;
  178.         p->cost = weight;
  179.         p->link = NodeTable[v1].head;   //link to 'v1' edgeLinkNode 
  180.         NodeTable[v1].head = p;
  181.         q->dest = v1;
  182.         q->weight = weight;
  183.         q->link = NodeTable[v2].head;    //link to 'v2' edgeLinkNode 
  184.         NodeTable[v2].head = q;
  185.         numEdges++;
  186.         return true;
  187.     }
  188.     return 0;
  189. }
  190. template<typename T,typename E>
  191. bool AdjListGraph<T,E>::RemoveEdge(int v1,int v2)
  192. {
  193.     if ((v1 != -1) && (v2 != -1))
  194.     {
  195.         Edge<T,E>* p = NodeTable[v1].head,*q = NULL,*s = p;
  196.         while (p!=NULL && p->dest!=v2)   //v1对应边中找删除边 
  197.         {
  198.             q = p;
  199.             p = p->link;
  200.         }
  201.         if (p!=NULL)   //fimd the edge prepare to delete 
  202.         {
  203.             if (p == s)NodeTable[v1].head = p->link;   //first node of edgeLinkTable 
  204.             else q->link = p->link;   //else reconnection 
  205.             delete p;
  206.         }
  207.         else return false;   //can't find the edge to remove 
  208.         p = NodeTable[v2].head;
  209.         q = NULL,s = p;   //v2对应边中找删除边 
  210.         while (p->dest != v1)
  211.         {
  212.             q = p;
  213.             p = p->link;
  214.         }
  215.         if (p == s)NodeTable[v2].head = p->link;
  216.         else q->link = p->link;
  217.         delete p;
  218.         return true;
  219.     }
  220.     return false;
  221. }
  222. template<typename T,typename E>
  223. istream& operator >>(istream& in,AdjListGraph<T,E>& G)
  224. {
  225.     int n,m;
  226.     T v1,v2;
  227.     E weight;
  228.     cout<<"please the numbers of vertex and edge:";
  229.     cin>>n>>m;    //input vertices number,edges number; 
  230.     for (int i=0;i<n;i++)      //create data of vertex table 
  231.     {
  232.         in>>v1;
  233.         G.InsertVertex(v1);
  234.     }
  235.     int j=0;
  236.     while (j<m)
  237.     {
  238.         in>>v1>>v2>>weight;              //input  information 
  239.         int pos1 = G.GetVertexPos(v1);
  240.         int pos2 = G.GetVertexPos(v2);
  241.         if (pos1==-1 || pos2==-1) cout<<"Error,please input again"<<endl;
  242.         else j++,G.InsertEdge(pos1,pos2,weight);
  243.     }
  244.     return in;
  245. }
  246. template<typename T,typename E>
  247. ostream& operator <<(ostream& out,AdjListGraph<T,E>& G)
  248. {
  249.     //out all the information of graph's vertices and edges 
  250.     int n = G.NumberOfVertices(),m = G.NumberOfEdges();
  251.     cout<<"number of vertices is "<<n<<", number of edges if "<<m<<endl;
  252.     for (int i=0;i<n;i++)
  253.           for (int j=i+1;j<n;j++)
  254.           {
  255.               int w = G.GetWeight(i,j);
  256.               if (w>0 && w<maxWeight)
  257.               {
  258.                   int e1 = G.GetValues(i),e2 = G.GetValue(j);
  259.                   out<<"("<<e1<<","<<e2<<","<<w<<")"<<endl;
  260.               }
  261.           }
  262.     return out;
  263. }
  264. //main.cpp 
  265. #include "AdjListGraph.cpp" 
  266. int main(void)  //a simple test 
  267. {
  268.     AdjListGraph<int,int> AdjListG(10);
  269.     return 0;
  270. }

 

 

 

原创粉丝点击