# 邻接矩阵-C++

#include <iostream>
using namespace std;

struct Edge
{
int start;
int dest;
int weight;
friend ostream &operator<<(ostream &out,Edge &e);
};

ostream &operator<<(ostream &out,Edge &e)
{
out<<"("<<e.start<<","<<e .dest<<","<<e .weight<<")";
return out;
}

#include "Edge.h"
#include "SeqList.h"
#include <iostream>
using namespace std;

const int MAX_WEIGHT=9999;

template<class T>
class AdjMatrixGraph
{
public:
AdjMatrixGraph(int size=10);
AdjMatrixGraph(T vertice[],int vertCount,Edge edges[],int edgeCount);
~AdjMatrixGraph();

int vertexCount();
T Get(int i);
void insertVertex(T vertex);
bool insertEdge(int i,int j,int weight);
bool insertEdge(Edge edge);
friend ostream &operator<<<T>(ostream &out,AdjMatrixGraph<T> &graph);
bool removeEdge(int i,int j);
bool removeVertex(int v,T &old);

private:
SeqList<T> vertexList;
int **adjmatrix;
int size;
int vertCount;
void init(int size);
};

template<class T>
AdjMatrixGraph<T>::AdjMatrixGraph(int size)
{
init(size);
}

template<class T>
void AdjMatrixGraph<T>::init(int size)
{
this->size=size<10?10:size;
this->adjmatrix=new int *[this->size];
for(int i=0;i <this->size;i++)
{
this->adjmatrix[i]=new int[this->size];
for (int j=0;j<this->size;j++)
{
this->adjmatrix[i][j]=(i==j)?0:MAX_WEIGHT;
}
this->vertCount=0;
}
}

template<class T>
AdjMatrixGraph<T>::AdjMatrixGraph(T vertice[],int vertCount,Edge edges[],int edgeCount)
{
init(vertCount);
for (int i=0;i<vertCount;i++)
{
insertVertex(vertice[i]);
}
for (int j=0;j<edgeCount;j++)
{
insertEdge(edges[j]);
}
}

template<class T>
AdjMatrixGraph<T>::~AdjMatrixGraph()
{
for (int i=0;i<size;i++)
{
delete(adjmatrix[i]);
}
delete(adjmatrix);
}

template<class T>
int AdjMatrixGraph<T>::vertexCount()
{
return vertCount;
}

template<class T>
T AdjMatrixGraph<T>::Get(int i)
{
return vertexList.get(i);
}

template<class T>
ostream &operator<<(ostream &out,AdjMatrixGraph<T> &graph)
{
out<<"顶点集合："<<graph.vertexList<<"邻接矩阵：\n";
int n=graph.vertCount;
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
if (graph.adjmatrix[i][j]==MAX_WEIGHT)
{
out<<"∞ ";
}
else
{
out<<" "<<graph.adjmatrix[i][j]<<" ";
}
}
out<<"\n";
}
return out;
}

template<class T>
void AdjMatrixGraph<T>::insertVertex(T vertex)
{
vertexList.insert(vertex);
vertCount++;
if (vertCount>size)
{
int **temp=adjmatrix;
adjmatrix=new int *[size*2];
int i,j;
for (i=0;i<size;i++)
{
adjmatrix[i]=new int[size*2];
for (j=0;j<size;j++)
{
adjmatrix[i][j]=temp[i][j];
}
for (j=size;j<size*2;j++)
{
adjmatrix[i][j]=MAX_WEIGHT;
}
}
for (i=size;i<size*2;i++)
{
adjmatrix[i]=new int[size*2];
for (j=0;j<size*2;)
{
adjmatrix[i][j]=(i==j)?0:MAX_WEIGHT;
}
}
size*=2;
}
}

template<class T>
bool AdjMatrixGraph<T>::insertEdge(int i,int j,int weight)
{
if (i>=0&&i<vertCount&&j>=0&&j<vertCount&&i!=j&&adjmatrix[i][j]==MAX_WEIGHT)
{
adjmatrix[i][j]=weight;
return true;
}
return false;
}

template<class T>
bool AdjMatrixGraph<T>::insertEdge(Edge edge)
{
return insertEdge(edge.start,edge.dest,edge.weight);
}

