Stacks, Queues and Monotonicity

Stack

Stack data structure is a linear data structure that accompanies a principle known as LIFO (Last In First Out) or FILO (First In Last Out).

implementing stack using linked list

push operation

  • initialize a node
  • update the value of the node with data
  • now link this node to the head of linked list
  • update the pointer to the current node (head = node)

pop operation

  • first check whether there is a node in the linked list, if not return
  • otherwise make a pointer let say temp point to the top node and move forward the top node one step
  • now free the temp node

top operation

  • check whether there is a node in the linked list if not return
  • otherwise return the value of the top node of the linked list at the head

  • DSA MOC — Full map of DSA topics
  • Recursion — Call stack is a stack; recursion uses LIFO implicitly
  • Graph Theory — BFS uses queues; DFS uses stacks; monotonic stacks for next-element problems
  • Binary Search — Binary search can use stack-based iteration
  • Two Pointers — Stack/queue alternatives for some two-pointer problems
  • trees — Stack for iterative tree traversal; queue for level-order traversal

Queue