【发布时间】:2021-03-15 16:14:59
【问题描述】:
我正在尝试删除图中顶点的边缘,但是 removeVertex() 方法中的嵌套循环 不会通过顶点来移除特定的边。
class graph{
constructor(){
this.adjacencyList = {
}
}
addvertex(vertex){
this.adjacencyList[vertex] = []
}
addEdge(v1,v2,edge){
this.adjacencyList[v1].push(v2);
this.adjacencyList[v2].push(v1);
}
removeVertex(vertex){
for( let property in this.adjacencyList){
for( let i = 0 ; i++ ; i < [property].length){
console.log([property][i])
}
}
}
}
【问题讨论】:
-
this.adjacencyList[property].length而不是[property].length
标签: javascript algorithm loops graph