请大家讨论一下这些代码的优缺点好吗?
////////////////////////////////////////////////////////////////////////////////////
/////使用邻接表实现无向图的各类操作/////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#include "iostream.h"
/////定义弧结点//////////////
typedef struct ArcNode{
int adjvex;//此弧所指顶点
ArcNode *nextarc;//指向下一条弧
char *info;//此弧相关信息
}ArcNode;
/////定义顶点///////////////
typedef struct VNode{
int data;//顶点信息
ArcNode *firstarc;//指向第一条弧
}VNode, *AdjList;
/////根据图的ADT定义类/////////////////////////////////////////////////////////////
class Graph{
public:
Graph(int allvexnum);
~Graph();
int LocateVex(VNode u);//返回顶点在图中的位置
int GetVex(VNode v);//返回顶点中的数据
void PutVex(VNode *v,int value);//对v赋值value
int FirstAdjVex(VNode v);
int NextAdjVex(VNode v,VNode w);
AdjList vertices;//各个顶点的集合
private:
int vexnum,arcnum;//当前顶点数和弧数
};
/////定义函数//////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//构造函数
Graph::Graph(int vexnum_at_first){
int i;
int vexcon;
ArcNode *newnode, *p;
vexnum=vexnum_at_first;
arcnum=0;
vertices=new VNode[vexnum];
for(i=0;i<vexnum;i++){
cout<<"输入第"<<i+1<<"个顶点的值:"<<endl;
cin>>vertices[i].data;
}
for(i=0;i<vexnum;i++){
cout<<"与第"<<i+1<<"个顶点相连的顶点有(输入负数结束):"<<endl;
cin>>vexcon;
if(vexcon<0)continue;
newnode=new ArcNode;
newnode->adjvex=vexcon;
newnode->nextarc=NULL;//此语句必需
vertices[i].firstarc=newnode;
p=vertices[i].firstarc;
arcnum++;
do{
cin>>vexcon;
if(vexcon<0)break;
newnode=new ArcNode;
newnode->adjvex=vexcon;
newnode->nextarc=NULL;
p->nextarc=newnode;
p=p->nextarc;
arcnum++;
}while(1);
}
};
//析构函数
Graph::~Graph(){
int i;
ArcNode *p,*q;
for(i=0;i<vexnum;i++){
p=vertices[i].firstarc;
if(!p)continue;
q=p->nextarc;
delete p;
while(q){
p=q;
q=q->nextarc;
delete p;
}
}
delete[] vertices;
cout<<"图已删除";
}
//定位函数,返回顶点在图中的位置,否则返回-1
int Graph::LocateVex(VNode u){
int i;
ArcNode *p,*q;
p=u.firstarc;
for(i=0;i<vexnum;i++){
q=vertices[i].firstarc;
while(p&&q){
if(p==q){
p=p->nextarc;
q=q->nextarc;
}
else break;
}
if(p==q)return i;
}
return -1;
}
//返回顶点V的值
int Graph::GetVex(VNode V){
return V.data;
}
//对顶点v赋值
void Graph::PutVex(VNode *v, int value){
v->data=value;
}
//求第一个邻接顶点
int Graph::FirstAdjVex(VNode v){
return v.firstarc? v.firstarc->adjvex:-1;
}
//求下一个邻接点
int Graph::NextAdjVex(VNode v,VNode w){
int i,j;
ArcNode *p;
i=LocateVex(v);
j=LocateVex(w);
p=vertices[i].firstarc;
while(p){
if(p->adjvex==j)break;
p=p->nextarc;
}
if(!p)return -1;
return p->nextarc->adjvex;
}
不知会不会很菜?