软件构造实验2 Poetic Walks

阅读: 评论:0

软件构造实验2 Poetic Walks

软件构造实验2 Poetic Walks

软件构造实验2 Poetic Walks

题目的2.1的要求

我们需要实现一个抽象数据类型图,需要完成图的一系列方法。
整体的实验过程就不细说了,主要讲一个里面需要实现的方法。

set

/**
* Add, change, or remove a weighted directed edge in this graph.
* If weight is nonzero, add an edge or update the weight of that edge;
* vertices with the given labels are added to the graph if they do not
* already exist.
* If weight is zero, remove the edge if it exists (the graph is not
* otherwise modified).
*
* @param source label of the source vertex
* @param target label of the target vertex
* @param weight nonnegative weight of the edge
* @return the previous weight of the edge, or zero if there was no such
* edge,返回-1如果输入的权值是负数.
*/

规约是这样的,总之就是weight是0则删除边;
大于0则设置边或更新权重(如果点还没在点集里需要使点增加到点集)(返回之前的权重,如果之前没有就返回0);
小于零则报错。

@Override public int set(L source, L target, int weight) {//throw new RuntimeException("not implemented");if(weight<0) {System.out.println("权值不能是负数");checkRep();return -1;}else if (weight > 0) {//添加或更新Iterator<Edge<L>> iterator1 =edges.iterator();while(iterator1.hasNext()) {Edge<L> tmp&#();source().equals(source)  &&  arget().equals(target)) {int pre&#weight();ve();Edge<L> newone = new Edge<L>(source,target,weight);edges.add(newone);checkRep();return pre;}}vertices.add(target);vertices.add(source);Edge<L> newone = new Edge<L>(source,target,weight);edges.add(newone);checkRep();return 0;}else {//weight为0int pre = 0;Iterator<Edge<L>> iterator1 =edges.iterator();while(iterator1.hasNext()) {Edge<L> tmp&#();source().equals(source)  &&  arget().equals(target)) {pre&#weight();ve();break;}}checkRep();return pre;}}

我的代码也是基于这三种情况进行考虑的。
注意就是用equals去进行等值比较。

本文发布于:2024-01-31 12:03:01,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170667378228381.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:软件   Poetic   Walks
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23