本文整理匯總了Python中LinkedList.LinkedList.add_node方法的典型用法代碼示例。如果您正苦於以下問題:Python LinkedList.add_node方法的具體用法?Python LinkedList.add_node怎麽用?Python LinkedList.add_node使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類LinkedList.LinkedList
的用法示例。
在下文中一共展示了LinkedList.add_node方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: partition
# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add_node [as 別名]
def partition (LList, x):
this_node = LList.get_root()
smaller = LinkedList()
larger = LinkedList()
while this_node:
if this_node.get_data() < x:
smaller.add_tail(this_node.get_data())
else:
larger.add_tail(this_node.get_data())
this_node = this_node.get_next()
smaller.add_node(larger.get_root())
return smaller
示例2: __init__
# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add_node [as 別名]
class Queue:
"""
This code contains a Queue data-structure implementation based on the LinkedList class
"""
type = "queue"
list = None
def __init__(self):
self.list = LinkedList()
pass
def push(self, data):
self.list.add_node(data)
def pop(self):
if self.list.size > 0:
return self.list.remove_node(0)
else:
return None
示例3: add_linked_lists
# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add_node [as 別名]
def add_linked_lists(l1, l2):
if l1 is None:
return l2
elif l2 is None:
return l1
carry = 0
sum_list = LinkedList()
while l1 is not None and l2 is not None:
cur_sum = l1.data + l2.data + carry
sum_list.add_node(cur_sum % 10)
carry = cur_sum / 10
l1 = l1.next
l2 = l2.next
while l1 is not None:
sum_list.add_node(l1.data)
l1 = l1.next
while l2 is not None:
sum_list.add_node(l2.data)
l2 = l2.next
return sum_list
示例4: LinkedList
# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add_node [as 別名]
slow = circular_list.head
# Since fast is k nodes from the start of the cycle as well,
# moving both pointers one node at a time would cause them to
# meet at the beginning of the cycle
while fast != slow:
fast = fast.next
slow = slow.next
return fast
if __name__ == "__main__":
list_vals = ["E", "D", "C", "B", "A"]
circular_list = LinkedList()
for val in list_vals:
circular_list.add_node(val)
ptr = circular_list.head
while ptr.next is not None:
ptr = ptr.next
circular_list.print_list()
ptr.next = circular_list.head.next.next
cycle_start = find_loop_start(circular_list)
print cycle_start.data
示例5: LinkedList
# 需要導入模塊: from LinkedList import LinkedList [as 別名]
# 或者: from LinkedList.LinkedList import add_node [as 別名]
sum_list.add_node(cur_sum % 10)
carry = cur_sum / 10
l1 = l1.next
l2 = l2.next
while l1 is not None:
sum_list.add_node(l1.data)
l1 = l1.next
while l2 is not None:
sum_list.add_node(l2.data)
l2 = l2.next
return sum_list
l1 = LinkedList()
l2 = LinkedList()
n1 = [5, 1, 3]
n2 = [2, 9, 5]
for i in n1:
l1.add_node(i)
for i in n2:
l2.add_node(i)
sum_list = add_linked_lists(l1.head, l2.head)
sum_list.print_list()