Stack is a data structure that follows the LIFO (Last in First out) principle. Design and implement a stack to implement the following functions:
1. Push(num): Push the given number in the stack if the stack is not full.
2. Pop: Remove and print the top element from the stack if present, else print -1.
3. Top: Print the top element of the stack if present, else print -1.
4. isEmpty: Print 1 if the stack is empty, else print 0.
5. isFull: Print 1 if the stack is full, else print 0.
You have been given ‘m’ operations which you need to perform in the stack. Your task is to implement all the functions of the stack.
We perform the following operations on an empty stack which has capacity 2:
When operation 1 1 is performed, we insert 1 in the stack.
When operation 1 2 is performed, we insert 2 in the stack.
When operation 2 is performed, we remove the top element from the stack and print 2.
When operation 3 is performed, we print the top element of the stack, i.e., 3.
When operation 4 is performed, we print 0 because the stack is not empty.
When operation 5 is performed, we print 0 because the stack is size 1, which is not equal to its capacity.
The first line contains two single space-separated integers, ‘n’ and ‘M’, representing the size of the stack and number of operations, respectively.
The next ‘m’ lines contain operations that have to be performed on the stack.
Output Format :
Print output of each query in a separate line.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
2 6
1 1
1 2
2
3
4
5
2
1
0
0
We perform the following operations on an empty stack which has capacity 2:
When operation 1 1 is performed, we insert 1 in the stack.
When operation 1 2 is performed, we insert 2 in the stack.
When operation 2 is performed, we remove the top element from the stack and print 2.
When operation 3 is performed, we print the top element of the stack, i.e., 1.
When operation 4 is performed, we print 0 because the stack is not empty.
When operation 5 is performed, we print 0 because the stack is size 1, which is not equal to its capacity.
5 5
1 2
2
2
1 1
3
2
-1
1
We perform the following operations on an empty stack which has a capacity of 5:
When operation 1 2 is performed, we insert 2 in the stack.
When operation 2 is performed, we remove the top element from the stack and print 2.
When operation 2 is performed, as the stack is empty, we print -1.
When operation 1 1 is performed, we insert 1 in the stack.
When operation 3 is performed, we print the top element of the stack, i.e., 1.
1 <= 'n', 'm' <= 10^3
Time Limit: 1 sec
Try to store the current size of the stack.
The basic idea is to store the size of the stack while performing the operations. We keep a variable (say, ‘stackSize’) to store the size, initialized with -1.
Push: While inserting a number in the stack, we increase the size value by 1 and store it in our array at index ‘stackSize’.
When we pop the element from the stack
Pop: We check if the ‘stackSize’ value equals 0. If it is not equal to 0, we simply print the value present at index ‘stackSize’ and decrease the ‘stackSize’ value by 1.
Top: This operation is similar to the ‘Pop’ operation, we don’t decrease the ‘stackSize’ value.
isEmpty: We simply check whether the ‘stackSize’ value is -1 or not.
isFull: We check whether the value of ‘stackSize’ equals ‘n’ - 1 or not.
Here is the algorithm :
push(‘num’)
pop()
top()
isEmpty()
isFull()
O(1)
We don’t traverse the array in any of the operations. Therefore, the overall time complexity will be O(1).
O(N), where ‘N’ is the capacity of the stack.
We use extra space to store the elements of the stack. Therefore, the overall space complexity will be O(N).