# Python program for implementation of stack# import maxsize from sys module# Used to return -infinite when stack is emptyfrom sys import maxsize# Function to create a stack. It initializes size of stack as 0def createStack():stack = []return stack# Stack is empty when stack size is 0def isEmpty(stack):return len(stack) == 0# Function to add an item to stack. It increases size by 1def push(stack, item):stack.append(item)print(item + " pushed to stack ")# Function to remove an item from stack. It decreases size by 1def pop(stack):if (isEmpty(stack)):return str(-maxsize -1) # return minus infinitereturn stack.pop()# Function to return the top from stack without removing itdef peek(stack):if (isEmpty(stack)):return str(-maxsize -1) # return minus infinitereturn stack[len(stack) - 1]# Driver program to test above functionsstack = createStack()push(stack, str(10))push(stack, str(20))push(stack, str(30))print(pop(stack) + " popped from stack")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。