JavaScript Stack - Exercises, Practice, Solution
JavaScript Stack [35 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Stack Operations
Write a JavaScript 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. Sort Stack Descending
Write a JavaScript program to sort the elements of a given stack in descending order.
3. Sort Stack Ascending
Write a JavaScript program to sort the elements of a given stack in ascending order.
4. Reverse Stack
Write a JavaScript program to reverse the elements of a given stack.
5. Max & Min in Stack
Write a JavaScript program to find the maximum and minimum elements in a stack.
6. Clear Stack
Write a JavaScript program to remove all elements from a given stack.
7. Count Stack Elements
Write a JavaScript program to count all the elements in a given stack.
8. Check Element in Stack
Write a JavaScript program to implement a stack that checks if a given element is present or not in the stack.
9. Remove Duplicates from Stack
Write a JavaScript program to remove duplicates from a given stack.
10. Top & Bottom of Stack
Write a JavaScript program to find the top and bottom elements of a given stack.
11. Rotate Stack Left
Write a JavaScript program to rotate the stack elements to the left direction.
12. Rotate Stack Right
Write a JavaScript program to rotate the stack elements to the right direction.
13. Middle Element(s) of Stack
Write a JavaScript program to get the middle element(s) of a given stack.
14. Remove Specific Element from Stack
Write a JavaScript program to remove a specific element from a stack.
15. Swap Top Two Elements
Write a JavaScript program to swap the top two elements of a given stack.
16. Nth Element from Top
Write a JavaScript program to get the nth element from the top of the stack.
17. Nth Element from Bottom
Write a JavaScript program to get the nth element from the bottom of the stack.
18. Move Nth from Top to Top
Write a JavaScript program to implement a stack and move the nth element from the top of the stack to the top.
19. Move Nth from Bottom to Top
Write a JavaScript program to implement a stack and move the nth element from the bottom of the stack to the top.
20. Merge Two Stacks
Write a JavaScript program to merge two stacks into one.
21. Linked List Stack Implementation
Write a JavaScript program to implement a stack using a linked list with push and pop operations. Find the top element of the stack and check if the stack is empty or not.
22. Stack to Array
Write a JavaScript program to implement a stack that supports toArray() operation, which converts the stack into an array.
23. Create Stacks from Arrays
Write a JavaScript program that can create stacks from arrays.
24. Stack Concat Operation
Write a JavaScript program to implement a stack that supports concat() operation, which concatenates two stacks into a new stack.
25. Copy Stack
Write a JavaScript program to implement a stack that creates a copy of the stack.
26. Check Subset Stack
Write a JavaScript program that implements a stack and checks if a stack is a subset of another stack.
27. Compare Two Stacks
Write a JavaScript program that implements a stack and checks if two stacks are equal.
28. Common Elements in Two Stacks
Write a JavaScript program that implements a stack and finds elements that are common in two stacks..
29. Elements in Stack1 Not in Stack2
Write a JavaScript program that implements a stack and find elements that are in the first stack but not in the second stack.
30. Union of Two Stacks
Write a JavaScript program that implements a stack and creates a new stack that contains all elements from both stacks without duplicates.
31. Stack Symmetric Difference
Write a JavaScript 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.
32. New Stack from Portion
Write a JavaScript program that implements a stack and creates a new stack from a portion of the original stack.
33. Check All Satisfy Condition
Write a JavaScript program that implements a stack and checks if all elements of the stack satisfy a condition.
34. Check Any Satisfy Condition
Write a JavaScript program that implements a stack and checks if at least one element of the stack satisfies a condition.
35. Filter Stack by Condition
Write a JavaScript program that implements a stack and create a new stack by removing elements that do not satisfy a condition.
More to Come !
* To run the code mouse over on Result panel and click on 'RERUN' button.*
Live Demo:
See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.
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/javascript-exercises/stack/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics