zoukankan      html  css  js  c++  java
  • 图的邻接表的类实现

    const int DefaultVertices = 30;
    template<class T,class E>
    struct Edge{
        int dest;  //边结点定义
        Edge<T,E> *link;   //下一条边链指针
        Edge(){}
        Edge(int num,E weight):dest(num),link(NULL) {}
        bool operator!=(Edge<T,E>& R)const{
        return (dest!= R.deat)? true :false;
        }
    };
    
    template<class T,class E>
    struct Vertex{
        T data;   //顶点名字 
        Edge<T,E> *adj;   //边链表的头指针 
    };
    template <class T,class E>
    class Graphlnk  //注意这里没用继承
    {   public:
          Graphlnk(int sz=DefaultVertices);   
          ~Graphlnk();
           T getValue(int i) {  return (i>=0 &&i<numVertices)?NodeTable[i].data : 0;  }
           int getVnum() { return numVertices; }
           int getEnum() { return numEdges; }
           bool insertVertex (const T& vertex);
           bool insertEdge (int v1, int v2);   
           int getFirstNeighbor (int v);
           int getNextNeighbor (int v, int w);    
           void input();
           void output();
            int getVertexPos (T& vertex)    //给出顶点vertex在图中的位置
            {       for (int i=0; i <numVertices; i++)
                if (NodeTable[i].data == vertex) return i;
             return -1; 
             }
       private:
            int maxVertices,numVertices,numEdges;    //必须加上这3个数据成员,分别为当前最大顶点数,当前顶点数,当前边数 
            Vertex<T, E> *NodeTable;             //顶点表 (各边链表的头结点)     
    };
     
     template <class T, class E>
    void Graphlnk<T, E>::input()
    {  int i,j,k,vn,en;      //vn表示顶点数,en表示边数
       T v1,v2;
       cout<<"输入顶点数和边数:
    ";
       cin>>vn>>en;
       for(i=0;i<vn;i++)
       {   cin>>v1;    
           insertVertex(v1);   }
       i=0;
       while(i<en)
       {     cout<<"输入边的两个顶点:";
             cin>>v1>>v2;
             j=getVertexPos(v1);
             k=getVertexPos(v2);
            if(j==-1 || k==-1)
                cout<<"重新输入边的两个顶点信息!"<<endl;
           else
                  {  insertEdge(j,k);
              i++;
        }
       }
    }
    
    template <class T, class E>
    void Graphlnk<T, E>::output()
    {   int i,vn,en;
        Edge<T,E> *p;
        vn=numVertices;   en=numEdges;
       cout<<endl<<"图的顶点数="<<vn<<"边数="<<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<class T,class E>
    Graphlnk<T,E>::Graphlnk(int sz){
        maxVertices = sz;
        numVertices = 0;
        numEdges = 0;
        NodeTable = new Vertex<T,E>[maxVertices];  //创建顶点表数组
        if(NodeTable == NULL){
            cout << "存储分配错误!" << endl;exit(1);
        } 
        for(int i = 0;i < maxVertices;i++)NodeTable[i].adj = NULL;
    }
    
    template<class T,class E>
    Graphlnk<T,E>::~Graphlnk(){
        for(int i =0;i<numVertices;i++){
            Edge<T,E> *p = NodeTable[i].adj;
            while(p != NULL){
                NodeTable[i].adj = p->link;
                delete p ;
                p = NodeTable[i].adj;
            }
        }
        delete[] NodeTable;  // 删除顶点表数组 
    }
    
    template<class T,class E>
    int Graphlnk<T,E>::getFirstNeighbor(int v){
        if(v != -1){
            Edge<T,E> *p = NodeTable[v].adj;
            if(p!=NULL)return p->dest;
        }
        return -1;
    } 
    
    template<class T,class E>
    int Graphlnk<T,E>::getNextNeighbor(int v,int w){
        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)
            return p->link->dest;
        }
        return -1;
    }
     
    template<class T,class E>
    bool Graphlnk<T,E>::insertVertex(const T& Vertex){
        if(numVertices == maxVertices)return false;
        NodeTable[numVertices].data = Vertex;
        numVertices++;
        return true;
    }
    
    template<class T,class E>
    bool Graphlnk<T,E>::insertEdge(int v1,int v2){
        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 = new Edge<T,E>; q = new Edge<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 <class T, class E>
    /*void BFS(Graphlnk<T,E>& G,T& v)   
    {    
        int  i,w,n=G.getVnum();    //此处需修改,类中没定义该函数         
        bool *visited=new bool[n];
        LinkedQueue<int> Q;
        printf("
    广度优先搜索顺序为:
    ");
        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);
            }
        }
    }*/

  • 相关阅读:
    CF1305 Ozon Tech Challenge 2020 游戏存档
    CF1310A Recommendations 题解
    CF755G PolandBall and Many Other Balls 题解
    关于后缀自动机
    具体数学学习笔记
    Flask-SQLAlchemy中解决1366报错
    常用的SQLalchemy 字段类型
    flask_model防止循环引用
    navicate远程访问ubuntu上的mysql数据库
    flask运行环境搭建(nginx+gunicorn)
  • 原文地址:https://www.cnblogs.com/Peit/p/6115045.html
Copyright © 2011-2022 走看看