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
- Instead can also use set<>
- Use max heap to maintain internal order
- If want to implement min heap =>
- here vi stores data values
- NOTE :- this is stored in reverse order means for max heap => max element is last element of vi
- 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
- To build heap => O(n)
- To pop/push => O(log n)
- Can use to copy existing array
Comments
Post a Comment