#include<iostream> #include<list> #define Max 20 using namespace std; //class Vertex //{ //public: // Vertex(char lab) {Label=lab;} //private: // char Label; //}; template <class T> class Graph { public: Graph(const int vertices):n(vertices) { VertexList=new T*[n]; HeadNodes=new list<int>[n]; nVerts=0; } ~Graph() { delete[] VertexList; delete[] HeadNodes; } void addVertex(T* v); void addEdge(int start,int end); void printVertice(); void printAdjList(); private: T** VertexList; list<int>* HeadNodes; int n; int nVerts; }; template<class T> void Graph<T>::addVertex(T * v) { VertexList[nVerts++]=v; } template<class T> void Graph<T>::addEdge(int start,int end) { HeadNodes[start].push_back(end); } template<class T> void Graph<T>::printVertice() { for(int i=0;i<nVerts;i++) cout<<*VertexList[i]<<" "; cout<<endl; } template<class T> void Graph<T>::printAdjList() { for(int i=0;i<nVerts;i++) { cout<<i<<"->"; for(list<int>::iterator iter=HeadNodes[i].begin();iter!=HeadNodes[i].end();++iter) cout<<*iter<<"->";//因为迭代器是指针 cout<<"end"<<endl; } } int main() { Graph<char> g(5); char a='A'; char b='B'; char c='C'; char d='D'; char e='E'; g.addVertex(&a);//因为数组里保存的是字符的指针 g.addVertex(&b); g.addVertex(&c); g.addVertex(&d); g.addVertex(&e); g.addEdge(0,1); g.addEdge(0,3); g.addEdge(1,0); g.addEdge(1,4); g.addEdge(2,4); g.addEdge(3,4); g.addEdge(3,0); g.addEdge(4,1); g.addEdge(4,3); g.addEdge(4,2); g.printAdjList(); g.printVertice(); return 0; }