当前位置: 首页>>代码示例>>Python>>正文


Python LinkedList.pop方法代码示例

本文整理汇总了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
开发者ID:adusca,项目名称:SmallAlgos,代码行数:36,代码来源:3_6.py

示例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())
开发者ID:PeteRichardson,项目名称:practice-python,代码行数:12,代码来源:test_linkedlist.py

示例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
开发者ID:adusca,项目名称:SmallAlgos,代码行数:24,代码来源:3_2.py

示例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))
#.........这里部分代码省略.........
开发者ID:PeteRichardson,项目名称:practice-python,代码行数:103,代码来源:test_linkedlist.py


注:本文中的linkedlist.LinkedList.pop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。