Java数据结构——关键路径

一、更新ALGraph

(一)、Java代码

package graph;

/**
 * 邻接表
 * 
 * Adjacency List Graph
 * 
 * @author 己千之
 * @time 2022/2/16(周三)
 */
public class ALGraph {
   
	
	/**
	 * @vexs
	 * 顶点数组
	 */
	public VexNode[] vexs;
	
	/**
	 * @arcs 
	 * 边数组 
	 */
	ArcNode[] arcs;
	
	
	/**
	 * 顶点,边个数
	 */
	int vexNum, arcNum;

	/**
	 * 边的信息结点,如权值
	 */
	static class Info {
   
		String value;
		int weight;

		public Info(String value) {
   
			this.value = value;
		}

		public Info(int weight) {
   
			this.weight = weight;
		}

		public Info(String value, int weight) {
   
			this.value = value;
			this.weight = weight;
		}
		
		public String toString() {
   
			return this.value + "=" + this.weight;
		}
	}
	
	/**
	 * 弧结点,表结点
	 */
	class ArcNode {
   
		int index;
		Info info; // 把int改为String
		ArcNode nextNode;
	}

	/**
	 * 顶点,表头结点
	 */
	class VexNode {
   
		String data;
		ArcNode firstArc;
	}

	/**
	 * 边关系
	 * 
	 * @static:测试方法中,用到了Edge数组 Edge是个内部类,你要用,只能: 1.把Edge作为参数,可以用 2.把Edge设为静态调用
	 */
	static class Edge {
   
		String start;
		String end;
		Info info;

		public Edge(String start, String end, Info info) {
   
			this.start = start;
			this.end = end;
			this.info = new Info(info.value, info.weight);
		}
	}

	/**
	 * 构造方法,构造有向图
	 * 
	 * @param vertex
	 * @param edge
	 */
	public ALGraph(String[] vertexs, Edge[] edges) {
   
		// TODO
		// 1.设置成员变量大小
		vexs = new VexNode[vertexs.length];
		arcs = new ArcNode[edges.length];
		vexNum = vertexs.length;
		arcNum = edges.length;

		// 2.初始化数组
		for (int i = 0; i < vexs.length; i++) {
   
			vexs[i] = new VexNode();
			vexs[i].data = vertexs[i];
			vexs[i].firstArc = null;
		}

		// 3.链接
		for (int i = 0; i < edges.length; i++) {
   

			// 3.1 由new Edge('A', 'B', 10)来链接
			String start = edges[i].start;
			String end = edges[i].end;
			int indexOfStart = indexOfVex(start);
			int indexOfEnd = indexOfVex(end);

			// 3.2 构造一个新的arcNode类型的结点
			ArcNode arcNode = new ArcNode();
			
			// one: index
			arcNode.index = indexOfEnd;
			
			// two: info
			arcNode.info = edges[i].info; // TODO ?

			// three: nextNode
			linkedLast(indexOfStart, arcNode); // 尾插法,有向,不是无向
			
			// 3.3 更新arcs
			arcs[i] = arcNode;
		}
	} // stucture

	/**
	 * 获取顶点元素位置
	 * 
	 * @param v
	 * @return
	 */
	public int indexOfVex(String v) {
   
		// 定位顶点的位置

		for (int i = 0; i < vexs.length; i++) {
   
			if (vexs[i].data == v)
				return i;
		}
		return -1;
	} // indexOfVex

	/**
	 * 尾插法,将新元素插在链表尾部
	 * 
	 * @param index
	 * @param node
	 */
	public void linkedLast(int indexofstart, ArcNode arcnode) {
   
		if (vexs[indexofstart].firstArc == null) {
   

			// 关键代码
			vexs[indexofstart].firstArc = arcnode;
		} else {
   
			ArcNode tempNode = vexs[indexofstart].firstArc;
			while (tempNode.nextNode != null) {
   
				tempNode = tempNode.nextNode;
			}

			// 关键代码
			tempNode.nextNode = arcnode;
		}
	} // linkedLast

	/**
	 * 像邻接表一样打印
	 */
	public void printALGraphByChar() {
   
		System.out.println("邻接表存储的图:");
		for (int i = 0; i < vexs.length; i++) {
   

			// 1.打印顶点
			System.out.print(vexs[i].data + "----->");

			// 2.打印挂接链表
			if (vexs[i].firstArc != null) {
   

				// 2.1 firstArc
				ArcNode tempNode = vexs[i].firstArc;

				// 2.2 nextArc
				while (tempNode.nextNode != null) {
   
					System.out.print(vexs[tempNode.index].data + "--->");
					tempNode = tempNode.nextNode;
				}

				// 2.2 tempNode.nextNode = null
				System.out.print(vexs[tempNode.index].data);
			} else {
   
				System.out.print("NULL");
			}

			// 3. 换行
			System.out.println();
		}
	} // printALGraph

	public void printALGraphByIndex() {
   
		System.out.println("邻接表存储的图:");
		for (int i =
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜满月

鼓励,鼓励,更加努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值