C++ Stack Exercises: Kth element of a stack from top position
Write a C++ program to get the Kth element of a stack from top position.
Test Data:
Input some elements onto the stack:
Stack elements are: 4 3 2 1
Remove 2 from the said stack:
2nd element from the top of the stack: 3
Sample Solution:
C++ Code:
Sample Output:
Input some elements onto the stack: Stack elements are: 4 3 2 1 Remove 2 from the said stack: 2nd element from the top of the stack: 3 Input two more elements onto the stack: Stack elements are: 0 -1 4 3 2 1 1st element from the top of the stack: 0 3rd element from the top of the stack: 4 6th element from the top of the stack: 1
Flowchart:



CPP Code Editor:
Contribute your code and comments through Disqus.
Previous C++ Exercise: Delete all occurrences of an item in a stack.
Next C++ Exercise: Replace the kth element with new value in a stack.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics