@Xc-liu
2018-08-31T23:26:12.000000Z
字数 5794
阅读 758
c++
数据结构(图)
- 代码主体
- 完整代码
#ifndef GRAPH_H_
#define GRAPH_H_
#include <climits>
const int DefaultVertices=30;
template<typename T,typename E>
class Graph{
public:
const E maxWeight=INT_MAX;
Graph(int sz=DefaultVertices);
~Graph();
bool GraphEmpty()const{
if(numEdges==0){return true;}
else{return false;}
}
bool GraphFull()const{
if(numVertices==maxVertices||numEdges==maxVertices*(maxVertices-1)/2){
return true;
}else{
return false;
}
}
int NumberOfVertices(){return numVertices;}
int NumberOfEDges(){return numEdges;}
virtual T getValue(int i);
virtual E getWeight(int v1,int v2);
virtual int getFirstNeighbor(int v);
virtual int getNextNeighbor(int v,int w);
virtual bool insertVertex(const T& vertex);
virtual bool insertEdge(int v1,int v2,E const);
virtual bool removeVertex(int v);
virtual bool removeEdge(int v1,int v2);
protected:
int maxVertices;
int numEdges;
int numVertices;
virtual int getVertexPos(T vertex);
};
#endif
#ifndef GRAPHMTX_H_
#define GRAPHMTX_H_
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "Graph.h"
using namespace std;
extern const int DefaultVertices;
template<typename T,typename E>
class Graphmtx:public Graph<T,E>{
public:
Graphmtx(int size=DefaultVertices);
Graphmtx(char *filename,int size=DefaultVertices);
~Graphmtx(){delete []VerticesList;delete []Edge;}
T getValue(int i){
return i>=0&&i<=this->numVertices?VerticesList[i]:NULL;
}
E getWeight(int v1,int v2){
return v1!=-1&&v2!=-1?Edge[v1][v2]:0;
}
int getFirstNeighbor(int v);
int getNextNeighbor(int v,int w);
bool insertVertex(const T& vertex);
bool insertEdge(int v1,int v2,E cost);
bool removeVertex(int v);
bool removeEdge(int v1,int v2);
friend ostream& operator << <T,E>(ostream& out,Graphmtx<T,E>& G);
private:
T *VerticesList;
E **Edge;
int getVertexPos(T vertex){
for(int i=0;i<this->numVertices;i++){
if(VerticesList[i]==vertex){return i;}
return -1;
}
}
};
template<typename T,typename E>
Graphmtx<T,E>::Graphmtx(int size){
cout << "fuck" <<endl;
this->maxVertices=size;this->numVertices=0;this->numEdges=0;
int i,j;
VerticesList=new T[this->maxVertices];
Edge=(E **)new E*[this->maxVertices];
for(i=0;i<this->maxVertices;i++){
Edge[i]=new E[this->maxVertices];
}
for(j=0;j<this->maxVertices;j++){
for(i=0;i<this->maxVertices;i++){
Edge[i][j]=(i==j)?0:this->maxWeight;
}
}
}
template<typename T,typename E>
Graphmtx<T,E>::Graphmtx(char *filename,int size){
Graphmtx( );
ifstream inFile;
inFile.open(filename);
if(!inFile.is_open()){
cout<<"Couldn't open the file: "<<filename <<endl;
exit(EXIT_FAILURE);
}
T v_value;E w_value;
inFile >> v_value;
insertVertex(v_value);
while(v_value!=';'&&!inFile.eof()){
inFile>>v_value;
if(v_value!=','){
insertVertex(v_value);
}
}
int j,k;
while(!inFile.eof()){
for(int i=0;i<6;i++){
if(i<4){
inFile >> v_value;
if(i==0&&v_value!=','){j=getVertexPos(v_value);}
if(i==2&&v_value!=','){k=getVertexPos(v_value);}
}else if(i==4){
inFile >> w_value;
if(j==-1||k==-1){
cout << "input error!" <<endl;
}else{
insertEdge(j,k,w_value);
}
}else if(i==5){
char temp;
inFile >>temp;
if(temp!=';'){
cout << "input error!"<< endl;
}
}
}
}
}
template<typename T,typename E>
int Graphmtx<T,E>::getFirstNeighbor(int v){
if(v!=-1){
for(int col=0;col<this->numVertices;col++){
if(Edge[v][col]>0&&Edge[v][col]<this->maxWeight){
return col;
}
}
}else{return -1;}
}
template<typename T,typename E>
int Graphmtx<T,E>::getNextNeighbor(int v,int w){
if(v!=-1&&w!=-1){
for(int col=w+1;col<this->numVertices;col++){
if(Edge[v][col]>0&&Edge[v][col]<this->maxWeight){
return col;
}
}
}else{return -1;}
}
template<typename T,typename E>
bool Graphmtx<T,E>::insertVertex(const T& vertex){
if(this->numVertices==this->maxVertices){return false;}
VerticesList[this->numVertices++]=vertex;
return true;
}
template<typename T,typename E>
bool Graphmtx<T,E>::removeVertex(int v){
if(v<0||v>=this->numVertices){return false;}
if(this->numVertices==1){return false;}
int i,j;
VerticesList[v]=VerticesList[this->numVertices-1];
for(i=0;i<this->numVertices;i++){
if(Edge[i][v]>0&&Edge[i][v]<this->maxWeight){this->numEdges--;}
}
for(i=0;i<this->numVertices;i++){
Edge[i][v]=Edge[i][this->numVertices-1];
}
for(j=0;j<this->numVertices;j++){
Edge[v][j]=Edge[this->numVertices-1][j];
}
this->numVertices--;
return true;
}
template<typename T,typename E>
bool Graphmtx<T,E>::insertEdge(int v1,int v2,E cost){
if(v1>-1&&v1<this->numVertices&&v2>-1&&v2<this->numVertices
&&Edge[v1][v2]==this->maxWeight){
Edge[v1][v2]=Edge[v2][v1]=cost;
this->numEdges++;
return true;
}else{
return false;
}
}
template<typename T,typename E>
bool Graphmtx<T,E>::removeEdge(int v1,int v2){
if(v1>-1&&v1<this->numVertices&&v2>-1&&v2<this->numVertices
&&Edge[v1][v2]>0&&Edge[v1][v2]<this->maxWeight){
Edge[v1][v2]=Edge[v2][v1]=this->maxWeight;
this->numEdges--;
return true;
}else{
return false;
}
}
template<typename T,typename E>
ostream& operator <<(ostream& out,Graphmtx<T,E>& G){
int i,j,n,m;T e1,e2;E w;
n=G.NumberOfVertices();m=G.NumberOfEDges;
out<< n << ","<<m<<endl;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
w=G.getWeight(i,j);
if(w>0 &&w<Graph<T,E>::maxWeight){
e1=G.getValue(i);e2=G.getValue(j);
out << "(" << e1 << ","<<e2 <<":" << w << ")"<<endl;
}
}
}
return out;
}
#endif
3.g_mtx_test.cpp
#include "Graphmtx.h"
#include <iostream>
using namespace std;
int main(){
/*char* filename=new char[30];
cout << "please input the filename (less than 30 signals) " << endl;
cin.getline(filename,30); */
Graphmtx<char,int> g_mtx(filename);
//cout << g_mtx <<endl;
return 0;
}
4.g_mtx.txt
A,B,C,D,E;
A,B,24;A,C,46;B,C,15;B,E,67;C,B,37;C,D,53;E,D,31;
CC = g++
CFLAGS = -Wall -O2 -std=c++11 #-I../../common_library/# 指定头文件首先搜索的路径
Filename=g_mtx_test
End=.cpp
$(Filename): $(Filename)$(End) #../../common_library/mystd.cpp
@$(CC) $(CFLAGS) -o $@ $^ #-I ../../DataStructureHomework/chapter2
- 报错形式
编译器显示在链接的过程中出错。
问题在于重载友元函数出错。下面给出目前为止发现能够正确编译的一种重载友元函数的方式:
......
template<typename T>
class class_name;
tempalate<typename T>
ostream& operator << (ostream& out,class_name<T> class_instance);
......
tempalate<typename T>
class class_name(){
......
friend ostream& operator << <T> (ostream& out,class_name<T> class_instance)//注意这里的区别
......
}
//重载友元函数的具体实现
tempalate<typename T>
ostream& operator << (ostream& out,class_name<T> class_instance){
}