首页 > 解决方案 > 将新节点附加到链表的末尾

问题描述

我正在尝试创建约会的链接列表,但是当我运行测试代码时,它给了我一个类型错误,说我缺少 3 个位置参数。我想解决方案很简单,但我一直在修补它一段时间,无法弄清楚。

from datetime import datetime


class VaccList:
    class Appointment:
        def __init__(self, name, age, city, date):
            assert type(name) is str, 'name variable must be a string'
            assert type(age) is int, 'age variable must be a integer'
            assert type(city) is str, 'city variable must be a string'
            assert type(date) is datetime, 'date variable must be a datetime object'
            assert name != None, 'name variable cannot be empty'
            assert age >= 18 and age <= 100, 'age must be between 18 and 100'
            #   ADD 6 asserts.  4 for the types and name cannot be empty, 
            #   and age must be between 18 and 100

        self.name = name
        self.age = age
        self.city = city
        self.date = date
        self.confirmed = False
        self.next = None

        def __str__(self):
            s = "Appointment for " + self.name + " on " + str(self.date) + " age:" + str(
            self.age) + "  city:" + self.city
            if self.confirmed:
                s += " (confirmed)"
            else:
                s += " (unconfirmed)"
            return s

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

    def isEmpty(self):
        return self.head is None

    def print(self):  
        '''
        Print all the appointments, one per line.  Print a blank line after the last one.
        If the list is empty, print a line saying the Appointment List is empty.
        '''
        pass

    def append(self, newAppt):  
        '''  Given a pointer to an Appointment object, tack it onto the end.
            If the list was empty, then make sure to set both pointers! 
            This is only used for appointments that are canceled or completed, 
            since we want the active list to remain sorted by date.
        '''
        assert type(newAppt) is VaccList.Appointment, "append() requires a pointer to an Appointment object."
        #  Note, no loop is needed!
        newnode = VaccList.Appointment(newAppt)
        if self.head is None:
            self.head = newnode
        else:
            newnode.next = self.head
            self.head = newnode

if __name__ == "__main__":

    active = VaccList()
    appt = VaccList.Appointment("Henry", 72, "Buffalo", datetime(2021, 5, 1, 12, 0, 0))
    active.append(appt)

以下是我一直遇到的以下错误:

Traceback (most recent call last):
  File "/Users/nicholas/Downloads/SHELL/vacclist.py", line 152, in <module>
active.append(appt)
  File "/Users/nicholas/Downloads/SHELL/vacclist.py", line 54, in append
    newnode = VaccList.Appointment(newAppt)
TypeError: __init__() missing 3 required positional arguments: 'age', 'city', and 'date'

由于我正处于学习 python 的初始阶段,因此对此的任何帮助都非常感谢。

标签: pythonooplinked-list

解决方案


推荐阅读