Stacks
are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.
Stacks are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements.
Elements are pushed/popped from the “back” of the specific container, which is known as the top of the stack
The functions associated with stack are:
stack<int>s; (creates an empty stack of integers)
int size = s.size(); // Gives the size of stack
s.top() //Returns a reference to the top most element of the stack
s.push(x); //(where x is an integer.The size increases by 1 after this.) //Adds the element x at the top of the stack
s.pop(); //Deletes the top most element of the stack. (After this the size decreases by 1)
s.empty() //Returns a boolean value which tells whether the stack is empty or not
Example
stack <int> s;
s.push(5);
s.push(3);
s.push(10);
s.push(3);
cout<<s.top()<<endl;
// prints 3
cout<<s.empty()<<endl;
// prints 0, since it's false
s.pop();
cout<<s.top()<<endl;
// prints 10
Queue
Queues
are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.
The functions supported by queue are:
queue<int>q; (creates an empty queue of integers)
int size = q.size(); // Gives the size of queue
q.front() // Returns a reference to the first element of the queue q.back() // Returns a reference to the last element of the queue
q.push(x); //(where x is an integer.The size increases by 1 after this.) // Adds the element x at the end of the queue
q.pop(); // Deletes the first element of the queue. (After this the size decreases by 1)
q.empty() //Returns a boolean value which tells whether the queue is empty or not
Example
queue <int> q;
q.push(5);
q.push(3);
q.push(10);
q.push(3);
cout<<q.front()<<endl;
// prints 5
cout<<q.empty()<<endl;
// prints 0, since it's false
q.pop();
cout<<q.front()<<endl;
// prints 3
Deque
Double ended queue or Deque(part of C++ STL) are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). The member functions of deque that are mainly used are:
deque mydeque; //Creates a double ended queue of deque of int type
int size = mydeque.size(); // Gives the size of the deque
mydeque.front() // Returns a reference to the first element of the deque mydeque.back() // Returns a reference to the last element of the deque
mydeque.push_back(1); //Pushes element at the end mydeque.push_front(2); //Pushes element at the beginning
mydeque.pop_back(); //Pops element from the end mydeque.pop_front(); //Pops element from the beginning
mydeque.empty() //Returns a boolean value which tells whether the deque is empty or not
Example
deque <int> dq;
dq.push_front(5); // dq = [5]
dq.push_back(3); // dq = [5, 3]
dq.push_back(10); // dq = [5, 3, 10]
dq.push_front(3); // dq = [3, 5, 3, 10]
cout<<dq.front()<<endl;
// prints 3
cout<<dq.empty()<<endl;
// prints 0, since it's false
dq.pop_front();
cout<<dq.front()<<endl;
// prints 5
Try the following example in the editor below.
Given an array of integers A. There is a sliding window of size B which is moving from the very left of the array to the very right.
You can only see the B numbers in the window. Each time the sliding window moves rightwards by one position.
You have to find the maximum for each window and print it in a new line.
Return an array C, where C[i] is the maximum value in the array from A[i] to A[i+B-1].
Refer to the given example for clarity.
Constraints
1 <= B <= |A|<= 106
Input Format
The first argument given is the integer array A.
The second argument given is the integer B.
Output Format
Return an array C, where C[i] is the maximum value of from A[i] to A[i+B-1].
Sample Input
A = [1, 3, -1, -3, 5, 3, 6, 7]
B = 3
Sample Output
[3, 3, 5, 5, 6, 7]