Table of Contents

Python Stack Operations

Example Description
Master stack operations in Python through this comprehensive guide. Learn essential stack functions like opPush, opPop, isEmpty, size, top, and more. Explore practical examples showcasing stack management, element addition, removal, and visualization.

Introduction:

Stack operations are fundamental in computer science and programming. In this tutorial, we delve into various stack operations implemented in Python. From adding and removing elements to checking stack status and displaying contents, you’ll gain a solid understanding of stack manipulation. Through hands-on examples, you’ll enhance your Python programming skills and grasp the essence of stack data structures.

Code:

def isEmpty(glassStack):
    return len(glassStack) == 0

def opPush(glassStack, element):
    glassStack.append(element)

def size(glassStack):
    return len(glassStack)

def opPop(glassStack):
    if isEmpty(glassStack):
        print('underflow')
        return None
    else:
        return glassStack.pop()

def top(glassStack):
    if isEmpty(glassStack):
        print('Stack is empty')
        return None
    else:
        x = len(glassStack)
        return glassStack[x - 1]

def display(glassStack):
    x = len(glassStack)
    print("Current elements in the stack are:")
    for i in range(x - 1, -1, -1):
        print(glassStack[i])

# Initialize the stack
glassStack = list()

# add elements to stack
element = 'glass1'
print("Pushing element ", element)
opPush(glassStack, element)

element = 'glass2'
print("Pushing element ", element)
opPush(glassStack, element)

# display number of elements in stack
print("Current number of elements in stack is", size(glassStack))

# delete an element from the stack
element = opPop(glassStack)
print("Popped element is", element)

# add a new element to the stack
element = 'glass3'
print("Pushing element ", element)
opPush(glassStack, element)

# display the last element added to the stack
print("Top element is", top(glassStack))

# display all elements in the stack
display(glassStack)

# delete all elements from the stack
while True:
    item = opPop(glassStack)
    if item is None:
        print("Stack is empty now")
        break
    else:
        print("Popped element is", item)

Logic:

  1. Define the isEmpty function to check if a stack is empty.
  2. Define the opPush function to add an element to the stack.
  3. Define the size function to calculate the number of elements in the stack.
  4. Define the opPop function to remove and return the top element from the stack.
  5. Define the top function to retrieve the top element from the stack.
  6. Define the display function to print all elements in the stack.
  7. Initialize an empty glassStack list.
  8. Use the opPush function to add elements to the stack.
  9. Utilize the size function to display the number of elements in the stack.
  10. Employ the opPop function to remove an element from the stack and print it.
  11. Use the opPush function again to add a new element to the stack.
  12. Utilize the top function to display the top element.
  13. Use the display function to print all elements in the stack.
  14. Use a loop to empty the stack using the opPop function.

Output:

>>Pushing element glass1

>>Pushing element glass2

>>Current number of elements in stack is 2

>>Popped element is glass2

>>Pushing element glass3

>>Top element is glass3

>>Current elements in the stack are:

>>glass3

>>glass1

>>Popped element is glass3

>>Popped element is glass1

>>Stack is empty now