template<class T>
bool AdjMatrixGraph<T>::removeEdge(int i,int j)
{
if (i>=0&&i<vertCount&&j>=0&&j<vertCount&&i!=j&&adjmatrix[i][j]!=MAX_WEIGHT)
{
adjmatrix[i][j]=MAX_WEIGHT;
return true;
}
return false;
}

template<class T>
bool AdjMatrixGraph<T>::removeVertex(int v,T &old)
{
if (v>=0&&v<vertCount&&vertexList.remove(v,old))
{
for (int i=v;i<vertCount-1;i++)
{
for (int j=0;j<vertCount;j++)
{
adjmatrix[i][j]=adjmatrix[i+1][j];
}
}

for (int j=v;j<vertCount-1;j++)
{
for (int i=0;i<vertCount-1;i++)
{
adjmatrix[i][j]=adjmatrix[i][j+1];
}
}
vertCount--;
return true;
}
return false;
}

#include <iostream>
using namespace std;

template<class T>
class SeqList
{
public:
SeqList(int size=64);
SeqList(T value[],int n);
~SeqList();

bool isEmpty();
int length();
T get(int i);
bool set(int i,T x);
friend ostream &operator<<<T>(ostream &out,SeqList<T> &list);
void insert(int i,T x);
void insert(T x);
bool remove(int i,T &old);
void clear();

private:
T *element;
int size;
int len;
};

template<class T>
SeqList<T>::SeqList(int size)
{
this->size=size<64?64:size;
this->element=new T[this->size];
this->len=0;
}

template<class T>
SeqList<T>::SeqList(T value[],int n)
{
if (n>0)
{
this->element=new T[2*n];
this->size=2*n;
this->len=n;

for(int i=0;i <n;i ++)
{
this->element[i]=value[i];
}
}
}

template<class T>
SeqList<T>::~SeqList()
{
delete []this->element;
}

template<class T>
bool SeqList<T>::isEmpty()
{
return len==0;
}

template<class T>
int SeqList<T>::length()
{
return len;
}

template<class T>
T SeqList<T>::get(int i)
{
return element[i];
/*if (i >=0&&i<len)
{
return element[i];
}
return "false";*/
}

template<class T>
bool SeqList<T>::set(int i,T x)
{
if (i>=0&&i<len)
{
element[i]=x;
return true;
}
return false;
}

template<class T>
ostream &operator<<(ostream &out,SeqList<T> &list)
{
out<<"(";
if (list.len>0)
{
out<<list.element[0];
for (int i=1;i<list.len;i++)
{
out<<","<<list.element[i];
}
}
out<<")\n";
return out;
}

template<class T>
void SeqList<T>::insert(int i,T x)
{
if (len==size)
{
T *temp=element;
element=new T[size*2];
for (int i=0;i<size;i ++)
{
element[i]=temp[i];
}
size*=2;
}
if (i<0)
{
i=0;
}
if (i>len)
{
i=len;
}
for (int j=len-1;j>=i;j--)
{
element[j+1]=element[j];
}
element[i]=x;
len++;
}

template<class T>
void SeqList<T>::insert(T x)
{
insert(len,x);
}

template<class T>
bool SeqList<T>::remove(int i,T &old)
{
if (len>0&&i>=0&&i<len)
{
old=element[i];
for (int j=i;j<len;j++)
{
element[j]=element[j+1];
}
len--;
return true;
}
return false;
}

template<class T>
void SeqList<T>::clear()
{
len=0;
}
#include "AdjMatrixGraph.h"
#include <iostream>
using namespace std;

int main()
{
char *vertices="ABCDE";
Edge edges[]={{0,1,5},{0,3,2},{1,0,5},{1,2,7},{1,3,6},{2,1,7},{2,3,8},{2,4,3},{3,0,2},{3,1,6},{3,2,8},{3,4,9},{4,2,3},{4,3,9}};
AdjMatrixGraph<char> graph(vertices,5,edges,14);
cout<<"带权无向图G3，\n"<<graph<<endl;
graph.insertVertex('F');
cout<<"插入顶点F,插入边（A，F,20)？"<<graph.insertEdge(0,5,20)<<"\n";
char old=' ';
if (graph.removeVertex(2,old))
{
cout<<"删除顶点"<<old<<"\n";
}
cout<<"删除边（v2，v3），"<<(graph.removeEdge(2,3)&&graph.removeEdge(3,2))<<"\n";
cout<<graph<<endl;
system("pause");
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：邻接矩阵-C++ 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)