class implementation of adjacency table for graphs

Source: Internet
Author: User

Const intDefaultvertices = -; template<classTclassE>structedge{intDest//Edge Node DefinitionEdge<t,e> *link;//Next edge chain pointerEdge () {} Edge (intnum,e weight):d est (num), link (NULL) {}BOOL operator! = (edge<t,e>& R)Const{    return(dest!= r.deat)?true:false; }};template<classTclassE>structvertex{T data; //Vertex nameEdge<t,e> *adj;//the head pointer of the edge linked list};template<classTclassE>classGraphlnk//Note that there is no inheritance.{ Public: Graphlnk (intsz=defaultvertices); ~Graphlnk (); T GetValue (inti) {return(i>=0&&i<numvertices)? Nodetable[i].data:0; }       intGetvnum () {returnnumvertices;} intGetenum () {returnNumedges;} BOOLInsertvertex (Constt&vertex); BOOLInsertedge (intV1,intv2); intGetfirstneighbor (intv); intGetnextneighbor (intVintW); voidinput (); voidoutput (); intGetvertexpos (t& vertex)//gives the position of the vertex vertex in the diagram{ for(intI=0; I <numVertices; i++)            if(Nodetable[i].data = = vertex)returni; return-1; }   Private:        intMaxvertices,numvertices,numedges;//the 3 data members must be added, respectively , the current maximum number of vertices, the current number of vertices, the current number of edgesVertex<t, e> *nodetable;//vertex table (head node of each edge linked list)}; Template<classTclassE>voidGraphlnk<t, e>:: Input () {intI,j,k,vn,en;//vn denotes the number of vertices, en indicates the number of edgesT V1,v2; cout<<"Enter the number of vertices and the number of edges: \ n"; CIN>>vn>>en;  for(i=0; i<vn;i++) {cin>>v1;   Insertvertex (v1); } I=0;  while(i<en) {cout<<"Enter the two vertices of the edge:"; CIN>>v1>>v2; J=Getvertexpos (v1); K=Getvertexpos (v2); if(j==-1|| k==-1) cout<<"re-enter the two vertex information for the edge! "<<Endl; Else{Insertedge (j,k); I++; }}}template<classTclassE>voidGraphlnk<t, e>:: Output () {intI,vn,en; Edge<T,E> *p; VN=numvertices; en=numedges; cout<<endl<<"number of vertices of the graph ="<<vn<<"number of sides ="<<en<<endl<<Endl;  for(i=0; i<vn;i++) {cout<<i<<": "<<Nodetable[i].data; P=Nodetable[i].adj;  while(p!=NULL) {cout<<" -"<<p->dest; P=p->link; } cout<<Endl; }}template<classTclassE>Graphlnk<t,e>::graphlnk (intSZ) {maxvertices=sz; Numvertices=0; Numedges=0; Nodetable=Newvertex<t,e>[maxvertices];//creating an array of vertex tables    if(Nodetable = =NULL) {cout<<"Storage allocation Error!"<< Endl;exit (1); }      for(inti =0; i < maxvertices;i++) Nodetable[i].adj =NULL;} Template<classTclassE>Graphlnk<t,e>::~Graphlnk () { for(inti =0; i<numvertices;i++) {Edge<T,E> *p =Nodetable[i].adj;  while(P! =NULL) {Nodetable[i].adj= p->link; Deletep; P=Nodetable[i].adj; }    }    Delete[] nodetable;//Delete a vertex table array}template<classTclassE>intGraphlnk<t,e>::getfirstneighbor (intv) {    if(V! =-1) {Edge<T,E> *p =Nodetable[v].adj; if(P!=null)returnP->dest; }    return-1;} Template<classTclassE>intGraphlnk<t,e>::getnextneighbor (intVintW) {    if(v!=-1) {Edge<T,E> *p=Nodetable[v].adj;  while(p!=null&&p->dest!=W) P= p->link; if(p!=null&&p->link!=NULL)returnP->link->dest; }    return-1;} Template<classTclassE>BOOLGraphlnk<t,e>::insertvertex (Constt&Vertex) {    if(numvertices = = maxvertices)return false; Nodetable[numvertices].data=Vertex; Numvertices++; return true;} Template<classTclassE>BOOLGraphlnk<t,e>::insertedge (intV1,intv2) {    if(V1 >=0&& v1 < numvertices && v2>=0&& V2 <numvertices) {Edge<T,E> *q, *p =Nodetable[v1].adj;  while(p!=null&& p->dest!=2) P= p->link; if(P!=null)return false; P=Newedge<t,e>; Q =NewEdge<t,e>; P->dest =v2; P->link =Nodetable[v1].adj; Nodetable[v1].adj=p; Q->dest =v1; Q->link =Nodetable[v2].adj; Nodetable[v2].adj=Q; Numedges++; return true; }} template<classTclassE>/*void BFS (graphlnk<t,e>& g,t& v) {int i,w,n=g.getvnum ();    Modifications are required here, the function is not defined in the class bool *visited=new bool[n];    Linkedqueue<int> Q;    printf ("\ n breadth-First search order: \ n");    for (i=0; i<n; i++) Visited[i]=false;    int loc = G.getvertexpos (v);    cout << g.getvalue (Loc) << "";    Visited[loc] = true;    Q.enqueue (Loc); while (!        Q.isempty ()) {q.dequeue (loc);        w = G.getfirstneighbor (loc);                while (w! =-1) {if (visited[w]==false) {cout << g.getvalue (w) << "";                VISITED[W] = true;            Q.enqueue (w);        } w = G.getnextneighbor (loc,w); }    }}*/

class implementation of adjacency table for graphs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.