不多说了直接上代码,若有代码块有错的话欢迎大家指正哦。
#include<bits/stdc++.h>
using namespace std;
struct Edge //由于邻接链表表示时链表数组的序数即为起始点,而每个节点可以只带有终点。
{
int endVertex;
int weight;
Edge* next;//下一个与顶点可达的点的指针
Edge(int v,int w):endVertex(v),weight(w),next(NULL){}
};
struct Vertex//顶点索引
{
Edge *headnode;
int neighbor;//相邻节点的个数
Vertex():headnode(NULL),neighbor(0){}
};
class Graph
{
private:
Vertex *V;//邻接链表的头节点数组。
int numofvertex;
public:
Graph():V(NULL),numofvertex(0){}
Graph(int num):numofvertex(num)
{
numofvertex=num;
V=new Vertex[num+1];//V[0]不做记录
}
Graph(const Graph & g)//赋值构造函数
{
numofvertex=g.numofvertex;
V=new Vertex[numofvertex];
for(int i=1;i<=numofvertex;i++)
{
V[i].neighbor=g.V[i].neighbor;
Edge *eg=g.V[i].headnode;
if(g.V[i].headnode!=NULL)//分别开辟空间存入邻接数组中
{
V[i].headnode=new Edge(eg->endVertex,eg->weight);
eg=eg->next;
}
Edge *eg1=V[i].headnode;
while(eg!=NULL)
{
eg1->next=new Edge(eg->endVertex,eg->weight);
eg1=eg1->next;
eg=eg->next;
}
}
}
~Graph()//析构函数
{
if(V!=NULL)
{
for(int i=1;i<=numofvertex;i++)
{
Edge *eg=V[i].headnode,*eg1;
while(eg!=NULL)
{
eg1=eg;
eg=eg->next;
delete eg1;
}
}
delete [] V;//注意,这个容易被遗忘,不仅仅要删除链表,还有数组本身哦
V=NULL;
}
}
void InsertVertex(int x,int y,int weight)
{
if(x>numofvertex||x<1||y>numofvertex||y<1||x==y)
{
cerr<<"this edge is invalid!!"<<endl;
return ;
}
if(V!=NULL)
{
Edge *eg=new Edge(y,weight);
if(V[x].headnode==NULL||V[x].headnode->endVertex>y)//按从小到大的顺序连接X的所有可达点
{
eg->next=V[x].headnode;
V[x].headnode=eg;
}
else
{
Edge *eg1=V[x].headnode,*eg2;
while(eg1->next||eg1->endVertex<y)
{
eg2=eg1;
eg1=eg1->next;
}
eg2->next=eg;
eg->next=eg1;
}
}
V[x].neighbor++;
}
void show()
{
if(V!=NULL)
{
for(int i=1;i<=numofvertex;i++)
{
Edge *eg=V[i].headnode;
cout<<i<<"的可达点分别是 ";
while(eg)
{
cout<<eg->endVertex<<" 权值为"<<eg->weight<<" ";
eg=eg->next;
}
cout<<endl;
}
}
}
void DeleteVertex(int x,int y)
{
if(x>numofvertex||x<1||y>numofvertex||y<1||x==y)
{
cerr<<"this edge is invalid!!"<<endl;
return ;
}
if(V)
{
Edge *eg=V[x].headnode,*eg1;
while(eg&&eg->endVertex!=y)//与链表的删除类似哦
{
eg1=eg;
eg=eg->next;
}
if(eg==NULL){cerr<<"no found!!"<<endl;}
else if(eg->endVertex==y)
{
Edge *eg2=eg;
eg1->next=eg->next;
cout<<x<<" "<<y<<"边已被删除"<<endl;
delete eg2;
}
}
V[x].neighbor--;
}
friend void BFS(Graph &g,int m);
friend void DFS(Graph &g,int m);
};
void BFS(Graph &g,int m)
{
queue<int>q;
q.push(m);
int visit[11]={0};
visit[m]=1;
Edge *eg=g.V[m].headnode;
if(!eg){cout<<"none"<<endl;return;}
while(!q.empty())
{
int s=q.front();
q.pop();
eg=g.V[s].headnode;
cout<<s<<" ";
if(!eg||!eg->next) continue;
while(eg&&!visit[eg->endVertex])
{
q.push(eg->endVertex);
visit[eg->endVertex]=1;
eg=eg->next;
}
}
}
int visit[11];
void DFS(Graph &g,int m)
{
cout<<m<<" ";
visit[m]=1;
Edge *eg=g.V[m].headnode;
for(Edge *eg=g.V[m].headnode;eg;eg=eg->next)
if(!visit[eg->endVertex])
DFS(g,eg->endVertex);
}
int main(void)
{
Graph g(10);
for(int i=1;i<=10;i++)
{
for(int j=10;j>=1;j--)
{
g.InsertVertex(i,j,rand()%100+1);
}
}
Graph g1(g);
g1.show();
cout<<endl;
g1.DeleteVertex(3,5);
g1.show();
cout<<endl;
int n=2;
BFS(g1,n);
cout<<endl;
DFS(g1,n);
cout<<endl;
return 0;
}