Question:
Design a stack with operations on middle element.
How to implement a stack which will support following operations in O(1) time complexity?
1. push() which adds an element to the top of stack.
2. findMiddle() which will return middle element of the stack.
3. pop() which removes an element from top of stack.
Push and pop are standard stack operations. If the number of elements to be pushed in the stack is 0 or negative, it should display “Invalid Input”.
Note: Implement the main() inside the class ‘StackDriver‘
Sample Input 1:
Enter the number of elements to be pushed in the stack:
5
Enter the element 1:
10
Enter the element 2:
8
Enter the element 3:
25
Enter the element 4:
8
Enter the element 5:
3
Sample Output1:
The middle element is: 25
The popped element is: 3
Sample Input 2:
Enter the number of elements to be pushed in the stack:
4
Enter the element 1:
10
Enter the element 2:
8
Enter the element 3:
25
Enter the element 4:
3
Sample Output2:
The middle element is: 8
The popped element is: 3Sample Input3:
Enter the number of elements to be pushed in the stack:
-7
Sample Output3:
Invalid Input
ConversionConversion EmoticonEmoticon