7Solve Blog
Chapter NotesClass 12, College, GATE

Difference Between Stack and Queue (With Table and Examples)

Stack vs queue — LIFO vs FIFO, the operations, where each is used, a Python implementation of both, and the Class 12 and GATE questions that test them.

17 September 2026·2 min read·7Solve Team

Both hold items in order and let you add and remove them. The difference is which end you remove from: a stack removes the item you added last (a pile of plates); a queue removes the item you added first (a line at a counter).

The comparison table

StackQueue
OrderLIFO — last in, first outFIFO — first in, first out
Insertpush, at the topenqueue, at the rear
Removepop, from the topdequeue, from the front
Peektopfront
Ends usedOne (top)Two (front and rear)
Real-life picturePlates, browser Back button, undoTicket line, print jobs, keyboard buffer
Uses in CSFunction calls, recursion, expression evaluation, bracket matching, DFSScheduling, BFS, buffering, producer–consumer

Stack in Python

stack = []
stack.append(10)      # push
stack.append(20)
stack.append(30)
top = stack[-1]       # peek → 30
x = stack.pop()       # pop → 30
print(stack)          # [10, 20]

Underflow check: if not stack: print("Underflow") before popping.

Queue in Python

from collections import deque
q = deque()
q.append("A")         # enqueue
q.append("B")
q.append("C")
front = q[0]          # peek → "A"
y = q.popleft()       # dequeue → "A"
print(q)              # deque(['B', 'C'])

A plain list works but pop(0) shifts every element (O(n)); deque.popleft() is O(1).

Worked example — bracket matching with a stack

Check whether {[()]} is balanced: push each opening bracket; on a closing bracket, pop and check it matches; at the end the stack must be empty.

def balanced(s):
    pairs = {')': '(', ']': '[', '}': '{'}
    st = []
    for ch in s:
        if ch in '([{':
            st.append(ch)
        elif ch in ')]}':
            if not st or st.pop() != pairs[ch]:
                return False
    return not st

balanced("{[()]}") → True; balanced("([)]") → False.

Worked example — a queue trace

Start empty. enqueue 5, enqueue 8, dequeue, enqueue 2, dequeue, dequeue.

StepQueue (front → rear)Removed
enqueue 55
enqueue 85 8
dequeue85
enqueue 28 2
dequeue28
dequeue(empty)2

The same trace on a stack (push 5, push 8, pop, push 2, pop, pop) removes 8, then 2, then 5.

Variants worth knowing

How it is tested

Frequently asked questions

What is the main difference between a stack and a queue?

A stack is last-in first-out — the most recently added item is removed first. A queue is first-in first-out — the earliest added item is removed first.

What does "overflow" and "underflow" mean?

Overflow is trying to add to a structure that is full (fixed-size implementation). Underflow is trying to remove from one that is empty.

Which Python structure should I use for a queue?

collections.deque, whose append and popleft are both O(1). Using a list with pop(0) is O(n) because every element shifts.

Do it on 7Solve

ShareWhatsAppXTelegram

Related reading

Study smarter with 7By

7SolveAI-powered learning and problem solving
  • Solve doubts step by step
  • AI Tutor and practice
  • Check your own answers
Open 7Solve
7Marks by 7ByAdvanced exams and mock papers
  • Timed mock exams
  • Chapter-wise papers
  • Exam-style practice
Explore 7Marks Separate 7By product · opens in a new tab
7Solve appStudy anywhere with the 7Solve app
  • Solve from your phone
  • Snap a question with the camera
  • Pick up where you left off
Get the Android app
Chrome extensionUse 7Solve while you browse
  • Capture a question from a page or PDF
  • Solve it in a side panel
  • Open 7Solve in one click
Get the Chrome extension Desktop Chrome