本文整理汇总了Python中linkedlist.LinkedList.pop方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.pop方法的具体用法?Python LinkedList.pop怎么用?Python LinkedList.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.pop方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import pop [as 别名]
class AnimalShelter:
def __init__(self):
self.dogs = LinkedList()
self.cats = LinkedList()
self.counter = 0
def enqueue(self, animal):
if animal.species == 'dog':
self.dogs.add((animal, self.counter))
elif animal.species == 'cat':
self.cats.add((animal, self.counter))
else:
raise Exception("Unsupported animal type.")
self.counter += 1
def dequeue_dog(self):
return self.dogs.pop()
def dequeue_cat(self):
return self.cat.pop()
def dequeue_any(self):
dog = self.dogs.head
cat = self.cats.head
if dog is None:
return cat
if cat is None:
return dog
if dog.value[1] > cat.value[1]:
return cat
else:
return dog
示例2: test_pop_to_empty
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import pop [as 别名]
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())
示例3: __init__
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import pop [as 别名]
class Stack:
def __init__(self):
self.stack = LinkedList()
self.min_stack = LinkedList()
def push(self, value):
self.stack.add(value)
if self.min_stack.head is not None:
self.min_stack.add(min(value, self.min_stack.head.value))
else:
self.min_stack.add(value)
def pop(self):
self.min_stack.pop()
return self.stack.pop()
def min(self):
return self.min_stack.head.value
def empty(self):
return self.stack.head is None
示例4: Test_LinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import pop [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))
#.........这里部分代码省略.........