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


Python LinkedList类代码示例

本文整理汇总了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()
开发者ID:bianXiaoTuan,项目名称:leetCode,代码行数:9,代码来源:RemoveLinkedListElements.py

示例2: reverseList

    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        list = LinkedList(head)
        list.reverse()

        return list.head()
开发者ID:bianXiaoTuan,项目名称:leetCode,代码行数:9,代码来源:ReverseLinkedList.py

示例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())
开发者ID:Restuta,项目名称:LinkedListTests,代码行数:9,代码来源:LinkedListTests.py

示例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
开发者ID:daisyang,项目名称:data_structure_and_algorithm,代码行数:11,代码来源:Detectcircle.py

示例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]
开发者ID:jonathanvdc,项目名称:datastructures-project,代码行数:54,代码来源:Stack.py

示例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
开发者ID:Haruuuko,项目名称:CTCI_5th,代码行数:11,代码来源:question2_4.py

示例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__()
开发者ID:emiliogq,项目名称:pyrobot,代码行数:53,代码来源:StackList.py

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

示例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
开发者ID:aihex,项目名称:cracking-the-coding-interview-python,代码行数:13,代码来源:Q2.2.py

示例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()
开发者ID:SaadTalaat,项目名称:Algorithms-bag,代码行数:51,代码来源:HashTable.py

示例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()
开发者ID:tomfitzhenry,项目名称:Misc,代码行数:15,代码来源:Stack.py

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

示例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
开发者ID:jonathanvdc,项目名称:datastructures-project,代码行数:7,代码来源:Stack.py

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

示例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;
开发者ID:danteWei,项目名称:CSCI-3136-Assignments,代码行数:16,代码来源:ListRepr.py


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