本文整理汇总了Python中linkedlist.LinkedList.add方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.add方法的具体用法?Python LinkedList.add怎么用?Python LinkedList.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.add方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [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: main
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
def main():
test = LinkedList()
for i in range(20):
test.add(i)
print(test)
print('Find the node before last', findKelementh(test, 2))
print('Find the last node', findKelementh(test, 1))
print('Finding 12th to last Node: ', findKelementh(test, 12))
示例3: reverse
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
def reverse(ll):
"""
Return a new LinkedList that is the reverse of ll without altering ll
"""
new = LinkedList()
node = ll.head
while node:
new.add(node.value)
node = node.next
return new
示例4: test_delete_node
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
def test_delete_node(self):
test_list = LinkedList()
test_list.add(1)
test_list = delete_node(test_list, test_list.head)
self.assertTrue(test_list.head is None)
for i in range(10):
test_list.add(val=i)
for node in test_list:
if node.value == 2:
delete = node
test_list = delete_node(test_list, delete)
self.assertFalse(test_list.get(delete.value))
示例5: partition
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
def partition(ll, k):
larger_list = LinkedList()
previous = ll.head
node = previous.next
while node is not None:
if node.value >= k:
previous.next = node.next
larger_list.add(node.value)
else:
previous = previous.next
node = node.next
previous.next = larger_list.head
return ll
示例6: is_palindrome2
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
def is_palindrome2(ll):
nl = LinkedList()
size = ll.length/2
node = ll.head
for i in range(size):
nl.add(node.value)
node = node.next
node2 = nl.head
if ll.length % 2 == 1:
node = node.next
while node:
if node.value != node2.value:
return False
node = node.next
node2 = node2.next
return True
示例7: bfs
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
def bfs(node):
ans = []
line = deque()
dist = -1
line.append((node, 0))
new_l = None
while line:
current, cur_dist = line.popleft()
if cur_dist == dist:
new_l.add(current)
else:
new_l = LinkedList()
ans.append(new_l)
new_l.add(current)
dist = cur_dist
for v in (current.left, current.right):
if v:
line.append((v, cur_dist + 1))
return ans
示例8: __init__
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [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
示例9: LinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
from linkedlist import LinkedList
myLL = LinkedList()
myLL.add(10)
myLL.add(11)
myLL.add(12)
myLL.add(13)
myLL.erase(10)
myLL.add(14)
myLL.add(15)
# myLL.invert()
myLL.print_list()
square = lambda x: x * x
myLL.map(square)
# myLL.print_list()
print(myLL.selfie())
print(myLL.reduce("+"))
print(myLL.reduce("*"))
print(myLL.length)
示例10: LinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
from linkedlist import LinkedList
_list = LinkedList()
_list.add(10)
_list.add(11)
_list.add(12)
_list.add(13)
print _list.find(11)
print _list.remove(11)
示例11: LinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
from linkedlist import LinkedList
SCORE_LIST = LinkedList()
with open("GAME.dat", 'r') as infile:
for line in infile:
player_id, score = line[:-1].split()
SCORE_LIST.add(player_id.lower(), score)
def val_rank_range(rank_range, linkedlist):
try:
lower, upper = rank_range.split('-') # check correct format ("X-Y")
except:
return False
if (lower.isdigit() and upper.isdigit()
and int(lower) > 0 and int(upper) > 0
and int(lower) < int(upper)
and int(upper) <= linkedlist.get_max_rank()):
return True
else:
return False
def display_rank(linkedlist):
if linkedlist.isEmpty():
print("- List is empty -")
return # EXIT
while True:
示例12:
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import add [as 别名]
if((i!=0)&(j!=0)):
f1=maxSim(list1[j-1],list2[i-1])
n=n+1
if((j!=(l1-1))&(i!=(l2-1))):
f2=maxSim(list1[j+1],list2[i+1])
n=n+1
num2=num2+((1.0+f1+f2)/n)*idf(list2[i])"""
num2=num2+idf(list2[i])
j=j+1
i=i+1
count=0
sim=0.5*((num1/deno1)+(num2/deno2))
if(sim>.7): #why .7 ---- Jaccard's
l.add(stok1[0],pos_count)
pos_count=pos_count+1
l.addsen(stok1[1],pos_count,l.search(stok1[0]))
pos_count=pos_count+1
count=1
#print(l.head.child.data)
else:#two clusters
l.add(stok1[0],pos_count)
pos_count=pos_count+1
l.add(stok1[1],pos_count)
pos_count=pos_count+1
count=2
#print(l.head.child.data)
#print(l.head.next.child.data)
#print("aaaaa\n")