稀疏图这样做可以省空间,不过邻接表写算法蛋疼了很多,我还是喜欢用矩阵的。。。
// cunzai direction and value de listGraph
#ifndef listGRAPH_H_
#define listGRAPH_H_
#define MAXSIZE 100
#include <iostream>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;
struct edgeNode
{
char info;
int cost;
edgeNode *next;
};
struct headNode
{
char info;
int index;
edgeNode *firstEdge;
};
class listGraph
{
public:
listGraph();
~listGraph();
void creatGraph();
void dfs();
void bfs();
private:
int headNum;
int edgeNum;
headNode *list;
};
listGraph::listGraph()
{
cout<<"Please input headNum and edgeNum."<<endl;
cin>>headNum>>edgeNum;
list = new headNode[headNum];
cout<<"input every info of the head."<<endl;
for(int i=0; i<headNum; i++)
{
cin>>list[i].info;
list[i].index