本文整理汇总了Python中data_structures.linked_list.LinkedList.set_at_index方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.set_at_index方法的具体用法?Python LinkedList.set_at_index怎么用?Python LinkedList.set_at_index使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类data_structures.linked_list.LinkedList
的用法示例。
在下文中一共展示了LinkedList.set_at_index方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestLinkedList
# 需要导入模块: from data_structures.linked_list import LinkedList [as 别名]
# 或者: from data_structures.linked_list.LinkedList import set_at_index [as 别名]
#.........这里部分代码省略.........
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')
self.ll[0] = 'Data2'
self.assertEqual(self.ll['head'], 'Data2', 'Set at 0 should update head pointer in linked list with one item')
self.assertEqual(self.ll['tail'], 'Data2', 'Set at 0 should update tail pointer in linked list with one item')
self.ll[1] = 'Data3'
self.assertEqual(self.ll['head'], 'Data2', 'Set at 1 should not update head pointer in linked list with one item')
self.assertEqual(self.ll['tail'], 'Data3', 'Set at 1 should update tail pointer in linked list with one item')
def test_negative_set(self):
with self.assertRaises(IndexError, msg='Set at negative out of bounds should raise IndexError'):
self.ll.set_at_index(-2, 'Data')
self.ll.set_at_index(-1, 'Data1')
self.assertEqual(self.ll['head'], 'Data1', 'Set at -1 should update head pointer in linked list with no items')
self.assertEqual(self.ll['tail'], 'Data1', 'Set at -1 should update tail pointer in linked list with no items')
self.ll.set_at_index(-1, 'Data2')
self.assertEqual(self.ll['head'], 'Data2', 'Set at -1 should update head pointer in linked list with one item')
self.assertEqual(self.ll['tail'], 'Data2', 'Set at -1 should update tail pointer in linked list with one item')
self.ll.set_at_index(-2, 'Data3')
self.assertEqual(self.ll['head'], 'Data3', 'Set at -2 should update head pointer in linked list with one item')
self.assertEqual(self.ll['tail'], 'Data2', 'Set at -2 should not update tail pointer in linked list with one item')
self.ll.set_at_index(-3, 'Data4')
self.assertEqual(self.ll['head'], 'Data3', 'Set at -3 should update head pointer in linked list with multiple items')
self.assertEqual(self.ll['tail'], 'Data2', 'Set at -3 should not update tail pointer in linked list with multiple items')
def test_remove_items(self):
with self.assertRaises(ValueError, msg='Remove functions should raise ValueError on empty linked list'):
self.ll.remove_item(None)
with self.assertRaises(ValueError, msg='Remove functions should raise ValueError on empty linked list'):
self.ll.remove_head()
with self.assertRaises(ValueError, msg='Remove functions should raise ValueError on empty linked list'):
self.ll.remove_tail()
self.ll = LinkedList(1, 2, 3, 4, 5, 3)
self.ll.remove_item(2)
self.assertFalse(2 in self.ll)
self.ll.remove_item(3)
self.assertTrue(3 in self.ll)
self.ll.remove_head()
self.assertEqual(self.ll['head'], 4)
with self.assertRaises(ValueError):
self.ll.remove_item('None')
with self.assertRaises(ValueError):
self.ll.remove_item(None)