|

Python Stack Notes – Class 12 CS (083) | Complete Revison with Practicals

Learn Python Stack Notes for Class 12 CS (083) covering stack data structure, push and pop operations, stack implementation using lists, and practical programs.

Data Structure

  • A data structure is a way to store, organize, and access data efficiently, along with defining the operations that can be performed on it.
  • A data structure in which elements are organized in a sequence is called a linear data structure.
  • Stack and Queue are important data structures. They are not built-in in Python, but their concepts are widely used and implemented using built-in tools like list, etc.

Stack

  • A stack is a linear data structure in which elements are arranged in a sequence.
  • It can be understood with real-life examples like a pile of books or a stack of plates.
  • In a stack, elements are always inserted at the TOP.
  • Similarly, elements are always removed from the top only.
  • Both insertion and deletion occur at the same end, called the top of the stack.
  • Stack follows the LIFO (Last-In, First-Out) principle, meaning the last inserted element is the first to be removed.

Applications of Stack

  • Used to reverse data (like strings, numbers, etc.).
  • Used in undo and redo operations in editors (text/image).
  • Used in web browsers to manage BACK and FORWARD navigation.
  • Used for checking balanced parentheses in expressions.

Operations on Stack

  • Two fundamental operations performed on the stack are:
    • PUSH
    • POP

PUSH in Stack

Push is an operation that adds a new element at the top of the stack. It is an insertion operation. A stack can accept elements until it becomes full. If an attempt is made to add an element to a full stack, it causes an exception called overflow.

POP in Stack

Pop is an operation that removes the topmost element from the stack. It is a deletion operation. Elements can be removed until the stack becomes empty. If an attempt is made to remove an element from an empty stack, it causes an exception called underflow.

A stack is used insert and delete elements in LIFO order.

Stack Terms

  • Overflow: refers to situation when one tries to push an item in stack that is full
  • underflow: refers to situation when one tries to pop an item from empty stack
  • Size: refers to total number of items in stack
  • Top: insertion and deletion end of stack

Implementation of Stack in Python

  • In python List data type is used to implement stack.
  • We can use append() method of list to push (insert) item in stack
  • We can use pop() method of list to pop(delete) item from stack
  • The last element of the list acts as the TOP of the stack.
  • Before popping, we should check whether the stack is empty to avoid errors.

Example: Stack Implementation in Python (Books Stack Example)

Let us create a Stack (stack of books) where we perform the following operations:
• Insert elements (PUSH)
• Delete elements (POP)
• Check if stack is empty
• Find number of elements
• Read the topmost element
• Display all elements

Creating an Empty Stack

We create an empty stack named bookStack using a list:

bookStack = list()

Check if Stack is Empty

This function checks whether the stack has no elements.

def isEmpty(bookStack):
    if len(bookStack) == 0:
        return True
    else:
        return False

PUSH Operation (Insert Element)

Adds an element at the top of the stack.

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

Size of Stack

Returns the number of elements in the stack.

def size(bookStack):
    return len(bookStack)

TOP Operation (Read Top Element)

Returns the most recent element without removing it.

def top(bookStack):
    if isEmpty(bookStack):
        print(“Stack is empty”)
        return None
    else:
        return bookStack[-1]

POP Operation (Delete Element)

Removes and returns the top element.

def opPop(bookStack):
    if isEmpty(bookStack):
        print(“Underflow”)
        return None
    else:
        return bookStack.pop()

Display Stack

Displays all elements from top to bottom.

def display(bookStack):
    print(“Current elements in the stack are:”)
    for i in range(len(bookStack)-1, -1, -1):
        print(bookStack[i])

Main Program (Using Stack Operations)

bookStack = list()

# PUSH operations
opPush(bookStack, “Book1”)
print(“Pushing element Book1”)

opPush(bookStack, “Book2”)
print(“Pushing element Book2”)

# Size of stack
print(“Current number of elements in stack is”, size(bookStack))

# POP operation
print(“Popped element is”, opPop(bookStack))

# PUSH new element
opPush(bookStack, “Book3”)
print(“Pushing element Book3”)

# TOP element
print(“Top element is”, top(bookStack))

# Display stack
display(bookStack)

# Remove all elements
while not isEmpty(bookStack):
    print(“Popped element is”, opPop(bookStack))

print(“Stack is empty now”)

Output (Expected)

Pushing element Book1
Pushing element Book2
Current number of elements in stack is 2
Popped element is Book2
Pushing element Book3
Top element is Book3
Current elements in the stack are:
Book3
Book1
Popped element is Book3
Popped element is Book1
Stack is empty now

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *