本文整理汇总了Python中LinkedList.LinkedList类的典型用法代码示例。如果您正苦于以下问题:Python LinkedList类的具体用法?Python LinkedList怎么用?Python LinkedList使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LinkedList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_link
def add_link(head1, head2):
carry = 0
dummy1 = LinkedList()
dummy2 = LinkedList()
dummy1.next = head1
dummy2.next = head2
p1 = dummy1
p2 = dummy2
while p1.next and p2.next:
carry += p1.next.val + p2.next.val
p1.next.val = carry % 10
carry /= 10
p1 = p1.next
p2 = p2.next
while p1.next:
carry += p1.next.val
p1.next.val = carry % 10
carry /= 10
p1 = p1.next
while p2.next:
carry += p2.next.val
p1.next = LinkedList(carry % 10)
carry /= 10
p1 = p1.next
p2 = p2.next
if carry:
p1.next = LinkedList(carry)
return dummy1.next
示例2: Doc
class Doc():
def __init__(self, doc):
self.doc = doc;
self.lines = LinkedList();
self.count = 1;
self._iterator = None;
self.documentFrequency = None;
def reset(self):
self.lines.reset();
self._iterator = None;
def __str__(self):
return '\n\t' + str(self.doc) + ', count:' + str(self.count) + self.lines.toString(',');
def __lt__(self, other):
return self.doc < other.doc;
def __eq__(self, other):
return self.doc == other.doc;
def insertLine(self, line):
line = IndexList.Line(int(line));
self.lines.insertSorted(line);
def __iter__(self):
return self;
def next(self):
if self._iterator is None:
if self.lines._head is None: raise StopIteration
self._iterator = self.lines._head;
return self._iterator.value;
self._iterator = self._iterator.next;
if self._iterator is None: raise StopIteration
return self._iterator.value;
示例3: reverse
def reverse(lst):
reverse_lst = LinkedList()
cur = lst.head
while cur is not None:
reverse_lst.insert(cur.get_key())
cur = cur.get_next()
return reverse_lst
示例4: LLQueue
class LLQueue(object):
def __init__(self, size=0):
self._list = LinkedList()
self._size = size
def enqueue(self, data):
if (self._size > 0 and self._list.count < self._size):
self._list.add(data)
else:
raise Exception("Too many items in the queue")
def dequeue(self):
if (self._list.count == 0):
raise Exception("No items in the queue")
node = self._list[0]
del self._list[0]
return node
def isEmpty(self):
return (self._list.count == 0)
def clear(self):
self._list.clear()
@property
def size(self):
return self._size
def __len__(self):
return len(self._list)
示例5: sum_list
def sum_list(l1, l2):
"""
returns a list which contains the addition of input two lists.
"""
lsum = LinkedList()
carry = 0
l1_node = l1.head
l2_node = l2.head
while l1_node or l2_node:
val = carry
if l1_node:
val += l1_node.value
l1_node = l1_node.next
if l2_node:
val += l2_node.value
l2_node = l2_node.next
lsum.add(val % 10)
carry = val / 10
if carry:
lsum.add(carry)
return lsum
示例6: __init__
class Queue:
def __init__(self):
self._ls = LinkedList()
self.size = 0
def __str__(self):
return "Queue contains:{0}".format(self._ls)
def __iter__(self):
return self
def next(self):
if not self.isEmpty():
j = self.dequeue()
return j
else:
raise StopIteration()
def enqueue(self,n):
self._ls.append(n,None)
self.size += 1
def dequeue(self):
j = self._ls.popHead()
self.size -= 1
return j.key
def isEmpty(self):
return self.size == 0
def show(self):
self._ls.show()
示例7: read_file
def read_file(file):
'''
Read CSV File, Important Only Read column 1 and column 2
Parameters
----------
file : str, path of file
path for file to read and fill dataset
Returns
-------
dataset: LinkedLink dataset
'''
#Open File
with open(file, 'rb') as csvfile:
reader = csv.reader(csvfile)
dataset=LinkedList()
#for row in file:
for row in reader:
#if length of row >= 2:
if len(row) >= 2:
#add to LinkedList Float Row[0] and Row[1]
try:
dataset.add(( float(row[0]) , float(row[1]) ))
except ValueError:
log.warning("%s Problem whit row" % row[0])
else:
#mesnsaje: row whit insuficient data.
log.warning("row whit insuficient data or Empty Row")
return dataset
示例8: add_two_numbers_adding_digit_reverse
def add_two_numbers_adding_digit_reverse(lst1, lst2):
lst = LinkedList()
current_1 = lst1.head
current_2 = lst2.head
carry = 0
def get_digit(node1, node2):
nonlocal carry
num1 = 0 if node1 is None else node1.get_data()
num2 = 0 if node2 is None else node2.get_data()
value = num1 + num2 + carry
if value >= 10:
carry = 1
value %= 10
else:
carry = 0
return value
current_lst = lst.head
while current_1 is not None or current_2 is not None or carry > 0:
digit = get_digit(current_1, current_2)
lst.insert(digit)
current_1 = current_1.get_next() if current_1 is not None else None
current_2 = current_2.get_next() if current_2 is not None else None
return reverse_list(lst)
示例9: reverse_list
def reverse_list(lst):
new_lst = LinkedList()
current = lst.head
while current:
new_lst.insert(current.get_data())
current = current.get_next()
return new_lst
示例10: setUp
def setUp(self):
self.lst1 = LinkedList()
self.lst2 = LinkedList()
self.common_lst = LinkedList()
self.lst1.insert(4)
self.lst1.insert(2)
self.lst1.insert(5)
self.lst1.insert(8)
self.lst1.insert(6)
self.lst2.insert(3)
self.lst2.insert(10)
self.lst2.insert(8)
self.common_lst.insert(3)
self.common_lst.insert(2)
self.common_lst.insert(5)
self.common_lst.insert(7)
current = self.lst1.head
while current.get_next():
current = current.get_next()
current.set_next(self.common_lst.head)
current = self.lst2.head
while current.get_next():
current = current.get_next()
current.set_next(self.common_lst.head)
示例11: 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"
示例12: test_delete
def test_delete(self):
x = LinkedList([1,2,3,4,5])
val1 = x.delete(0) # 1st element
val2 = x.delete(-1) # last element
self.assertEqual(val1, 1)
self.assertEqual(val2, 5)
self.assertEqual(list(x), [2,3,4])
示例13: __init__
class Queue:
def __init__(self):
self.__elements = LinkedList()
# Adds an element to this queue
def enqueue(self, e):
self.__elements.add(e)
# Removes an element from this queue
def dequeue(self):
if self.getSize() == 0:
return None
else:
return self.__elements.removeAt(0)
# Return the size of the queue
def getSize(self):
return self.__elements.getSize()
# Returns a string representation of the queue
def __str__(self):
return self.__elements.__str__()
# Return true if queue is empty
def isEmpty(self):
return self.getSize() == 0
示例14: testPrepend
def testPrepend():
my_linked_list = LinkedList()
for i in range(0,10):
my_linked_list.prepend(i)
print my_linked_list
示例15: insert
def insert(UI):
#chop the UI to get only (position,item)
command = UI[6:]
#Regular expression: (\(\d*,.\))
expression = re.compile('(\(\d*,.*\))')
if (expression.match(command) != None):
#get the position
pos = command[1]
i = 2
while command[i] != ",":
pos += command[i]
i += 1
#Check that position is a number
if (~pos.isdigit()):
break
#get the item
item = command[i+1]
i += 2
while command[i] != ")":
item += command[i]
i += 1
#called insert with pos, item
LinkedList.insert(int(pos), item)