Stack + Queue + Priority queue

                                                    STACK

//while pushing top++ and while pop top--
in full stack TOP=Max-1 and empty stack ->top = -1

Stack is full and we are pushing => overflow
Stack is empty and we are popping => underflow
 
                                                QUEUE

empty queue -> front = -1  and rear = -1
if inserted ,i.e., enqueue => rear++
if deleted ,i.e., dequeue => front++
{NOTE:- front is index before first element}
eg.- _523 =>front is 0 bcz 1st element '5' is at index 1

                                            Priority Queue
  1. Instead can also use set<>
  2. Use max heap to maintain internal order
    • priority_queue <int> pq;
  3. If want to implement min heap => 
    • priority_queue <int, vector<int>, greater<int> > pq;
      • here vi stores data values
      • NOTE :- this is stored in reverse order means for max heap => max element is last element of vi
    • priority_queue <int, vector<int>, cmp > pq;
      • for customized comparison
      • cmp must be in struct or as class for operator overloading
    • Use max heap but multiply all element by -1 at time of use/print => again multiply -1
  4. To build heap => O(n)
  5. To pop/push => O(log n)
  6. priority_queue <int> pq(arr,arr+n);
    • Can use to copy existing array

Comments

Popular posts from this blog

Graphs

Basics

Hashing