先写这么多,明天给例子和邻接表实现
#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);
list<T> getVertex(int v);
list<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);
list<T>& BFS(const T& vertex);
void DFS();
void DFS(const int v, int visited[]);
};
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>
list<T>& getNeighbors(const T& vertex);
{
;
}
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 -1;
}
edge[pos1][pos2] = weight;
}
template <class T>
void Graph<T>::insertVertex(const T& vertex)
{
if(numofVertex + 1 > MaxSize)
{
cerr << "Error2!" << endl;
exit(1);
}
vertexList.insert(vertex);
numofVertex++;
}
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)
{
return 0;
}