數據結構經典算法彙總___圖的鄰接表實現

圖的鄰接表實現:

Graph2.h 文件

#include<iostream.h>/////////////// 鄰接表實現圖

const Maxvertexes=4;

template<class VertexType,class ArcType>struct ArcNode{  // 弧節點

       int adjex;  // 相關連的接點箭頭頭部節點的序號

       ArcType weight;

       ArcNode<VertexType,ArcType> *nextarc;// 下條弧指針

};

template<class VertexType,class ArcType>struct VertexNode{// 頂點接點

       VertexType data;

       ArcNode<VertexType,ArcType> *firstarc;

};

template<class VertexType,class ArcType>class Graph{

  private:

         VertexNode<VertexType,ArcType>* vertextable;

         int currentnumarc;    // 當前邊數目

         int currentnumvertex; // 當前頂點數

  public:

         Graph(VertexType v[],int num=Maxvertexes);

         ~Graph();

         int InsertVertex(VertexType v){

 

                if (currentnumvertex<=Maxvertexes){

                       vertextable[currentnumvertex].data=v;

              vertextable[currentnumvertex].firstarc=NULL;

                       currentnumvertex++;

                       return 1;

                }

                cout<<"Insert unsuccessful ,the num vertex is full"<<endl;

                return 0;

         }

         int InsertArc(int tail,int head,ArcType weight){      

                //////////////////@@@@@@@@@@@@@@@@@@@@@@@@@ 注意這裏的 tali head 是序列號是,數組的下標 暈死了 !!

                if(tail>currentnumvertex-1){

                       cout<<"the vertex(tail) not exist"<<endl; return 0;}

             if(head>currentnumvertex-1){

                   cout<<"the vertex(head) not exist"<<endl; return 0;}

          ArcNode<VertexType,ArcType> *p=vertextable[tail].firstarc;

                if(p==NULL){//p 插入的要爲第一個邊

                       ArcNode<VertexType,ArcType> *temp=new ArcNode<VertexType,ArcType>;

                       temp->weight=weight;

                    temp->nextarc=NULL;

                    temp->adjex=head;

                       //cout<<head<<" ";

                    vertextable[tail].firstarc=temp;

                    currentnumarc++;

                    return 1;

                }

                while(p->nextarc!=NULL) p=p->nextarc;

                ArcNode<VertexType,ArcType> *temp=new ArcNode<VertexType,ArcType>;

                temp->weight=weight;

                temp->nextarc=NULL;

                temp->adjex=head;

                // cout<<head<<" ";

                p->nextarc=temp;

                currentnumarc++;

                return 1;

         }

         int Getpos(VertexType v){

                for(int i=0;i<currentnumvertex;i++){

                       if(vertextable[i].data==v){

                              //cout<<" 等於 "<<endl;

                              return  i;

                       }

                }

                return -1;

         }

         void printvertex(){

                for(int i=0;i<currentnumvertex;i++)

                       cout<<vertextable[i].data;

         }

         int Getcurrentvertex()

         { return currentnumvertex;}

         int Getcurrentnumarc()

         { return currentnumarc;}

         void visite(int i){

                cout<<vertextable[i].data<<" ";

 

         }

         void DFTraverse();   // 深度優先遍歷圖

         void DFS(const int v,int visited[]);

         int Getnextneighbor(int v);// 取下標 V 的第一個鄰接點下標的下標

         int Getnextneighbor(int v,int w);// 取下標 V 的位於下標 W 後的鄰接點的下標

         void print1(){

                ArcNode<VertexType,ArcType> *p;

                for(int i=0;i<currentnumvertex;i++){

                       cout<<vertextable[i].data<<":";

                   p=vertextable[i].firstarc;

                   while(p!=NULL)

                       {  cout<<"->["<<p->adjex<<"]"<<vertextable[p->adjex].data;

                      p=p->nextarc;

                       }

                       cout<<endl;

                }

         }

 

};

