一个简单的C++数据结构问题
前面两个类要仔细看的,看最底下就行。然后问题是:底下这个Dijkstra这个函数要怎么才能调用?。我要在主函数怎么新建一个类?我主函数这样写,它提示调用错误。Graphm...
前面两个类要仔细看的,看最底下就行。然后问题是: 底下这个Dijkstra这个函数要怎么才能调用?。我要在主函数怎么新建一个类?
我主函数这样写,它提示调用错误。
Graphm M(5);
Dijkstra(M,D,0);
类函数:
class Graph {
private:
void operator = (const Graph&) {}
Graph( const Graph&) {}
public:
Graph() {}
virtual ~Graph() {}
virtual void Init(int n)=0;
virtual int n()=0;
virtual int e()=0;
virtual int first(int v)=0;
virtual int next(int v, int w)=0;
virtual void setEdge(int v1, int v2, int wght)=0;
virtual void delEdge(int v1, int v2)=0;
virtual bool isEdge(int i, int j)=0;
virtual int weight(int v1, int v2)=0;
virtual int getMark(int v)=0;
virtual void setMark(int v, int val)=0;
};
class Graphm: public Graph {
private:
int numVertex, numEdge;
int **matrix;
int *mark;
public:
Graphm(int numVert)
{ Init(numVert); }
~Graphm() {
delete[] mark;
for (int i=0;i<numVertex;i++)
delete [] matrix[i];
delete [] matrix;
}
void Init(int n) {
int i;
numVertex = n;
numEdge = 0;
mark = new int[n];
for (i=0; i<numVertex; i++)
mark[i] = -10;
matrix = (int**) new int * [numVertex];
for (i=0; i<numVertex; i++)
matrix[i] = new int[numVertex];
for (i=0; i<numVertex; i++)
for (int j=0; j<numVertex; j++)
matrix[i][j]=0;
}
int n() { return numVertex; }
int e() { return numEdge; }
int first(int v) {
for(int i=0;i<numVertex;i++)
if(matrix[v][i] !=0) return i;
return numVertex;
}
int next(int v, int w) {
for(int i=w+1;i<numVertex;i++)
if(matrix[v][i] !=0) return i;
return numVertex;
}
void setEdge(int v1, int v2, int wt) {
// Assert(wt>0, "illegal weight value");
if(matrix[v1][v2] ==0) numEdge++;
matrix[v1][v2] = wt;
}
void delEdge(int v1, int v2) {
if(matrix[v1][v2] !=0) numEdge--;
matrix[v1][v2] = 0;
}
bool isEdge(int v1, int v2)
{ return matrix[v1][v2] != 0; }
int weight(int v1, int v2) { return matrix[v1][v2]; }
int getMark(int v) {return mark[v]; }
void setMark(int v, int val) { mark[v] = val; }
}; 展开
我主函数这样写,它提示调用错误。
Graphm M(5);
Dijkstra(M,D,0);
类函数:
class Graph {
private:
void operator = (const Graph&) {}
Graph( const Graph&) {}
public:
Graph() {}
virtual ~Graph() {}
virtual void Init(int n)=0;
virtual int n()=0;
virtual int e()=0;
virtual int first(int v)=0;
virtual int next(int v, int w)=0;
virtual void setEdge(int v1, int v2, int wght)=0;
virtual void delEdge(int v1, int v2)=0;
virtual bool isEdge(int i, int j)=0;
virtual int weight(int v1, int v2)=0;
virtual int getMark(int v)=0;
virtual void setMark(int v, int val)=0;
};
class Graphm: public Graph {
private:
int numVertex, numEdge;
int **matrix;
int *mark;
public:
Graphm(int numVert)
{ Init(numVert); }
~Graphm() {
delete[] mark;
for (int i=0;i<numVertex;i++)
delete [] matrix[i];
delete [] matrix;
}
void Init(int n) {
int i;
numVertex = n;
numEdge = 0;
mark = new int[n];
for (i=0; i<numVertex; i++)
mark[i] = -10;
matrix = (int**) new int * [numVertex];
for (i=0; i<numVertex; i++)
matrix[i] = new int[numVertex];
for (i=0; i<numVertex; i++)
for (int j=0; j<numVertex; j++)
matrix[i][j]=0;
}
int n() { return numVertex; }
int e() { return numEdge; }
int first(int v) {
for(int i=0;i<numVertex;i++)
if(matrix[v][i] !=0) return i;
return numVertex;
}
int next(int v, int w) {
for(int i=w+1;i<numVertex;i++)
if(matrix[v][i] !=0) return i;
return numVertex;
}
void setEdge(int v1, int v2, int wt) {
// Assert(wt>0, "illegal weight value");
if(matrix[v1][v2] ==0) numEdge++;
matrix[v1][v2] = wt;
}
void delEdge(int v1, int v2) {
if(matrix[v1][v2] !=0) numEdge--;
matrix[v1][v2] = 0;
}
bool isEdge(int v1, int v2)
{ return matrix[v1][v2] != 0; }
int weight(int v1, int v2) { return matrix[v1][v2]; }
int getMark(int v) {return mark[v]; }
void setMark(int v, int val) { mark[v] = val; }
}; 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询