<数据结构>图存储问题,求高手指点
我这个邻接表图存储有问题,不知道是哪里的问题,是顶点没存对还是边没存对?*****************************************************************************
#include<iostream>
using namespace std;
#define INFINITY 65535
typedef enum{UNVISITED,VISITED};
/*******************邻接表图*********************************************************************************************/
//边类
class Edge
{
public:
int from,to,weight;
Edge()
{
from = -1;to = -1;weight = 0;
}
Edge(int f,int t,int w)
{
from = f;to = t;weight = w;
}
};
//图基类
class Graph
{
public:
int numVertex;//顶点数量
int numEdge;//边的数量
int * Mark;//顶点是否已被游历过
int * Indegree;//顶点入度
int * VertexName;//顶点编号
Graph(int numVert)
{
numVertex = numVert;
numEdge = 0;
Mark = new int[numVertex];
Indegree = new int[numVertex];
VertexName = new int[numVertex];
for(int i = 0;i < numVertex;i ++)
{
Mark[i] = UNVISITED;
Indegree[i] = 0;
VertexName[i] = -1;
}
} //构造
~Graph()
{
delete[] Mark;
delete[] Indegree;
delete[] VertexName;
}//析构
bool IsEdge(Edge oneEdge)
{
if((oneEdge.weight > 0)&&(oneEdge.weight < INFINITY)&&(oneEdge.to >= 0))
return true;
else return false;
}//是不是一条边
void setVertex(Graph & G)
{
for(int i = 0;i <= 9;i++)
G.VertexName[i] = i;
return;
}//设置顶点编号
/*****************
貌似上面这里有问题
******************/
};
//以下为邻接表类图
struct listUnit
{
int end_point;
int weight;
};
template<class Elem>
class Link
{
public:
Elem element;
Link * next;
Link(const Elem& elemval,Link *nextval = NULL)
{
element = elemval;
next = nextval;
}
Link(Link * nextval = NULL)
{
next = nextval;
}
};//构造
template<class Elem>
class LList
{
public:
Link<Elem> * head;
LList()
{
head = new Link<Elem>();//创建头指针以便于操作邻接表
}
};
//以上为邻接表类
class Graphl:public Graph
{
private:
LList<listUnit> * graList;
public:
Graphl(int numVert):Graph(numVert)
{
graList = new LList<listUnit>[numVertex];//设置邻接表存储图
}
int ToVertex(Edge currEdge)
{
return currEdge.to;//返回边的终点
}
Edge FirstEdge(int oneVertex)
{
Edge myEdge;
myEdge.from = oneVertex;
Link<listUnit> * temp = graList[oneVertex].head;
if(temp -> next != NULL)
{
myEdge.to = temp -> next -> element.end_point;
myEdge.weight = temp -> next -> element.weight;
}
return myEdge;
}//返回该顶点的第一条边
Edge NextEdge(Edge preEdge)
{
Edge myEdge;
myEdge.from = preEdge.from;
Link<listUnit> * temp = graList[preEdge.from].head;
while((temp -> next != NULL)&&(temp -> next -> element.end_point <= preEdge.to))
temp = temp -> next;
if(temp -> next != NULL)
{
myEdge.to = temp -> next -> element.end_point;
myEdge.weight = temp -> next ->element.weight;
}
return myEdge;
}//返回与改变相邻的边
void setEdge(int from,int to,int weight)
{
Link<listUnit> * temp = graList[from].head;
while((temp -> next != NULL)&&(temp -> next ->element.end_point < to))
temp = temp -> next;
if(temp -> next == NULL)
{
temp -> next = new Link<listUnit>;
temp -> next -> element.end_point = to;
temp -> next -> element.weight = weight;
numEdge ++;
Indegree[to] ++;
return;
}
if(temp -> next -> element.end_point == to)
{
temp -> next -> element.weight = weight;
return;
}
if(temp -> next -> element.end_point > to)
{
Link<listUnit> * other = temp -> next;
temp -> next = new Link<listUnit>;
temp -> next -> element.end_point = to;
temp -> next -> element.weight = weight;
temp -> next -> next = other;
numEdge ++;
Indegree[to] ++;
return;
}
}//设置一条边
void setAllEdge(Graphl & G)
{
G.setEdge(0,1,5);G.setEdge(0,2,6);
G.setEdge(1,3,3);
G.setEdge(2,3,6);G.setEdge(2,4,3);
G.setEdge(3,4,3);G.setEdge(3,5,4);G.setEdge(3,6,5);
G.setEdge(4,6,1);G.setEdge(4,7,4);
G.setEdge(5,9,4);
G.setEdge(6,8,5);
G.setEdge(7,8,2);
G.setEdge(8,9,2);
return;
}//设置边
}