以下是使用 Python 的链表实现:
class Node:
def __init__(self,data,next):
self.data = data
self.next = next
class List:
head=None
tail=None
def printlist(self):
print("list")
a=self.head
while a is not None:
print(a)
a=a.next
def append(self, data):
node = Node(data, None)
if self.head is None:
self.head = self.tail = node
else:
self.tail.next = node
self.tail = node
p=List()
p.append(15)
p.append(25)
p.printlist()
Run Code Online (Sandbox Code Playgroud)
输出:
list
<__main__.Node object at 0x03A9F970>
<__main__.Node object at 0x03A9F990>
Run Code Online (Sandbox Code Playgroud)
要检查您的答案,您需要编辑此内置方法def __repr__并重写它。
您也可以通过添加__str__方法来做到这一点
我们都对c ++中的私有变量和公共变量有所了解.是否有任何机制可以在机器级别区分私人和公共成员?如果可以使用成员的地址访问数据成员,那么为什么要在c ++编程中使用private和public.