本文整理汇总了Python中Node.Node.get_data方法的典型用法代码示例。如果您正苦于以下问题:Python Node.get_data方法的具体用法?Python Node.get_data怎么用?Python Node.get_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Node.Node
的用法示例。
在下文中一共展示了Node.get_data方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_question_three
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import get_data [as 别名]
def test_question_three():
print('\n====================\n Testing Question 3\n====================')
first_node = Node('ann')
second_node = Node('bob')
third_node = Node('cat')
first_node.set_next(second_node)
second_node.set_next(third_node)
Asst2.back_to_front_chain(first_node)
print('Test 1\n', ' Expected = ann cat bob None', '\n Got =', end = ' ')
print(first_node.get_data(), end = ' ')
print(first_node.get_next().get_data(), end = ' ')
print(first_node.get_next().get_next().get_data(), end = ' ')
print(first_node.get_next().get_next().get_next())
first_node = Node(1)
second_node = Node(2)
third_node = Node(3)
fourth_node = Node(4)
fifth_node = Node(5)
first_node.set_next(second_node)
second_node.set_next(third_node)
third_node.set_next(fourth_node)
fourth_node.set_next(fifth_node)
Asst2.back_to_front_chain(first_node)
print('Test 2\n', ' Expected = 1 5 2 3 4 None', '\n Got =', end = ' ')
print(first_node.get_data(), end = ' ')
print(first_node.get_next().get_data(), end = ' ')
print(first_node.get_next().get_next().get_data(), end = ' ')
print(first_node.get_next().get_next().get_next().get_data(), end = ' ')
print(first_node.get_next().get_next().get_next().get_next().get_data(), end = ' ')
print(first_node.get_next().get_next().get_next().get_next().get_next())
first_node = Node(1)
second_node = Node(2)
first_node.set_next(second_node)
Asst2.back_to_front_chain(first_node)
print('Test 3\n', ' Expected = 1 2 None', '\n Got =', end = ' ')
print(first_node.get_data(), end = ' ')
print(first_node.get_next().get_data(), end = ' ')
print(first_node.get_next().get_next())
示例2: __init__
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import get_data [as 别名]
class LinkedList:
def __init__(self):
self.head = Node(-1)
self.tail = self.head
self.curr = self.head
self.size = 0
def add(self, data):
self.link_after(data)
def link_after(self, data):
val_to_add = Node(data)
if self.head.nextNode is None:
self.head.nextNode = val_to_add
val_to_add.prevNode = self.head
self.tail = self.head.nextNode
else:
self.tail.nextNode = val_to_add
val_to_add.prevNode = self.tail
self.tail = self.tail.nextNode
self.size += 1
def remove(self):
self.unlink_before()
def unlink_before(self):
temp = self.head.get_data()
self.head = self.head.nextNode
self.head.prevNode = None
self.curr = self.head
self.size -= 1
return temp
def get_size(self):
return self.size
def head(self):
return self.head
def change_head(self, val):
self.head.set_data(val)
def tail(self):
return self.tail
def to_string(self):
string = ""
while self.curr.nextNode is not None:
string += str(self.curr.nextNode.get_data()) + " --> "
self.curr = self.curr.nextNode
self.curr = self.head
# remove the last 5 characters to remove the last -->
return string[0:len(string)-5]