本文整理汇总了Python中LinkedList类的典型用法代码示例。如果您正苦于以下问题:Python LinkedList类的具体用法?Python LinkedList怎么用?Python LinkedList使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LinkedList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: removeElements
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
list = LinkedList(head)
list.remove(val)
return list.head()
示例2: reverseList
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
list = LinkedList(head)
list.reverse()
return list.head()
示例3: test_when_there_are_two_items_added_to_list_that_point_to_each_other_should_report_it_as_a_cycle
def test_when_there_are_two_items_added_to_list_that_point_to_each_other_should_report_it_as_a_cycle(self):
linked_list = LinkedList()
linked_list.add('test item 0')
linked_list.add('test item 1')
node0 = linked_list.get(0)
node1 = linked_list.get(1)
node1.next_node = node0
self.assertTrue(linked_list.has_loop())
示例4: Test
def Test():
l = LinkedList(0)
for i in range (1,5):
l.AddNode(i)
last = l.last()
last.next = l.root.next.next
n = Detectcircle(l.root)
print n
m = FindFirstInloop(l.root)
print m
示例5: Stack
class Stack(IReadOnlyCollection):
""" Represents a generic stack. """
# Remarks:
# T is the type of item on the stack.
def __init__(self, dataContainer = None):
""" Creates a new stack instance that uses the specified list to store its data. """
if dataContainer is None:
self.data_container = None
self.data_container = LinkedList()
return
self.data_container = dataContainer
def push(self, Item):
""" Pushes an item on the stack. """
self.data_container.insert(0, Item)
def pop(self):
""" Pops the item at the top of the stack. """
# Pre:
# The stack may not be empty.
# Post:
# If the stack was empty, the stack's state will not change, and None will be returned.
if self.is_empty:
return None
value = self.data_container[0]
self.data_container.remove_at(0)
return value
def __iter__(self):
""" Creates an iterator that iterates over every element in the collection. """
return self.data_container.__iter__()
@property
def is_empty(self):
""" Gets a boolean value that indicates whether the stack is empty or not. """
return self.count == 0
@property
def count(self):
""" Gets the number of items on the stack. """
return self.data_container.count
@property
def top(self):
""" Peeks at the item at the top of the stack, without removing it. """
# Pre:
# The stack may not be empty.
# Post:
# If the stack was empty, None will be returned.
if self.is_empty:
return None
else:
return self.data_container[0]
示例6: partition
def partition(linkedList, x):
small, large = LinkedList(), LinkedList();
node = linkedList.head
while node:
if node.value < x:
small.addNode(node.value)
else:
large.addNode(node.value)
node = node.next
small.tail.next = large.head
return small
示例7: StackList
class StackList(object):
"""
StackList
"""
def __init__(self):
self.llista=LinkedList() # Initialize value
"""
Introdueix un element a la llista utilitzant el metode de la LinkedList.
:param:data, dada a introduir
:return: None
"""
def push(self, data):
# Type code here
self.llista.insertAfter(data)
"""
Extreu un element a la llista utilitzant el metode de la LinkedList.
:return: data Conte l'informació del node
"""
def pop(self):
# Type code here
data=self.llista.getTail()
self.llista.remove();
return data;
"""
Retorna l'element el primer element de la pila.
:return: data
"""
def head(self):
# Type code here
return self.llista.getTail()
"""
Esborra el contingut de tota la pila.
:return: None
"""
def purge(self):
self.llista.clear()
"""
Metode que s'utilitza per implementar la funcio len(). .
:param:data, dada a introduir
:return: int Longitud de la pila
"""
def __len__(self):
# Type code here
return self.llista.getSize()
"""
Metode que s'utilitza per implementar la funcio print().
:return string Retorna una cadena amb tots els elements de la pila.
"""
def __str__(self):
# Type code here
return self.llista.__str__()
示例8: test_when_3_items_have_been_added_and_then_first_item_removed_3_times_count_should_be_0
def test_when_3_items_have_been_added_and_then_first_item_removed_3_times_count_should_be_0(self):
linked_list = LinkedList()
linked_list.add('test item 0')
linked_list.add('test item 1')
linked_list.add('test item 2')
linked_list.remove(0)
linked_list.remove(0)
linked_list.remove(0)
self.assertEqual(linked_list.count, 0)
示例9: find_nth_to_last
def find_nth_to_last(head, n):
dummy = LinkedList()
dummy.next = head
p = dummy
while n > 0 and p.next:
p = p.next
n -= 1
pp = dummy
while pp.next and p.next:
pp = pp.next
p = p.next
return pp.next
示例10: HashTable
class HashTable():
buckets = 0
hash_func = None
match_func = None
destroy_fun = None
size = 0
table = None
def __init__(self,bucks, hasher, matcher,destroy):
self.buckets = bucks
self.hash_func = hasher
self.match_func = matcher
self.destroy_func = destroy
self.table = LinkedList();
for i in range(0, self.buckets):
self.table.attach(LinkedList())
return
def Table_destroy(self):
for i in range(0,self.buckets):
self.destroy(self.table.getElement())
self.table = None
return
def Table_insert(self, value):
to_insert = value
x = self.Table_lookup(to_insert)
if x == 0:
return 1
bucket = self.hash_func(value) % self.buckets
self.table.get(bucket).attach(value)
return
def Table_remove(self, value):
bucket = self.hash_func(value) % self.buckets
self.table.get(bucket).delete(value)
return
def Table_lookup(self, value):
bucket = self.hash_func(value) % self.buckets
print "Bucket", bucket
if not (self.table.get(bucket).isEmpty()):
x = self.table.get(bucket).getvalue()
else:
return 1
return 1
def Table_size(self):
return self.table.size()
示例11: __init__
class Stack:
def __init__(self):
self.stack = LinkedList()
def push(self,item):
self.stack.addToHead(Node(item,None))
def pop(self):
return self.stack.removeFromHead().data
def size(self):
return self.stack.size
def list(self):
return self.stack.list()
示例12: put
def put(self, key, value):
llist = self.links[self.hash(key)]
if llist == None:
node = Link(key = key, value = value)
llist = LinkedList(head=node)
self.links[self.hash(key)] = llist
return
cur_node = llist.head
while cur_node != None:
if cur_node.key == key:
cur_node.value = value
return
else:
cur_node = cur_node.next
llist.push(Link(key = key, value = value))
示例13: __init__
def __init__(self, dataContainer = None):
""" Creates a new stack instance that uses the specified list to store its data. """
if dataContainer is None:
self.data_container = None
self.data_container = LinkedList()
return
self.data_container = dataContainer
示例14: main
def main():
# make the nodes from the .csv file and put them into a linked list
with open('output.csv', 'r') as csvFile:
lists = LinkedList.linkedList()
for row in csvFile:
diseaseText = repr(row.strip())
lists.newNode(diseaseText)
# print the list to the console (currently prints in reverse order of addition to the list)
currentNode = lists.head
while currentNode.next != None:
print currentNode.cargo
currentNode = currentNode.next
# print only the diseases beginning with user inputted letter
hasDiseases = False
print " "
searchFor = raw_input("Enter a letter to narrow search (A-G): ")
print " "
currentNode = lists.head
while currentNode.next != None:
if currentNode.cargo[1] == searchFor:
print currentNode.cargo
hasDiseases = True
currentNode = currentNode.next
if hasDiseases == False:
print "List not extensive enough, no diseases found."
print " "
示例15: parseList
def parseList(self, index, s):
li=LinkedList();
i=0;
for i in range (index, len(s)):
# If it is a list
if s[i] =='(' or s[i] == '\'':
if i != index:
index, tmp=self.parseList(i+1, s);
li.prepend(tmp);
else:
continue;
elif s[i] == ')':
break;
else:
li.prepend(s[i]);
return i, li;