Queue - C P P

Theory


  • FIFO (First In First Out)
  • Time complexity of all operations is generally O(1)
  • Enqueue => Push
  • Dequeue => Pop
  • Other Types
    • Circular Queue
    • Input Restricted Queue
      • Pop operation from both sides
    • Output Restricted Queue
      • Push operation from both sides
    • Doubly Ended Queue
      • Push and Pop operation from both sides

STL Commands for Queue


  • #include <queue> => Header to include
  • queue<dataType> var => Initialize queue
  • var.push(val) => Insert value at front
  • var.pop() => Returns and removes the value from front
  • var.empty() => Returns boolean value true if queue is empty
  • var.size() => Returns the size of queue
  • var.back() => Returns last element
  • var.front() => Returns first element

STL Commands for Doubly Ended Queue


  • #include <deque> => Header to include
  • deque<dataType> var => Initialize a deque
  • var.push_front(value) => Insert element in the front
  • var.push_back(value) => Insert element in the back
  • var.pop_front() => Remove element from the front
  • var.pop_back() => Remove element from the back
  • var.at(N) => Returns element at nth index
  • var.front() => Returns element at the first position
  • var.back() => Returns element at the last position
  • var.empty() => Returns boolean value true if deque is empty
  • var.size() => Returns size of deque
  • var.empty(n1, n2) => Removes element form n1 position to n2
  • var.begin() => Returns an iterator pointing to the first element
  • var.end() => Returns an iterator pointing to the theoretical element that follows the last element
Share: