本文整理汇总了Python中linkedlist.LinkedList.prepend方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.prepend方法的具体用法?Python LinkedList.prepend怎么用?Python LinkedList.prepend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.prepend方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_prepend
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import prepend [as 别名]
def test_prepend(self):
ll = LinkedList(3)
self.assertEqual(1, len(ll))
self.assertEqual(3, ll.peek())
ll.prepend(2)
self.assertEqual(2, len(ll))
self.assertEqual(2, ll.peek())
ll.prepend(1)
self.assertEqual(3, len(ll))
self.assertEqual(1, ll.peek())
示例2: main
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import prepend [as 别名]
def main():
""" don't modify this function. even if you do I have a copy of the original to test with """
mylist = LinkedList()
# add some initial nodes
mylist.append(10)
mylist.append(20)
mylist.append(30)
# this should display 10 20 30
print(mylist)
# add some nodes to the front
mylist.prepend(0)
mylist.prepend(-10)
# this should display: -10 0 10 20 30
print(mylist)
mylist.update(0, -20) # change -10 to -20
mylist.insert(2, 5) # insert 5 between 0 and 10
mylist.insert(5, 25) # insert 25 between 20 and 30
mylist.remove(1) # remove the 0
# this should display: -20 5 10 20 25 30
print(mylist)
mylist.remove(3) # removes 20
mylist.append(40) # add 40 to the end
mylist.append(50) # add 50 to the end
mylist.update(5, 45) # update 40 to 45
mylist.remove(5) # remove 45
mylist.update(5, 55) # update 50 to 55
mylist.prepend(-30) # add -30 to the front
# this should display -30 -20 5 10 25 30 55
print(mylist)
示例3: Test_LinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import prepend [as 别名]
class Test_LinkedList(unittest.TestCase):
def setUp(self):
self.ll = LinkedList(4)
self.ll.prepend(5)
def test_simple(self):
self.assertEqual(5, self.ll.peek())
self.assertEqual(2, len(self.ll))
def test_prepend(self):
ll = LinkedList(3)
self.assertEqual(1, len(ll))
self.assertEqual(3, ll.peek())
ll.prepend(2)
self.assertEqual(2, len(ll))
self.assertEqual(2, ll.peek())
ll.prepend(1)
self.assertEqual(3, len(ll))
self.assertEqual(1, ll.peek())
def test_append(self):
self.ll.append(6)
self.assertEqual(5, self.ll.peek())
self.assertEqual(3, len(self.ll))
self.assertEqual(4, self.ll.next.peek())
self.assertEqual(6, self.ll.next.next.peek())
def test_len(self):
self.ll.append(6)
self.assertEqual(3, len(self.ll))
def test_len_uninitialized(self):
ll = LinkedList()
ll.prepend(5)
self.assertEqual(1, len(ll))
def test_pop(self):
head_value = self.ll.pop()
self.assertEqual(5, head_value)
self.assertEqual(1, len(self.ll))
self.assertEqual(4, self.ll.peek())
def test_pop_to_empty(self):
ll = LinkedList(5)
self.assertEqual(1, len(ll))
self.assertEqual(ll.next, None)
self.assertFalse(ll.isEmpty())
head_value = ll.pop()
self.assertEqual(head_value, 5)
self.assertEqual(0, len(ll))
self.assertEqual(ll.next, None)
self.assertTrue(ll.isEmpty())
def test_isEmpty(self):
ll = LinkedList()
self.assertTrue(ll.isEmpty())
def test_pop_empty(self):
ll = LinkedList()
self.assertRaises(ListEmptyError, ll.pop)
def test_add(self):
ll = LinkedList(4)
ll.append(6)
ll2 = LinkedList(10)
ll2.append(12)
ll2.append(13)
ll = ll + ll2
self.assertEqual(5, len(ll))
def test_clear(self):
self.assertFalse(self.ll.isEmpty())
self.assertTrue(len(self.ll) == 2)
self.ll.clear()
self.assertTrue(self.ll.isEmpty())
def test_clear_empty_list(self):
ll = LinkedList()
ll.clear()
self.assertTrue(ll.isEmpty())
def test_reversed(self):
ll = LinkedList(7)
ll.append(8)
ll.append(9)
self.assertEqual(3, len(ll))
self.assertEqual(7, ll.peek())
self.assertEqual("7->8->9", str(ll))
rev = ll.reversed()
self.assertEqual("9->8->7", str(rev))
self.assertEqual(9, rev.peek())
self.assertEqual(3, len(ll))
self.assertEqual(7, ll.peek())
self.assertEqual("7->8->9", str(ll))
def test_reverse(self):
ll = LinkedList(7)
ll.append(8)
ll.append(9)
self.assertEqual(3, len(ll))
#.........这里部分代码省略.........
示例4: test_len_uninitialized
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import prepend [as 别名]
def test_len_uninitialized(self):
ll = LinkedList()
ll.prepend(5)
self.assertEqual(1, len(ll))
示例5: TestLinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import prepend [as 别名]
class TestLinkedList(unittest.TestCase):
def setUp(self):
self.list = LinkedList()
def test_element(self):
e = LinkedList.Element(self.list, 10)
self.assertEqual(e._list, self.list)
self.assertEqual(e.value, 10)
self.assertEqual(e.next, None)
def test_init(self):
self.assertEqual(self.list.head, None)
self.assertEqual(self.list.tail, None)
self.assertEqual(self.list.isEmpty, True)
# self.assertRaises(RuntimeError, self.list.first)
# self.assertRaises(RuntimeError, self.list.last)
def test_pend(self):
self.list.prepend(10)
self.assertEqual(self.list.head.value, 10)
self.list.prepend(11)
self.assertEqual(self.list.head.value, 11)
self.list.append(5)
self.assertEqual(self.list.last, 5)
self.assertEqual(self.list.isEmpty, False)
self.list.purge()
self.assertEqual(self.list.isEmpty, True)
def test_extract(self):
self.assertRaises(KeyError, self.list.extract, 10)
self.list.prepend(10)
self.list.prepend(5)
self.list.extract(10)
self.assertRaises(KeyError, self.list.extract, 6)
self.list.extract(5)
self.assertEqual(self.list.isEmpty, True)
def test_copy(self):
self.list.prepend(10)
self.list.prepend(5)
a = copy(self.list)
self.assertEqual(a.first, self.list.first)
self.assertEqual(a.last, self.list.last)
示例6: TestLinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import prepend [as 别名]
class TestLinkedList(unittest.TestCase):
def setUp(self):
"""Test setup"""
self.empty_list = LinkedList()
self.A_LIST_HEAD = 42
self.a_list = LinkedList(self.A_LIST_HEAD)
def test_new_list(self):
"""A criação de uma Nova Lista"""
self.assertIsInstance(self.empty_list, LinkedList)
def test_new_list_empty(self):
"""Uma Nova Lista criada sem parâmetros é vazia"""
self.assertTrue(self.empty_list.is_empty())
def test_empty_list_equality(self):
"""Listas Vazias são iguais"""
a = LinkedList()
b = LinkedList()
self.assertEqual(self.empty_list, a)
self.assertEqual(self.empty_list, b)
self.assertEqual(a, b)
def test_new_list_not_empty_with_value(self):
"""Uma Nova Lista criada com parâmetros é não-vazia"""
self.assertFalse(self.a_list.is_empty())
def test_empty_list_size(self):
"""Uma Lista vazia possui tamanho zero"""
self.assertEqual(self.empty_list.size(), 0)
def test_a_list_with_one_element_size_is_one(self):
"""Uma Lista com um elemento possui tamanho 1"""
self.assertEqual(self.a_list.size(), 1)
def test_a_list_head_is_the_first_element(self):
"""A cabeça de uma lista é seu primeiro elemento"""
"""Caso a lista seja vazia, o valor da cabeça é None"""
self.assertEqual(self.empty_list.head().value,None)
self.assertEqual(self.a_list.head().value, self.A_LIST_HEAD)
def test_a_list_tail_is_the_rest_of_the_list(self):
"""A cauda de uma lista são todos os elementos menos a cabeça"""
"""Caso a lista seja vazia ou tenha somente um elemento,
a Cauda é uma Lista Vazia"""
self.assertEqual(self.empty_list.tail(),LinkedList())
self.assertEqual(self.a_list.tail(), LinkedList() )
def test_append_changes_list_size(self):
"""Append insere um elemento no final da lista"""
"""A operação append aumenta o tamanho da lista em 1"""
size = self.a_list.size()
self.a_list.append(55)
new_size = size + 1
self.assertEqual(self.a_list.size(), new_size)
def test_append_changes_list_size_multiple_elements(self):
"""X appends aumentam o tamanho da lista em X elementos"""
size = self.a_list.size()
x = random.randint(1, 200)
for i in range(0, x):
value = random.randint(1, 10000)
self.a_list.append(value)
new_size = size + x
self.assertEqual(self.a_list.size(), new_size)
def test_append_empty_list_appended_element_is_head(self):
"""Append numa lista vazia torna o novo elemento a cabeça da lista"""
self.assertTrue(self.empty_list.is_empty())
self.assertTrue(self.empty_list.head().is_empty())
x = 42
self.empty_list.append(x)
self.assertEqual(self.empty_list.head().value, x)
def test_prepend_changes_list_size(self):
"""Prepend insere um elemento no início da lista"""
"""A operação prepend aumenta o tamanho da lista em 1"""
size = self.a_list.size()
self.a_list.prepend(98)
new_size = size + 1
self.assertEqual(self.a_list.size(), new_size)
def test_prepend_changes_list_size_multiple_elements(self):
"""X prepends aumentam o tamanho da lista em X elementos"""
size = self.a_list.size()
x = random.randint(1, 200)
#.........这里部分代码省略.........