-1

我已经一年没有编程了,所以我有点生疏了。我真的很想合并一个链接列表,但我无法记住代码是如何工作的,并且必须在 Python 中实现它并没有帮助。

到目前为止,我只设置了节点类。显然,我不能使用令人讨厌的重载构造函数......

基本上我想编写一个程序,提示用户输入 X 个桶。每个桶将有 X 数量的不同颜色的球。用户将指定每种颜色的球数。

我欢迎任何帮助!

class Node:
    def __init__(self, bucketNumber ,colorONE, colorTWO,
        colorTHREE, colorFOUR, colorFIVE ):
        self.bucket = bucketNumber # index
        self.color1 = colorONE # quantity
        self.color2 = colorTWO # quantity
        self.color3 = colorTHREE # quantity
        self.color4 = colorFOUR # quantity
        self.color5 = colorFIVE # quantity


def printN(bucketNum):
    for i in range(0,bucketNum):
        print(nodes[i].bucket, nodes[i].color1, nodes[i].color2, nodes[i].color3, nodes[i].color4, nodes[i].color5)


colors = []
nodes = []
count = []

bucketNum = int(raw_input("The are 2-5 buckets with 2-5 ball colors. Enter number of Buckets:"))
colorNum = int(raw_input("Enter number of Colors:"))
for i in range(0,colorNum):
    colors.append(raw_input("Enter color: " + str(i+1) ))




for i in range(0,bucketNum):
    for j in range(0,colorNum):

        count.append((raw_input("How many "+ colors[j] +" balls in bucket " + str(i+1))))
    nodes.append( Node(i+1, count[0], count[1], count[2], count[3], count[4]) )
    del count[ 0:len(count) ]




for i in range(0,colorNum):
    print colors[i],
print " "
printN(bucketNum)
4

1 回答 1

0

您似乎没有问题,但请注意,可能不需要使用链表,除非您的列表将有很多插入并且很大(因为 pythonlist内存分配;我不认为这是一个问题,直到它出现在分析中),或者如果您将在列表的末尾有很多插入和删除。

在这种情况下,python 提供了collections.deque哪个是链接序列。

于 2012-02-25T11:10:27.190 回答