• python中栈的实现


    栈是一种线性数据结构,用先进后出或者是后进先出的方式存储数据,栈中数据的插入删除操作都是在栈顶端进行,常见栈的函数操作包括

    • empty() – 返回栈是否为空 – Time Complexity : O(1)
    • size() – 返回栈的长度 – Time Complexity : O(1)
    • top() – 查看栈顶元素 – Time Complexity : O(1)
    • push(g) – 向栈顶添加元素 – Time Complexity : O(1)
    • pop() – 删除栈顶元素 – Time Complexity : O(1)

    python中栈可以用以下三种方法实现:

    1)list

    2)collections.deque

    3)queue.LifoQueue

    使用列表实现栈

    python的内置数据结构list可以用来实现栈,用append()向栈顶添加元素, pop() 可以以后进先出的顺序删除元素

    但是列表本身有一些缺点,主要问题就是当列表不断扩大的时候会遇到速度瓶颈.列表是动态数组,因此往其中添加新元素而没有空间保存新的元素时,它会自动重新分配内存块,并将原来的内存中的值复制到新的内存块中.这就导致了一些append()操作会消耗更多的时间

    >>> stack = []
    >>> #append() fuction to push
    ... #element in list
    ... 
    >>> stack.append('hello')
    >>> stack.append('world')
    >>> stack.append('!')
    >>> print('Initial stack')
    Initial stack
    >>> print(stack)
    ['hello', 'world', '!']
    >>> #pop() function to pop element
    ... #from stack in LIFO order
    ... 
    >>> print('
    Element poped from stack')
    
    Element poped from stack
    
    >>> print(stack.pop())
    !
    >>> print(stack.pop())
    world
    >>> print(stack.pop())
    hello
    >>> print('
    Stack after all elements are poped')
    
    Stack after all elements are poped
    >>> print(stack)
    []

    使用collections.deque实现栈

    python中栈也可以用deque类实现,当我们想要在实现在容器两端更快速地进行append和pop操作时,deque比列表更合适.deque可以提供O(1)时间的append和pop操作,而列表则需要O(n)时间.

    >>> from collections import deque
    >>> stack = deque()
    >>> # append() fuction to push
    ... #element in list
    ... 
    >>> stack.append('hello')
    >>> stack.append('world')
    >>> stack.append('!')
    >>> print('Initial stack')
    Initial stack
    >>> print(stack)
    deque(['hello', 'world', '!'])
    >>> #pop() function to pop element
    ... #from stack in LIFO order
    ... 
    >>> print('
    Element poped from stack')
    
    Element poped from stack
    >>> print(stack.pop())
    !
    >>> print(stack.pop())
    world
    >>> print(stack.pop())
    hello
    >>> print('
    Stack after all elements are poped')
    
    Stack after all elements are poped
    >>> print(stack)
    deque([])

    使用queue module实现栈

    Queue模块有LIFO queue,也就是栈结构.用put()和get()操作从Queue中添加和获得数据

    >>> from queue import LifoQueue
    >>> stack = LifoQueue(maxsize = 3)
    >>> print(stack.qsize())
    0
    >>> stack.put('hello')
    >>> stack.put('world')
    >>> stack.put('!')
    >>> print('
    Element poped from stack')
    
    Element poped from stack
    >>> print(stack.get())
    !
    >>> print(stack.get())
    world
    >>> print(stack.get())
    hello
    >>> print('
    Empty:', stack.empty())
    
    Empty: True

    原文地址 : https://www.geeksforgeeks.org/stack-in-python/

  • 相关阅读:
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-softmax多分类-06
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-逻辑回归与交叉熵概述-05
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-多层感知器(神经网络)与激活函数概述-04
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-梯度下降算法概述-03
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-线性回归tf.keras概述-02
    深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-Tensorflow2.2-cpu/gpu环境安装-01
    深度学习-线性回归基础-02
    艾宾浩斯复习
    maven中设置jdk默认编译版本为1.8
    maven 编译命令
  • 原文地址:https://www.cnblogs.com/laozhanghahaha/p/12302836.html
Copyright © 2020-2023  润新知