w3resource

Java Stack - Exercises, Practice, Solution


This resource offers a total of 145 Java Stack problems for practice. It includes 29 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]

1. Implement a stack with push, pop, and top operations.

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.

Click me to see the solution

2. Sort the elements of a stack in ascending order.

Write a Java program to sort the elements of a given stack in ascending order.

Click me to see the solution

3. Sort the elements of a stack in descending order.

Write a Java program to sort the elements of the stack in descending order.

Click me to see the solution

4. Reverse the elements of a stack.

Write a Java program to reverse the elements of a stack.

Click me to see the solution

5. Find the maximum and minimum elements in a stack.

Write a Java program to find the maximum and minimum elements in a stack.

Click me to see the solution

6. Remove all elements from a stack.

Write a Java program to remove all elements from a stack.

Click me to see the solution

7. Count all elements in a stack.

Write a Java program to count all stack elements.

Click me to see the solution

8. Check if an element is present in the stack.

Write a Java program to implement a stack that checks if a given element is present or not in the stack.

Click me to see the solution

9. Remove duplicates from a given stack.

Write a Java program to remove duplicates from a given stack.

Click me to see the solution

10. Find the top and bottom elements of a stack.

Write a Java program to find the top and bottom elements of a given stack.

Click me to see the solution

11. Rotate stack elements to the right direction.

Write a Java program to rotate the stack elements to the right direction.

Click me to see the solution

12. Rotate stack elements to the left direction.

Write a Java program to rotate the stack elements in the left direction.

Click me to see the solution

13. Remove a specific element from the stack.

Write a Java program to remove a specific element from a stack.

Click me to see the solution

14. Swap the top two elements of the stack.

Write a Java program to swap the top two elements of a given stack.

Click me to see the solution

15. Get the nth element from the top of the stack.

Write a Java program to get the nth element from the top of the stack.

Click me to see the solution

16. Get the nth element from the bottom of the stack.

Write a Java program to get the nth element from the bottom of the stack.

Click me to see the solution

17. Move the nth element from the top to the top.

Write a Java program to implement a stack and move the nth element from the top of the stack to the top.

Click me to see the solution

18. Merge two stacks into one.

Write a Java program to merge two stacks into one.

Click me to see the solution

19. Check if one stack is a subset of another stack.

Write a JavaScript program that implements a stack and checks if the stack is a subset of another stack.

Click me to see the solution

20. Check if two stacks are equal.

Write a Java program that implements a stack and checks if two stacks are equal.

Click me to see the solution

21. Find common elements between two stacks.

Write a Java program that implements a stack and finds common elements between two stacks.

Click me to see the solution

22. Find elements in the first stack but not in the second.

Write a Java program that implements a stack and find elements that are in the first stack but not in the second stack.

Click me to see the solution

23. Create a new stack without duplicates from two stacks.

Write a Java program that implements a stack and creates a new stack that contains all elements from two stacks without duplicates.

Click me to see the solution

24. Create a new stack from a portion of the original stack.

Write a Java program that implements a stack and creates a new stack from a portion of the original stack.

Click me to see the solution

25. Create a new stack with elements in either stack but not both.

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.

Click me to see the solution

26. Check if all stack elements satisfy a condition.

Write a Java program that implements a stack and checks if all elements of the stack satisfy a condition.

Click me to see the solution

27. Check if at least one element satisfies a condition.

Write a Java program that implements a stack and checks if at least one element of the stack satisfies a condition.

Click me to see the solution

28. Create a new stack by removing elements that don't satisfy a condition.

Write a Java program that implements a stack and create a new stack by removing elements that do not satisfy a condition.

Click me to see the solution

29. Implement a stack using a linked list.

Write a Java program to implement a stack using a linked list.

Click me to see the solution

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.



Follow us on Facebook and Twitter for latest update.