首页 >> 大全

软件构造实验2 Poetic Walks

2024-01-03 大全 29 作者:考证青年

软件构造实验2 Walks

题目的2.1的要求

我们需要实现一个抽象数据类型图,需要完成图的一系列方法。

整体的实验过程就不细说了,主要讲一个里面需要实现的方法。

set

/**

* Add, , or a edge in this graph.

* If is , add an edge or the of that edge;

* with the given are added to the graph if they do not

* exist.

* If is zero, the edge if it (the graph is not

* ).

* @param label of the

* @param label of the

* @param of the edge

* @ the of the edge, or zero if there was no such

* edge,返回-1如果输入的权值是负数.

*/

规约是这样的,总之就是是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=iterator1.next();if(tmp.getsource().equals(source)  &&  tmp.gettarget().equals(target)) {int pre=tmp.getweight();iterator1.remove();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=iterator1.next();if(tmp.getsource().equals(source)  &&  tmp.gettarget().equals(target)) {pre=tmp.getweight();iterator1.remove();break;}}checkRep();return pre;}}

我的代码也是基于这三种情况进行考虑的。

注意就是用去进行等值比较。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了