Python/链表/动态/重载

2024-09-29 21:40:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

到目前为止,我只设置了Node类。显然,我不能使用重载构造函数,这是恼人的。。。在

基本上,我想编写一个程序,提示用户输入bucket的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)

Tags: inselfnodeforinputrawbucketcount
1条回答
网友
1楼 · 发布于 2024-09-29 21:40:51

您似乎没有任何疑问,但请注意,可能没有必要使用链表,除非您的列表将有许多插入并且很大(因为python list内存分配;而且我不会假设这是一个问题,直到它出现在评测中),或者在列表的末尾有很多插入和删除。在

在这种情况下,python提供了^{},这是一个链接序列。在

相关问题 更多 >

    热门问题