Stack là gì? (phần 4)

Thực thi Stack trong Python

# Stack implementation in python

# Creating a stack
def create_stack():

stack = []
return stack

# Creating an empty stack
def check_empty(stack):

return len(stack) == 0

# Adding items into the stack
def push(stack, item):

stack.append(item)
print(“pushed item: ” + item)

# Removing an element from the stack
def pop(stack):

if (check_empty(stack)):

return “stack is empty”

return stack.pop()

stack = create_stack()
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
push(stack, str(4))
print(“popped item: ” + pop(stack))
print(“stack after popping an element: ” + str(stack))

Output

pushed item: 1
pushed item: 2
pushed item: 3
pushed item: 4
popped item: 4
stack after popping an element: [‘1’, ‘2’, ‘3’]

Chia sẻ