【数据结构】堆栈

堆栈 满足先进后出原则

1、python 描述

# 堆栈 先进后出原则
MAXSTACK = 10
global stack
stack = [None] * MAXSTACK
top = -1


def is_empty():
    if top == -1:
        return True
    else:
        return False


def push(data):
    global top
    global MAXSTACK
    global stack
    if top >= MAXSTACK - 1:
        print("堆栈已满,无法加入")
    else:
        top += 1
        stack[top] = data


def pop():
    global top
    global stack
    if is_empty():
        print("堆栈是空的")
    else:
        print("弹出元素为: %d" % stack[top])
        top = top - 1


if __name__ == "__main__":
    i = 0
    while i < 10:
        i += 1
        push(i)
        
pop()
View Code

猜你喜欢

转载自www.cnblogs.com/jzsg/p/10889854.html