上次的那个邻接矩阵的实现点击打开链接没有给出print()函数等,这次都加上了,测试了,可以了。草草结束,写完邻接表和多重表之后再整合一下。:)
#include <iostream>
#include <vector>
#include <list>
using namespace std;
const int MaxSize = 12;
template <class T>
class Graph
{
private:
list<T> vertexList;
int edge[MaxSize][MaxSize];
int numofVertex,
numofArcs;
// int findVertex(list<T> &l, const T& vertex);
int getVertexPos(const T& vertex);
public:
Graph(void);
int numberOfVertices(void) const;
int getWeight(const T& vertex1, const T& vertex2);
T getVertex(int v);
T& getNeighbors(const T& vertex);
void insertVertex(const T& vertex);
void insertEdge(const T& vertex1, const T& vertex2, int weight);
void deleteVertex(const T& vertex);
void deleteEdge(const T& vertex1, const T& vertex2);
void print() const;
// list<T>& BFS(const T& vertex);
// void DFS();
// void DFS(const int v, int visited[]);
};
template <class T>
void Graph<T>::print() const
{
list<T>::const_iterator iter = vertexList.begin();//这里特别注意,之前写过,const函数,迭代器也要用const_iterator
for(;iter != vertexList.end(); iter++)
{
cout << *iter << " ";
}
cout << endl;
for(int i =0;i<MaxSize; ++i)
{
for(int j = 0; j< MaxSize; ++j)
cout << edge[i][j] << " ";
cout << endl;
}
}
template <class T>
Graph<T>::Graph(void)
{
for(int i =0;i<MaxSize; ++i)
{
for(int j = 0; j< MaxSize; ++j)
edge[i][j] = 0;
}
numofVertex = 0;
numofArcs = 0;
}
template <class T>
int Graph<T>::getVertexPos(const T& vertex)
{
list<T>::iterator iter = vertexList.begin();
int count = 1;
while(iter != vertexList.end() && *iter++ != vertex)
count++;
return count;
}
template <class T>
int Graph<T>::numberOfVertices(void) const
{
return numofVertex;
}
template <class T>
int Graph<T>::getWeight(const T& vertex1, const T& vertex2)
{
int pos1 = getVertexPos(vertex1),
pos2 = getVertexPos(vertex2);
if(pos1 == -1 || pos2 == -1)
{
cerr << "Error!" << endl;
return -1;
}
return edge[pos1][pos2];
}
template <class T>
T& Graph<T>::getNeighbors(const T& vertex)
{
list<T> temp;
cout << endl;
return temp;
}
template <class T>
void Graph<T>::insertEdge(const T& vertex1, const T& vertex2, int weight)
{
int pos1 = getVertexPos(vertex1),
pos2 = getVertexPos(vertex2);
if(pos1 == -1 || pos2 == -1)
{
cerr << "Error!" << endl;
return;
}
edge[pos1][pos2] = weight;
}
template <class T>
void Graph<T>::insertVertex(const T& vertex)
{
if(numofVertex + 1 > MaxSize)
{
cerr << "Error2!" << endl;
exit(1);
}
vertexList.push_back(vertex);
numofVertex++;
}
template <class T>
T Graph<T>::getVertex(int v)
{
list<T> temp;
cout << endl;
return temp;
}
template <class T>
void Graph<T>::deleteVertex(const T& vertex)
{
int pos = getVertexPos(vertex);
int row,
col;
if(-1 == pos)
{
cerr << "Error3!" << endl;
return;
}
list<T>::iterator iter = vertexList.begin();
while(iter != vertexList.end() && *iter != vertex);
vertexList.erase(vertex);
numofVertex--;
for(row =0; row < pos; row++)
{
for(col = pos+1; col < numofVertex; col++)
{
dege[row][col-1] = edge[row][col];
}
}
for(row =pos+1; row < numofVertex; row++)
{
for(col = pos+1; col < numofVertex; col++)
{
dege[row-1][col-1] = edge[row][col];
}
}
for(row =pos+1; row < numofVertex; row++)
{
for(col = 0; col < pos; col++)
{
dege[row-1][col] = edge[row][col];
}
}
}
template <class T>
void Graph<T>::deleteEdge(const T& vertex1, const T& vertex2)
{
insertEdge(vertex1, vertex2, 0);
}
int main(void)
{
Graph<int> g;
g.insertVertex(0);
g.insertVertex(1);
g.insertVertex(2);
g.insertEdge(0,1,2);
g.insertEdge(1,2,3);
g.insertEdge(0,2,1);
g.print();
return 0;
}