C Exercises: Find the maximum element in a stack
12. Maximum Element in Stack Variants
Write a C program to find the maximum element in a stack.
Sample Solution:
C Code:
Output:
Current stack elements: 5 2 1 6 8 Maximum element: 8 After removing two elements: Current stack elements: 5 2 1 Maximum element: 5 After adding one element: Current stack elements: 5 2 1 10 Maximum element: 10
Flowchart:
For more Practice: Solve these Related Problems:
- Write a C program to find the second maximum element in a stack without fully sorting it.
- Write a C program to implement an auxiliary stack that retrieves the maximum element in O(1) time.
- Write a C program to remove the maximum element from a stack and maintain the order of the other elements.
- Write a C program to display the current maximum element in the stack after each push or pop operation.
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the minimum element in a stack.
Next: Stack push, pop, get middle, and delete middle elements.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.