本文整理汇总了Python中linkedlist.LinkedList类的典型用法代码示例。如果您正苦于以下问题:Python LinkedList类的具体用法?Python LinkedList怎么用?Python LinkedList使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LinkedList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: reverse_in_place
def reverse_in_place(ll):
"""
Steps:
a -> b -> c
<- a b -> c * When we are changing b we need references to a and c
<- a <- b c
<- a <- b <- c
"""
if ll.length < 2:
return ll
previous = ll.head
current = previous.next
later = current.next
previous.next = None
while True:
current.next = previous
previous = current
current = later
if current is not None:
later = current.next
else:
break
reverse_list = LinkedList()
reverse_list.head = previous
return reverse_list
示例2: create_random_list
def create_random_list(self):
random_list = LinkedList()
x = random.randint(1, 200)
for i in range(0, x):
value = random.randint(1, 10000)
random_list.append(value)
return random_list
示例3: test_last_item_point_to_none
def test_last_item_point_to_none(self):
'''Item at the back has None as next pointer'''
ll = LinkedList()
ll.add_to_back(0, 23)
assert ll.back.next == None
ll.add_to_back(123, 234)
assert ll.back.next == None
示例4: test_reverse_single
def test_reverse_single(self):
ll = LinkedList(358)
self.assertEqual(358, ll.peek())
self.assertEqual(1, len(ll))
ll.reverse()
self.assertEqual(358, ll.peek())
self.assertEqual(1, len(ll))
示例5: test_in_filter_None_equal_to_identity
def test_in_filter_None_equal_to_identity(self):
ll = LinkedList()
item1 = ll.add_to_front(123, 8907067)
ll.in_filter(None)
assert item1.key == ll.front.key
assert item1.value == ll.front.value
assert ll.length == 1
示例6: test_in_filter_preserves_front_and_back_if_needed
def test_in_filter_preserves_front_and_back_if_needed(self):
ll = LinkedList()
item1 = ll.add_to_front(123, 123123)
item2 = ll.add_to_front(234234, 92834923)
ll.in_filter(None)
assert ll.front == item2
assert ll.back == item1
示例7: test_prev_from_front_is_none
def test_prev_from_front_is_none(self):
'''Item at the front has None as prev pointer'''
ll = LinkedList()
ll.add_to_front(0, 98)
assert ll.front.prev == None
ll.add_to_front(123, 98)
assert ll.front.prev == None
示例8: __init__
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
示例9: addition
def addition(llist1,llist2):
curr1 = llist1.head
curr2 = llist2.head
myList = LinkedList()
carry = 0
temp = None
while(curr1 is not None or curr2 is not None):
lvalue= curr1.data if curr1 else 0
rvalue= curr2.data if curr2 else 0
val = lvalue + rvalue + carry
carry = 1 if val >10 else 0
val = val % 10
newNode = Node(val)
if myList.head is None:
myList.head = newNode
temp = myList.head
else:
temp.next = newNode
temp = temp.next
if curr1 is not None:
curr1 = curr1.next
if curr2 is not None:
curr2 = curr2.next
if carry>0:
temp.next = Node(carry)
return myList
示例10: test_insert_two
def test_insert_two():
l_list = LinkedList()
l_list.insert("David")
l_list.insert("Thomas")
assert l_list.head.get_data() == "Thomas"
head_next = l_list.head.get_next()
assert head_next.get_data() == "David"
示例11: test_remove_updates_head_if_necessary
def test_remove_updates_head_if_necessary(self):
'''Removing head, updates head'''
ll = LinkedList()
item1 = ll.add_to_front(1234)
item2 = ll.add_to_front(list())
assert ll.front == item2
ll.remove(item2)
assert ll.front == item1
示例12: test_filter_creates_new_items
def test_filter_creates_new_items(self):
ll = LinkedList()
item1 = ll.add_to_front(12, 3534536)
ll2 = ll.filter(None)
item2 = ll2.front
assert item1 != item2
assert item1.key == item2.key
assert item2.value == item2.value
示例13: test_insert_before
def test_insert_before(self):
llist = LinkedList()
for i in xrange(10):
llist.push_back(i)
for item in llist:
if item.value % 2 == 0:
llist.insert_before(item, item.value)
self.assertEqual([0,0,1,2,2,3,4,4,5,6,6,7,8,8,9], [node.value for node in llist])
示例14: test_insert_before_return_new_item
def test_insert_before_return_new_item(self):
ll = LinkedList()
item1 = ll.add_to_front(5151, 91951)
key = 123
value = [1,2,3]
item2 = ll.insert_before(item1, key, value)
assert item2.key == key
assert item2.value == value
示例15: test_remove_all_items_deletes_front_and_back
def test_remove_all_items_deletes_front_and_back(self):
ll = LinkedList()
item1 = ll.add_to_back("lkjh")
item2 = ll.add_to_back([1,2,3])
ll.remove(item2)
ll.remove(item1)
assert ll.front == None
assert ll.back == None