template<class VertexType,class ArcType>Graph<VertexType,ArcType>:: Graph(VertexType v[],int num=Maxvertexes)

:currentnumarc(0),currentnumvertex(0)

{ int e,t,h;

  VertexType tail,head;

  ArcType w;

  vertextable=new VertexNode<VertexType,ArcType>[Maxvertexes];

  for(int i=0;i<Maxvertexes;i++)

         InsertVertex(v[i]);

  cout<<"please input the num of arc:";

  cin>>e;

  for(i=0;i<e;i++){

         cin>>tail>>head>>w;   // 逐條輸入邊和權值 tail 爲開始接點, head 爲結束點

         if((t=Getpos(tail))==-1){

                cout<<"the vertex(tail) not exist"<<endl;return ;}

         else if((h=Getpos(head))==-1){

                cout<<"the vertex(head) not exist"<<endl;return ;}

         else InsertArc(t,h,w);  //insert border from t ---->h  the  weight is w

  }

}

template<class VertexType,class ArcType>Graph<VertexType,ArcType>:: ~Graph()

{ ArcNode<VertexType,ArcType> *p1,*p2;

  for(int i=0;i<currentnumvertex;i++){

         p1=vertextable[i].firstarc;

         if(p1!=NULL){

                if(p1->nextarc==NULL){

                       delete p1;

                       return ;

                }

                p2=p1->nextarc;

                delete p1;

                p1=p2;

                p2=p2->nextarc;

         }

  }

      

}

template<class VertexType,class ArcType>void Graph<VertexType,ArcType>::DFTraverse(){

       int i,n=currentnumvertex;

       int *visited=new int[n];

       for(i=0;i<n;i++)

        visited[i]=0;

       for(i=0;i<n;i++){

              if(visited[i]==0)  DFS(i,visited);

       }

       delete []visited;

}

template<class VertexType,class ArcType>void Graph<VertexType,ArcType>::DFS(const int v,int visited[]){

       visite(v);

       visited[v]=1;

       int w;

       w=Getnextneighbor(v);// get first next vertex

       while(w!=-1){

           if(visited[w]==0)  DFS(w,visited);

           w=Getnextneighbor(v,w);   // v 的排在 w 後的下一個鄰接點 ,now the w is visited and muset 回溯

       }

}

template<class VertexType,class ArcType>int Graph<VertexType,ArcType>::Getnextneighbor(int v){

       if(vertextable[v].firstarc!=NULL)

           return (vertextable[v].firstarc->adjex);

       else return -1;

}

template<class VertexType,class ArcType>int Graph<VertexType,ArcType>::Getnextneighbor(int v,int w){

       ArcNode<VertexType,ArcType> *p=vertextable[v].firstarc;

       while(p!=NULL){

              if(p->adjex==w){

                     if(p->nextarc!=NULL)  return p->nextarc->adjex;

                     else return -1;

              }

              p=p->nextarc;

       }

       return -1;

}

主測試函數 main.cpp

#include<iostream.h>

//#include"heap.h"

//#include"heap2.h"

//#include"graph.h"

#include"graph2.h"

//#include"huffmantree.h"

int main()

{/////////////////////////////////////////////test Graph<> 有向圖鄰接表實現 + 圖的兩種遍歷算法

       char v[4];

       for(int i=0;i<4;i++)

              cin>>v[i];

       Graph<char,int> graph(v);

    graph.printvertex(); cout<<endl;

       cout<<"num of arc:"<<graph.Getcurrentnumarc()<<endl;

       cout<<"num of vertex:"<<graph.Getcurrentvertex()<<endl;

    cout<<"DFT is( 深度優先便歷 ):";graph.DFTraverse();

       cout<<endl;

       cout<<"Vertextable:( 鄰接表 )"<<endl;

       graph.print1();

       //cout<<endl;

       //cout<<graph.Getnextneighbor(0,1)<<endl;

   return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章