05-21-2011 03:37 AM
I'm using Stack which consists of value "1" as top of stack and value "2" as 2nd position and value "3" on 3rd position.. i have retrieved the value "1" by stack.peek().. now, i want to get value "2" which was in 2nd postion of the stack..Is there any method available to retrieve the value?
05-21-2011 05:54 AM
The method peek() just gives you the value on the top of the stack.
If you want to check the element below the top element you will need to use the method pop() - it returns the element on the top of the stack AND removes it from the stack . So when you call pop() it returns 1 , when you call pop() again it will return 2.
Of course you can write your own stack implementation and implement some specific logic there but in the classic version peek just check the element on top and pop removes it.
button to give kudos if I helped you 05-21-2011 07:16 AM
I removed topofstack value using pop() and then called peek() method to retrieve the second value..It works.
05-21-2011 10:18 AM
button to give kudos if I helped you