本文整理汇总了Python中linkedlist.LinkedList.insert_before方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.insert_before方法的具体用法?Python LinkedList.insert_before怎么用?Python LinkedList.insert_before使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.insert_before方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_insert_before
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import insert_before [as 别名]
def test_insert_before(self):
llist = LinkedList()
for i in xrange(10):
llist.push_back(i)
for item in llist:
if item.value % 2 == 0:
llist.insert_before(item, item.value)
self.assertEqual([0,0,1,2,2,3,4,4,5,6,6,7,8,8,9], [node.value for node in llist])
示例2: test_insert_before_updates_length
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import insert_before [as 别名]
def test_insert_before_updates_length(self):
ll = LinkedList()
item1 = ll.add_to_front(5151, 91951)
assert ll.length == 1
ll.insert_before(item1, 123, 234)
assert ll.length == 2
ll.insert_before(item1, 987234, "kjnsdfjn")
assert ll.length == 3
示例3: test_insert_before
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import insert_before [as 别名]
def test_insert_before(self):
ll = LinkedList()
item1 = ll.add_to_back(5151, 91951)
assert item1.prev == None
item2 = ll.insert_before(item1, 123, 234)
assert item1.prev == item2
assert item2.next == item1
item3 = ll.insert_before(item1, 987234, "kjnsdfjn")
assert item1.prev == item3
assert item3.prev == item2
assert item2.next == item3
assert item3.next == item1
示例4: test_insert_before_return_new_item
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import insert_before [as 别名]
def test_insert_before_return_new_item(self):
ll = LinkedList()
item1 = ll.add_to_front(5151, 91951)
key = 123
value = [1,2,3]
item2 = ll.insert_before(item1, key, value)
assert item2.key == key
assert item2.value == value
示例5: RuleList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import insert_before [as 别名]
class RuleList(object):
def __init__(self, max_size = 10):
self.__list = LinkedList()
self.__cmp = BoolCmp(1e-9)
self.__max_size = max_size
self.__rule_set = set()
def insert(self, informativity, rule):
if rule in self.__rule_set:
return
self.__rule_set.add(rule)
if not self.__list or self.__cmp(self.__list.back().value[0], informativity) > 0:
self.__list.push_back([informativity, rule])
else:
for node in self.__list:
if self.__cmp(node.value[0], informativity) > 0:
continue
if self.__cmp(node.value[0], informativity) == 0:
node.value.append(rule)
if self.__cmp(node.value[0], informativity) < 0:
self.__list.insert_before(node, [informativity, rule])
break
if len(self.__rule_set) - (len(self.__list.back().value) - 1) >= self.__max_size:
for item in self.__list.back().value[1:]:
self.__rule_set.remove(item)
self.__list.remove(self.__list.back())
def clear(self):
self.__list = LinkedList()
self.__rule_set = set()
def __iter__(self):
for node in self.__list:
for item in node.value[1:]:
yield node.value[0], item
def __len__(self):
return len(self.__rule_set)
def __contains__(self, rule):
return rule in self.__rule_set
示例6: test_cannot_insert_before_item_from_other_list
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import insert_before [as 别名]
def test_cannot_insert_before_item_from_other_list(self):
ll = LinkedList()
ll2 = LinkedList()
item_from_ll = ll.add_to_front(123, 123)
with pytest.raises(Exception):
ll2.insert_before(item_from_ll, 123, 1414)
示例7: test_cannot_insert_before_None
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import insert_before [as 别名]
def test_cannot_insert_before_None(self):
ll = LinkedList()
with pytest.raises(Exception):
ll.insert_before(None, 123, 1414)
示例8: test_insert_before_updates_front
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import insert_before [as 别名]
def test_insert_before_updates_front(self):
ll = LinkedList()
item1 = ll.add_to_back(5151, 91951)
assert ll.front == item1
item2 = ll.insert_before(item1, 123, 234)
assert ll.front == item2