0

First element of the stack

How to print first element of the stack without using s.peek()?

13th Jun 2018, 6:39 PM
Maria
Maria - avatar
1 Resposta
0
To print the first element of a stack in C++ without using the `s.peek()` method, you can use the `s.top()` method, combined with a temporary stack to reverse the elements. Here’s a step-by-step approach: 1. Create a temporary stack. 2. Pop elements from the original stack and push them onto the temporary stack until the original stack is empty. 3. The top element of the temporary stack is the first element of the original stack. 4. Print this element. 5. Restore the original stack by popping elements from the temporary stack and pushing them back onto the original stack. Here's an example. https://sololearn.com/compiler-playground/cP6Va4VJkJqR/?ref=app
15th Aug 2024, 8:27 AM
Hayk Mkrtchyan
Hayk Mkrtchyan - avatar