I don’t get why you have used stack stack = new stack<>(); for creating object.
in the video it is recommended to watch generics videos but that topic comes after 4 sections from oops and stack.
Oops and stack next greater element
When we create an object of stack class, we can access all the methods of that class.
Java Collection framework provides a Stack class which models and implements Stack data structure.
We create a new object named “stack” of Stack class which is inbuilt in Java collection in code line:
Stack < Integer> stack = new Stack< Integer>();
Methods in Stack class:
- Push :Pushes an element on the top of the stack.
- Pop : Removes and returns the top element of the stack. An ‘EmptyStackException’ exception is thrown if we call pop() when the invoking stack is empty.
- Empty : It returns true if nothing is on the top of the stack. Else, returns false.
- Peek : Returns the element on the top of the stack, but does not remove it.
- Search : It determines whether an object exists in the stack. If the element is found, it returns the position of the element from the top of the stack. Else, it returns -1.