资源简介
校园导游系统的设计与实现
关于数据结构 图的应用,实现图的深度优先遍历,广度优先遍历,最小生成树,最短路径等功能的完整C#源代码
有注释,帮助理解。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Text;
namespace play
{
class Vertex
{
public Vertex(string vna)
{
vname = vna;
wasVisited = false;
}
public string vname;
public bool wasVisited;
}
class Graph
{
private Vertex[] vertices = new Vertex[10];
private int[] cost = new int[10 10];//邻近矩阵包含边的代价
private int n;//n是顶点的数目
int infinity = 9999999;//将非常大的值作为无穷大。
Stack thestack = new Stack(10);
Queue thequeue = new Queue();
public Graph()
{
n = 0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
if (i == j)
cost[i j] = 0;
else
cost[i j] = infinity;
}
}
private bool edgeExists()
{
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
if ((cost[i j] != 0) && (cost[i j] != infinity))
return (true);
return (false);
}
public void addVertex(string vnam)
{
int i = getIndex(vnam);
if (i != -1)
{
Console.WriteLine(“\n该景点已经存在。“);
return;
}
vertices[n] = new Vertex(vnam);
n++;
}
private int getIndex(string vname)
{
for (int i = 0; i < n; i++)
if (vertices[i].vname == vname)
return (i);
return (-1);//如果在列表中没有找到,返回-1。
}
public void addEdge(string v1 string v2 int a)
{
int i1 i2;
if (n == 0)
{
Console.WriteLine(“\n不存在任何景点。你需要先增加一个景点。“);
return;
}
while (true)
{
i1 = getIndex(v1);
if (i1 == -1)
Console.WriteLine(“\n该起始景点不存在,请重试。“);
else
break;
}
while (true)
{
i2 = getIndex(v2);
if (i2 == -1)
Console.WriteLine(“\n该目的地景点不存在,请重试。“);
else
break;
}
cost[i1 i2] = cost[i2 i1] = a;
}
public void display()
{
if (n == 0)
{
Console.WriteLine(“\n图不存在。“);
return;
}
Console.WriteLine(“\n景点:“);
for (int i = 0; i < n; i++)
Console.WriteLine(vertices[i].vname);
if (edgeExists())
{
Console.WriteLine(“\n路径:“);
for (int i = 0; i < n; i++)
评论
共有 条评论