Java Stack - Exercises, Practice, Solution
Java Stack [29 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a Java program to implement a stack with push and pop operations. Find the top element of the stack and check if it is empty or not.
2. Write a Java program to sort the elements of a given stack in ascending order.
3. Write a Java program to sort the elements of the stack in descending order.
4. Write a Java program to reverse the elements of a stack.
5. Write a Java program to find the maximum and minimum elements in a stack.
6. Write a Java program to remove all elements from a stack.
7. Write a Java program to count all stack elements.
8. Write a Java program to implement a stack that checks if a given element is present or not in the stack.
9. Write a Java program to remove duplicates from a given stack.
10. Write a Java program to find the top and bottom elements of a given stack.
11. Write a Java program to rotate the stack elements to the right direction.
12. Write a Java program to rotate the stack elements in the left direction.
13. Write a Java program to remove a specific element from a stack.
14. Write a Java program to swap the top two elements of a given stack.
15. Write a Java program to get the nth element from the top of the stack.
16. Write a Java program to get the nth element from the bottom of the stack.
17. Write a Java program to implement a stack and move the nth element from the top of the stack to the top.
18. Write a Java program to merge two stacks into one.
19. Write a JavaScript program that implements a stack and checks if the stack is a subset of another stack.
20. Write a Java program that implements a stack and checks if two stacks are equal.
21. Write a Java program that implements a stack and finds common elements between two stacks.
22. Write a Java program that implements a stack and find elements that are in the first stack but not in the second stack.
23. Write a Java program that implements a stack and creates a new stack that contains all elements from two stacks without duplicates.
24. Write a Java program that implements a stack and creates a new stack from a portion of the original stack.
25. Write a Java program that implements a stack and creates a new stack that contains only elements that are in either the first or the second stack, but not in both.
26. Write a Java program that implements a stack and checks if all elements of the stack satisfy a condition.
27. Write a Java program that implements a stack and checks if at least one element of the stack satisfies a condition.
28. Write a Java program that implements a stack and create a new stack by removing elements that do not satisfy a condition.
29. Write a Java program to implement a stack using a linked list.
More to Come !
* To run the code mouse over on Result panel and click on 'RERUN' button.*
Java Code Editor
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/stack/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics