Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Stacks quickly and effectively.
489+ students studying
A stack is a linear data structure that stores data in a Last-In, First-Out (LIFO) order. This means that the last item added to the stack is the first one to be removed. Stacks are widely used in various scenarios, such as managing function calls, evaluating expressions, and undoing operations in software applications. Understanding stacks and how to implement them is crucial for problem-solving and algorithm development.
A stack supports two primary operations:
# Stack using a fixed-size array
MAX_SIZE = 10
stack = [None] * MAX_SIZE
top = -1 # Initialise stack as empty
def push(element):
global top
if top == MAX_SIZE - 1:
print("Stack Overflow")
return
top += 1
stack[top] = element
def pop():
global top
if top == -1:
print("Stack Underflow")
return None
element = stack[top]
top -= 1
return element
A stack can also be implemented using a linked list, where each node contains the data and a reference to the next node.
Push: Create a new node and link it as the new top.
Pop: Remove the top node and update the top pointer.
In OOP, stacks are typically implemented as classes.
Example in Python:
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if self.is_empty():
return "Stack Underflow"
return self.items.pop()
def peek(self):
if self.is_empty():
return None
return self.items[-1]
def is_empty(self):
return len(self.items) == 0
Using a Stack to Reverse a String:
def reverse_string(s):
stack = Stack()
for char in s:
stack.push(char)
reversed_str = ''
while not stack.is_empty():
reversed_str += stack.pop()
return reversed_str
Checking for Balanced Parentheses:
def is_balanced(expression):
stack = Stack()
for char in expression:
if char in "({[":
stack.push(char)
elif char in ")}]":
if stack.is_empty():
return False
top = stack.pop()
if not matches(top, char):
return False
return stack.is_empty()
def matches(opening, closing):
pairs = {'(': ')', '{': '}', '[': ']'}
return pairs[opening] == closing
Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
90 flashcards
Flashcards on Stacks
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards9 quizzes
Quizzes on Stacks
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Stacks
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Stacks
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Stacks
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Stacks to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered