Sitemap

Stacks and Queue

3 min readOct 4, 2026

--

Press enter or click to view image in full size

Stack

  • Insert → → →
  • Remove ← ← ← (from the same end)
  • LIFO: Last element added is first one removed.

Queue

  • Insert → → → (enqueue at end)
  • Remove ← ← ← (from front)
  • FIFO: First element added is first one removed.

Stacks(LIFO)

  • Stack is a data structure that follows the Last In, First Out (LIFO) principle.
  • Elements are added (append/push) and removed (pop) from the same end.

Stack operations:

  • Create Stack
  • append/Push method: append the data
  • Pop Method: last enter element(top) remove
  • Peek method: top element return
  • isEmpty() method: check element in the stack
  • isFull() method: check stack is full
  • detele() method: detele the entire stack
  • Common use cases: undo functions, call stack in recursion.

Create stack using list

  • easy to implement
  • speed problem when it grows

Stack using Linkedlist:

  • Fast Performance
  • Implementation not easy
class Stack:
def __init__(self):
self.lst= []

def str(self):
return str(self.lst)

def push(self,value):
return self.lst.append(value)

def pop(self):
return self.lst.pop()

def isEmpty(self):
if self.lst == []:
return True
else:
return False

def peek(self):
return self.lst[-1]

def delete(self):
self.lst= None

Queues(FIFO)

  • Queue is a data structure based on First In, First Out (FIFO).
  • Elements are added at the rear (enqueue/append) and removed from the front (dequeue).
  • Queue operations:
  • Enqueue method: Insert element (add to back)
  • Dequeue method: remove element (remove from front)
  • Peek method: see the front element
  • isEmpty method: check if queue is empty
  • isFull method: check stack is full
  • deleteQueue method: detele the entire stack
  • Common use cases: print queues, order processing.

Implementation

  • Python List:
    - Queue without list
    - Queue with capacity(Circular)
  • Linked List
class Queue:
def __init__(self):
self.items=[]

def __str__(self):
values= [str(x) for x in self.items]
return '\n'.join(values)

def isEmpty(self):
if self.items == []:
return True
else:
return False

def enqueue(self,value):
return self.items.append(value)

def dequeue(self):
if self.isEmpty():
return "There is not element in queue"
else:
return self.items.pop(0)

def peek(self):
if self.isEmpty():
return "There is not element in queue"
else:
return self.items[0]

def delete(self):
self.items= None

Time/Space Complexity

Time Complexity

Press enter or click to view image in full size

Space Complexity

Press enter or click to view image in full size

Stack and Queue Patterns

Monotonic Stack / Next-Greater / Next-Smaller

  • Easy

20. Valid Parentheses

Get Mansi Mehta’s stories in your inbox

Join Medium for free to get updates from this writer.

496. Next Greater Element I

1047. Remove All Adjacent Duplicates in String

  • Medium

739. Daily Temperatures

503. Next Greater Element II

556. Next Greater Element III

901. Online Stock Span

150. Evaluate Reverse Polish Notation

  • Hard

42. Trapping Rain Water

84. Largest Rectangle in Histogram

85. Maximal Rectangle

Array / Stack Hybrids (Histogram / Skyline / Spans)

901. Online Stock Span

1762. Buildings With an Ocean View

--

--

Mansi Mehta
Mansi Mehta