Import all necessary libraries: #include <bits/stdc++.h>
LIFO(Last in first out) (pancake).
The functions associated with stack are:
Function Name | Description | Time Complexity |
---|---|---|
empty() |
Returns whether the stack is empty | O(1) |
size() |
Returns the size of the stack. | O(1) |
top() |
Returns a reference to the topmost element of the stack | O(1) |
push(g) |
Adds the element ‘g’ at the top of the stack | O(1) |
pop() |
Deletes the most recent entered element of the stack | O(1) |
swap(): This function is used to swap the contents of one stack with another stack of the same type, but the size may vary. Syntax :
stackname1.swap(stackname2)
Time Complexity : O(1)
FIFO (first in, first out).
Function Name | Description | Time Complexity |
---|---|---|
queue::empty() |
Returns whether the queue is empty. It returns true if the queue is empty otherwise returns false. | O(1) |
queue::size() |
Returns the size of the queue. | O(1) |
queue::swap() |
Exchange the contents of two queues but the queues must be of the same data type, although sizes may differ. | O(1) |
queue::emplace() |
Insert a new element into the queue container, and the new element is added to the end of the queue. | O(1) |
queue::front() |
Returns a reference to the first element of the queue. | O(1) |
queue::back() |
Returns a reference to the last element of the queue. | O(1) |
queue::push(g) |
Adds the element ‘g’ at the end of the queue. | O(1) |
queue::pop() |
Deletes the first element of the queue. | O(1) |
make_heap()
: This is used to convert a container of values, such as vectors, into heaps (max heap unless we provide a comparator function to turn it into a min-heap).
front()
returns the maximum element of the heap.
vector<int> v;
make_heap(v.begin(), v.end());
cout<<v.front()<<endl;
to create min heap with make_heap()
struct greaters{
bool operator()(const long& a,const long& b) const{
return a>b;
}
};
make_heap(v.begin(),v.end(), greaters());