简体   繁体   English

我如何编程我的代码,以便它在LIFO结构的python中从上到下显示堆栈顺序

[英]how can I program my code so it displays a stack order from top to bottom in python for a LIFO structure

i've got the code working but I've written my code in python implementing a stack, it is pushing and popping following LIFO but when you view the list it prints it as: 我已经执行了代码,但是我已经用Python编写了实现堆栈的代码,它会按照LIFO进行推送和弹出,但是当您查看列表时,它将打印为:

1 
2 
3 

showing the last item which is 3 at the bottom, how can I make it show the last item at the top like a proper stack? 在底部显示最后一项为3,我如何使其在顶部显示最后一项,如适当的堆叠?

my code is as follows: 我的代码如下:

stack_pointer = 0
stack =[]
max_length = 2


def view():
        for x in range (len(stack)):
            print(stack[x])
def push():
    global stack_pointer
    if len (stack)> max_length:
        print("Maximum stack length reached!") 
    else:
        stack_pointer = stack_pointer + 1
        item = input("Please enter the  item you wishto add to the stack: ")
        stack.append(item)

def pop():
    global stack_pointer
    if len (stack)<= 0:
        print ("stack is empty!")
    else:
        item = stack.pop
        stack_pointer = stack_pointer - 1
        print ("you just popped out: ", item)

while True:
    print ("")
    print("Python implementation of a stack")
    print("********************************")
    print("1. view Stack")
    print("2. Push onto Stack")
    print("3. Pop out of Stack")
    print("********************************")
    print("")
    menu_choice = int (input("Please enter your menu choice: "))
    print ("")
    print ("")

    if menu_choice == 1:
        view()
    elif menu_choice == 2:
        push()
    elif menu_choice == 3:
        pop()

Try something like this: 尝试这样的事情:

def view():
    print(list(reversed(stack)))

to create reversed copy of your list and print it out. 创建列表的反向副本并打印出来。

This should work: a start of the stack's length - 1 , a stop of -1, and step of -1; 这应该起作用:堆栈长度的start -1, stop处-1, step长-1; no new lists are created or any extra operations, only a modification of the range object parameters, thus it's efficient: 无需创建新列表或进行任何其他操作,只需修改range对象参数即可,因此非常有效:

def view():
    for x in range (len(stack) - 1, -1, -1):
        print(stack[x])  # or print(stack[x], end=' ')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打包我的Python 3程序,以便我可以发布它 - How to pack my Python 3 program so I can release it 我必须仅使用一个队列按从上到下的升序对堆栈进行排序 - I have to sort a stack in ascending order from top to bottom using only one queue 我如何编写我的 python 代码,以便对 .txt 文件中的数字求和? - How can i write my python code so that it sums the numbers from a .txt file? 我应该如何构建我的 Python 模块,以便将来可以随时运行旧版本? - How should I structure my Python module so that I can run older versions at any time in the future? 我如何告诉python我的数据结构(二进制)是什么样子,以便我可以绘制它? - How do I tell python what my data structure (that is in binary) looks like so I can plot it? 如何在 python 的 output 中显示我的程序代码? - How can I show the code of my program in the output in python? Python:如何修复二十一点程序中的“无法访问的代码”? - Python: How can I fix 'Unreachable code' in my blackjack program? 如何在Python程序中获取SMS验证码? - How can I get SMS verification code in my Python program? 如何在Python代码中建立操作顺序? - How can I establish an order of operations in my Python code? Python 如何仅使用 FIFO 队列构建 LIFO 堆栈 - Python how to build a LIFO stack using only FIFO queues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM