Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用链表模拟排队 我几乎完全用C++来处理链表和类,所以我很难让它工作。第一个类应该有变量名,接下来是一个init函数、两个getter函数和两个setter函数。第二个类(行)应该有一个init函数和一个add函数,用于将项目添加到链表的末尾。我似乎无法使我的add函数工作。感谢您的帮助_Python_Linked List - Fatal编程技术网

在python中使用链表模拟排队 我几乎完全用C++来处理链表和类,所以我很难让它工作。第一个类应该有变量名,接下来是一个init函数、两个getter函数和两个setter函数。第二个类(行)应该有一个init函数和一个add函数,用于将项目添加到链表的末尾。我似乎无法使我的add函数工作。感谢您的帮助

在python中使用链表模拟排队 我几乎完全用C++来处理链表和类,所以我很难让它工作。第一个类应该有变量名,接下来是一个init函数、两个getter函数和两个setter函数。第二个类(行)应该有一个init函数和一个add函数,用于将项目添加到链表的末尾。我似乎无法使我的add函数工作。感谢您的帮助,python,linked-list,Python,Linked List,这是我目前掌握的代码 class PersonList(): """ The class to represent the node in a linked list of people. It takes the variables: name, which represents the person's name, and next_person, which links the next person in the linked list. ""

这是我目前掌握的代码

class PersonList():

    """
    The class to represent the node in a linked list of people.  It takes the variables:
    name, which represents the person's name, and next_person, which links the next
    person in the linked list.
    """
    def __init__(self, name = None, next_ = None):
        self.__name = name
        self.__next = next_

    def getName(self):
        return self.__name

    def getNext(self):
        return self.__next

    def setName(self, new_name):
        self.__name = new_name

    def setNext(self, new_person):
        self.__next = new_person

    def __str__(self):
        return (self.__name)

def printlist(node):
    next_node = node.getNext()
    while next_node != None:
        next_node = node.getNext()
        print (node)
        node = node.getNext()





class Line():

    """ The class that represents the line of people as the linked list.  Takes the variable
    head, that denotes the first person in the line
    """

    def __init__(self, head = None):
        self.head = None


    def add(self, name):


        if self.head == None:
            self.head = PersonList(name)

        else:

是一个选项

还是只跟踪尾部,以避免每次添加内容时遍历整个列表:

class Line():

""" The class that represents the line of people as the linked list.  Takes the variable
head, that denotes the first person in the line
"""

def __init__(self, head = None):
    self.head = None
    self.tail = None


def add(self, name):
    tmp = PersonList(name)
    if self.head == None:
        self.head = tmp
        self.tail = tmp
    else:
        self.tail.next = tmp
        self.tail = tmp

“似乎无法让它工作”到底是什么意思?你似乎还没有完成它的实现……你能描述一下你在尝试添加一个人时看到的行为吗?在python中,您可能不需要像在c中那样实现它。您可以在
Line()
中对
list
对象进行子类化,从而继承其
append()
和其他list方法。我花了大约3-4个小时来尝试add函数的不同变体,并在一个主函数中测试它们。我所有的尝试都以一个没有连接到任何东西的名称结束。我怀疑列表遍历是赋值+1的一部分,因为这是一个更好的解决方案
class Line():

""" The class that represents the line of people as the linked list.  Takes the variable
head, that denotes the first person in the line
"""

def __init__(self, head = None):
    self.head = None
    self.tail = None


def add(self, name):
    tmp = PersonList(name)
    if self.head == None:
        self.head = tmp
        self.tail = tmp
    else:
        self.tail.next = tmp
        self.tail = tmp