在图的邻接表存储结构下(基于顶点列表和单链表实现),本题要求图类里实现2个方法函数 def addVertex(self, vex_val): def addEdge(self, f, t, cost=0): 函数接口定义: 在这里描述函数接口。例如: def addVertex(self, vex_val): def addEdge(self, f, t, cost=0): 在这里解释接口参数。例如:其中 f和t分别是构成边的顶点在列表中的序号。 裁判测试程序样例: 在这里给出函数被调用进行测试的例子。例如: class arcnode: def __init__(self,adjvex,weight,link=None): self.adjvex = adjvex self.weight = weight self.link=link class vexnode: def __init__(self,data,first_arc=None): self.data = data self.first_arc = first_arc class Graph: def __init__(self): self.vex_list=[] self.vex_num=0 self.edge_num=0 # 请在这里填写答案 # 请在这里填写答案 def print_graph(self): for i in range(self.vex_num): print(self.vex_list[i].data,end="->") cur = self.vex_list[i].first_arc while cur: print("adj:{},weight:{}".format(cur.adjvex,cur.weight),end="->") cur = cur.link print('None') if __name__ =="__main__": g = Graph() s =input() for vertex in s: g.addVertex(vertex) g.addEdge(0,1,11) g.addEdge(0,2,55) g.addEdge(2,3,88) g.addEdge(0,3,33) g.addEdge(1,2,44) g.print_graph()
时间: 2024-03-26 17:37:30 浏览: 118
class arcnode:
def __init__(self, adjvex, weight, link=None):
self.adjvex = adjvex
self.weight = weight
self.link = link
class vexnode:
def __init__(self, data, first_arc=None):
self.data = data
self.first_arc = first_arc
class Graph:
def __init__(self):
self.vex_list = []
self.vex_num = 0
self.edge_num = 0
def addVertex(self, vex_val):
self.vex_list.append(vexnode(vex_val))
self.vex_num += 1
def addEdge(self, f, t, cost=0):
node = arcnode(t, cost)
node.link = self.vex_list[f].first_arc
self.vex_list[f].first_arc = node
self.edge_num += 1
def print_graph(self):
for i in range(self.vex_num):
print(self.vex_list[i].data, end="->")
cur = self.vex_list[i].first_arc
while cur:
print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->")
cur = cur.link
print('None')
if __name__ == "__main__":
g = Graph()
s = input()
for vertex in s:
g.addVertex(vertex)
g.addEdge(0, 1, 11)
g.addEdge(0, 2, 55)
g.addEdge(2, 3, 88)
g.addEdge(0, 3, 33)
g.addEdge(1, 2, 44)
g.print_graph()
阅读全文