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


Python LinkedList.insert_at_tail方法代码示例

本文整理汇总了Python中data_structures.linked_list.LinkedList.insert_at_tail方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.insert_at_tail方法的具体用法?Python LinkedList.insert_at_tail怎么用?Python LinkedList.insert_at_tail使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在data_structures.linked_list.LinkedList的用法示例。


在下文中一共展示了LinkedList.insert_at_tail方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestLinkedList

# 需要导入模块: from data_structures.linked_list import LinkedList [as 别名]
# 或者: from data_structures.linked_list.LinkedList import insert_at_tail [as 别名]
class TestLinkedList(unittest.TestCase):

    def setUp(self):
        self.ll = LinkedList()

    def test_init(self):
        ll = LinkedList()
        self.assertTrue(ll.is_empty(), msg="initalizing an empty linked list should be empty")
        ll = LinkedList('Data')
        self.assertFalse(ll.is_empty(), msg="initalizing a linked list with one element should not be empty")
        ll = LinkedList(['Data', 'Data2'])
        self.assertFalse(ll.is_empty(), msg="initalizing a linked list with one list should not be empty")
        ll = LinkedList('Data', 'Data2', 'Data3')
        self.assertFalse(ll.is_empty(), msg="initalizing a linked list with multiple element should not be empty")

    def test_init_with_list(self):
        self.ll = LinkedList([1, 2, 3, 4])
        self.assertEqual(self.ll['head'], [1, 2, 3, 4])
        self.ll = LinkedList(*[1, 2, 3, 4])
        self.assertEqual(self.ll['head'], 1)
        self.assertEqual(self.ll['tail'], 4)

    def test_get(self):
        with self.assertRaises(IndexError, msg="Get at head/tail from empty linked list should return IndexError"):
            self.ll.get_at_head()
            self.ll.get_at_tail()
        self.ll = LinkedList(1, 2, 3, 4)
        self.assertEqual(self.ll.get_at_head(), 1, 'Get at head should get head data')
        self.assertEqual(self.ll.get_at_tail(), 4, 'Get at tail should get tail data')
        self.assertEqual(self.ll.get_at_index(0), 1, 'Get at index 0 should get first item')
        self.assertEqual(self.ll.get_at_index(2), 3, 'Get at index 2 should get third item')
        self.assertEqual(self.ll.get_at_index(2), self.ll[2])
        self.assertEqual(self.ll.get_at_head(), self.ll['head'])
        self.assertEqual(self.ll.get_at_tail(), self.ll['tail'])
        with self.assertRaises(IndexError, msg='Get at out of bounds index should throw an IndexError'):
            self.ll[len(self.ll)]
        self.assertEqual(self.ll[-1], 4, 'Get at index -1 should return tail item')
        self.assertEqual(self.ll[-2], 3, 'Get at index -2 should return tail item')
        self.assertEqual(self.ll[len(self.ll) - 1], 4, 'Get at last index should return tail item')
        self.assertEqual(self.ll[-len(self.ll)], 1, 'Get at index (-length of list) should return head item')

    def test_get_slices(self):
        self.ll = LinkedList(1, 2, 3, 4, 5)
        self.assertEqual(self.ll[:], LinkedList(1, 2, 3, 4, 5), "Get slice '[:]' should return a shallow copy of the list")
        self.assertEqual(self.ll[2:], LinkedList(3, 4, 5), "Get slice '[2:]' should return a shallow subcopy of the list")
        self.assertEqual(self.ll[:2], LinkedList(1, 2), "Get slice '[:2]' should return a shallow subcopy of the list")
        self.assertEqual(self.ll[1:3], LinkedList(2, 3), "Get slice '[1:3]' should return a shallow subcopy of the list")
        self.assertEqual(self.ll[1:1], LinkedList(), "Get slice '[1:1]' should return an empty linked list")
        self.assertEqual(self.ll[5:], LinkedList(), "Get slice '[5:]' should return an empty linked list")
        self.assertEqual(self.ll[-3:], LinkedList(3, 4, 5), "Get slice '[-3:]' should return a shallow subcopy of the list")
        self.assertEqual(self.ll[:-3], LinkedList(1, 2), "Get slice '[:-3]' should return a shallow subcopy of the list")
        self.assertEqual(self.ll[-4:-2], LinkedList(2, 3), "Get slice '[-4:-2]' should return a shallow subcopy of the list")
        self.assertEqual(self.ll[-2:-2], LinkedList(), "Get slice '[:2]' should return a shallow subcopy of the list")
        self.assertEqual(self.ll[:-5], LinkedList(), "Get slice '[:-5]' should return an empty linked list")
        self.assertEqual(self.ll[1:-5], LinkedList(), "Get slice '[1:-5]' should return an empty linked list, values stop is less than start")
        self.assertEqual(self.ll[::-1], LinkedList(5, 4, 3, 2, 1))
        # self.assertEqual(self.ll[1:-5:-1], LinkedList(2, 1), "Get slice '[1:-5:-1]' should return an shallow subcopy of the linked list")

    def test_insert_at_head(self):
        self.ll.insert_at_head('Data1')
        self.assertEqual(self.ll['head'], 'Data1', 'Insert at head should update head pointer in linked list with no items')
        self.assertEqual(self.ll['tail'], 'Data1', 'Insert at head should update tail pointer in linked list with no items')
        self.ll.insert_at_head('Data2')
        self.ll.insert_at_head('Data3')
        self.ll.insert_at_head('Data4')
        self.ll.insert_at_head('Data5')
        self.ll.insert_at_head('Data6')
        self.assertEqual(self.ll['head'], 'Data6', 'Insert at head should update head pointer in linked list with many items')
        self.assertEqual(self.ll['tail'], 'Data1', 'Insert at head should not change tail pointer in linked list with many items')
        self.assertEqual(self.ll[0], 'Data6', 'Get at index 0 should return first item')
        self.assertEqual(self.ll[2], 'Data4', 'Get at index 2 should return third item')

    def test_insert_at_tail(self):
        self.ll.insert_at_tail('Data1')
        self.assertEqual(self.ll['head'], 'Data1', 'Insert at tail should update head pointer with no items')
        self.assertEqual(self.ll['tail'], 'Data1', 'Insert at tail should update tail pointer with no items')
        self.ll.insert_at_tail('Data2')
        self.ll.insert_at_tail('Data3')
        self.ll.insert_at_tail('Data4')
        self.ll.insert_at_tail('Data5')
        self.ll.insert_at_tail('Data6')
        self.assertEqual(self.ll['head'], 'Data1', 'Insert at tail should update head pointer in linked list with many items')
        self.assertEqual(self.ll['tail'], 'Data6', 'Insert at tail should not change tail pointer in linked list with many items')
        self.assertEqual(self.ll[2], 'Data3', 'Get at index 2 should return third item')

    def test_insert_at_index(self):
        self.ll = LinkedList('Data1', 'Data2', 'Data3', 'Data4', 'Data5')
        self.ll.insert_at_index(3, 'Data3.5')
        self.assertEqual(self.ll[3], 'Data3.5', 'Insert at index should set the data at the index')
        self.assertEqual(self.ll[2], 'Data3', 'Insert at index should not change the data at the index before it')
        self.assertEqual(self.ll[4], 'Data4', 'Insert at index should move the other data up an index')
        with self.assertRaises(IndexError, msg='Insert at out of bounds index should raise IndexError'):
            self.ll.insert_at_index(len(self.ll) + 1, 'Data7')

    def test_positive_set(self):
        with self.assertRaises(IndexError, msg='Set at positive out of bounds should raise IndexError'):
            self.ll[5] = 'Data'
        self.ll[0] = 'Data1'
        self.assertEqual(self.ll['head'], 'Data1', 'Set at 0 should update head pointer in linked list with no items')
        self.assertEqual(self.ll['tail'], 'Data1', 'Set at 0 should update tail pointer in linked list with no items')
#.........这里部分代码省略.........
开发者ID:ignat980,项目名称:CS-Theory,代码行数:103,代码来源:test_linked_list.py


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