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


Python LinkedList.insert方法代码示例

本文整理汇总了Python中LinkedList.LinkedList.insert方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.insert方法的具体用法?Python LinkedList.insert怎么用?Python LinkedList.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在LinkedList.LinkedList的用法示例。


在下文中一共展示了LinkedList.insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: add_two_numbers_adding_digit_reverse

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
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,代码行数:28,代码来源:add_two_numbers.py

示例2: reverse

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
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,代码行数:9,代码来源:isPalindrome.py

示例3: insert

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
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,代码行数:28,代码来源:Driver.py

示例4: reverse_list

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
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,代码行数:9,代码来源:check_palindrome.py

示例5: test_insert_multi

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
def test_insert_multi():
	l_list = LinkedList()
	l_list.insert("Batman")
	l_list.insert("Superman")
	assert l_list.head.get_key() == "Superman"
	second = l_list.head.get_next()
	assert second.get_key() == "Batman"
开发者ID:prathamtandon,项目名称:python-algorithms-and-data-structures,代码行数:9,代码来源:LinkedList_test.py

示例6: test_delete_head

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
def test_delete_head():
	l_list = LinkedList()
	l_list.insert("Batman")
	l_list.insert("Superman")
	assert l_list.head.get_key() == "Superman"
	l_list.delete("Superman")
	assert l_list.head.get_key() == "Batman"
开发者ID:prathamtandon,项目名称:python-algorithms-and-data-structures,代码行数:9,代码来源:LinkedList_test.py

示例7: test_insert_two

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
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,代码行数:9,代码来源:TestLinkedList.py

示例8: test_positive_search

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
def test_positive_search():
	l_list = LinkedList()
	l_list.insert("Batman")
	l_list.insert("Superman")
	found = l_list.search("Batman")
	assert found.get_key() == "Batman"
	found = l_list.search("Superman")
	assert found.get_key() == "Superman"
开发者ID:prathamtandon,项目名称:python-algorithms-and-data-structures,代码行数:10,代码来源:LinkedList_test.py

示例9: test_size

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
def test_size(): 
	l_list = LinkedList()
	assert l_list.size() == 0
	l_list.insert("Batman")
	l_list.insert("Superman")
	assert l_list.size() == 2
	l_list.delete("Superman")
	assert l_list.size() == 1
开发者ID:prathamtandon,项目名称:python-algorithms-and-data-structures,代码行数:10,代码来源:LinkedList_test.py

示例10: test_searchNone

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
def test_searchNone():
    l_list = LinkedList()
    l_list.insert("Jacob")
    l_list.insert("Pallymay")
    # make sure reg search works
    found = l_list.search("Jacob")
    assert found.get_data() == "Jacob"
    with pytest.raises(ValueError):
        l_list.search("Vincent")
开发者ID:pombredanne,项目名称:CrackCode,代码行数:11,代码来源:TestLinkedList.py

示例11: num_to_list

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
def num_to_list(num):
    lst = LinkedList()

    if num == 0:
        lst.insert(0)
        return lst

    while num > 0:
        digit = num % 10
        lst.insert(digit)
        num = int(num / 10)

    return lst
开发者ID:ZachLiuGIS,项目名称:Algorithm-Enthusiasts,代码行数:15,代码来源:add_two_numbers.py

示例12: get_kth_to_last_reverse

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
def get_kth_to_last_reverse(head, k):
    lst = LinkedList()
    current = head
    while current:
        lst.insert(current.get_data())
        current = current.get_next()

    current = lst.head
    for i in range(k-1):
        current = current.get_next()
        if current is None:
            return None

    return current
开发者ID:ZachLiuGIS,项目名称:Algorithm-Enthusiasts,代码行数:16,代码来源:get_kth_to_last.py

示例13: PartitionTest

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
class PartitionTest(unittest.TestCase):
    def setUp(self):
        self.default_list = LinkedList()
        self.default_list.insert(1)
        self.default_list.insert(2)
        self.default_list.insert(10)
        self.default_list.insert(5)
        self.default_list.insert(8)
        self.default_list.insert(5)
        self.default_list.insert(3)

    def test_partition(self):
        new_list = partition(self.default_list, 5)
        current = new_list.head
        self.assertTrue(current.get_data() < 5)
        current = current.get_next()
        self.assertTrue(current.get_data() < 5)
        current = current.get_next()
        self.assertTrue(current.get_data() < 5)
        current = current.get_next()
        self.assertTrue(current.get_data() >= 5)
        current = current.get_next()
        self.assertTrue(current.get_data() >= 5)
        current = current.get_next()
        self.assertTrue(current.get_data() >= 5)
开发者ID:ZachLiuGIS,项目名称:Algorithm-Enthusiasts,代码行数:27,代码来源:test.py

示例14: __init__

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
class Stack:
    def __init__(self):
        self.content = LinkedList()

    def push(self, x):
        self.content.insert(0, x)

    def pop(self):
        return self.content.delete(0)

    def top(self):
        return self.content[0]

    def isEmpty(self):
        return self.content.isEmpty()
开发者ID:ramda-phi,项目名称:PyPrac,代码行数:17,代码来源:Stack.py

示例15: num_to_list_reverse

# 需要导入模块: from LinkedList import LinkedList [as 别名]
# 或者: from LinkedList.LinkedList import insert [as 别名]
def num_to_list_reverse(num):
    lst = LinkedList()
    if num == 0:
        lst.insert(0)
        return lst

    arr = []
    while num > 0:
        digit = num % 10
        arr.append(digit)
        num = int(num / 10)
    arr.reverse()
    for digit in arr:
        lst.insert(digit)
    return lst
开发者ID:ZachLiuGIS,项目名称:Algorithm-Enthusiasts,代码行数:17,代码来源:add_two_numbers.py


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