I want to read all the elements of stack without popping them. I have looked up online and found that there is a 'peek' operation but that just returns the first element of the stack. How do I read the other elements of the stack? Please suggest the code for it in Java.
Thanks in advance :)
Just to reiterate on the original answer, it seems like you need a different data structure... if you elaborate on why exactly you need to see all elements of the stack without the standard operations, then perhaps a suggestion can be made. You've probably already thought of it, but if you pop off all of the elements and push them onto a different stack, then do the same back to the original stack, you can see the elements, but I'm not sure why you'd want to do this.