当前位置: 首页>>代码示例>>Python>>正文


Python LinkedList.LinkedList类代码示例

本文整理汇总了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
开发者ID:aihex,项目名称:cracking-the-coding-interview-python,代码行数:30,代码来源:Q2.4.py

示例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;
开发者ID:anconaesselmann,项目名称:pySearch,代码行数:31,代码来源:IndexList.py

示例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
开发者ID:prathamtandon,项目名称:python-algorithms-and-data-structures,代码行数:7,代码来源:isPalindrome.py

示例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)
开发者ID:bertothunder,项目名称:coursera-algorithms,代码行数:30,代码来源:LLQueue.py

示例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
开发者ID:vivekkeshore,项目名称:ctci,代码行数:25,代码来源:list_sum_2_5.py

示例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()
开发者ID:IoDmitri,项目名称:Python-Datastructures-and-Algorithims,代码行数:33,代码来源:Queue.py

示例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
开发者ID:arpagon,项目名称:assignaments,代码行数:30,代码来源:fit.py

示例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)
开发者ID:ZachLiuGIS,项目名称:Algorithm-Enthusiasts,代码行数:26,代码来源:add_two_numbers.py

示例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
开发者ID:ZachLiuGIS,项目名称:Algorithm-Enthusiasts,代码行数:7,代码来源:check_palindrome.py

示例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)
开发者ID:ZachLiuGIS,项目名称:Algorithm-Enthusiasts,代码行数:29,代码来源:test.py

示例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"
开发者ID:pombredanne,项目名称:CrackCode,代码行数:7,代码来源:TestLinkedList.py

示例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])
开发者ID:xpspectre,项目名称:cell-image,代码行数:7,代码来源:test_LinkedList.py

示例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
开发者ID:EthanSeaver,项目名称:Python-Projects,代码行数:26,代码来源:Queue.py

示例14: testPrepend

def testPrepend():
	my_linked_list = LinkedList()

	for i in range(0,10):
		my_linked_list.prepend(i)

	print my_linked_list
开发者ID:WillYee,项目名称:LinkedListDemo,代码行数:7,代码来源:LinkedListTest.py

示例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)
开发者ID:aes421,项目名称:VisualLinkedList,代码行数:26,代码来源:Driver.py


注:本文中的LinkedList.LinkedList类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。