# C++ Stack: Exercises, Practice, Solution

## C++ Stack [30 exercises with solution]

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

In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations:

• Push, which adds an element to the collection, and
• Pop, which removes the most recently added element that was not yet removed.

Stacks entered the computer science literature in 1946, when Alan M. Turing used the terms "bury" and "unbury" as a means of calling and returning from subroutines. Stack follows LIFO data structure type.

1. Write a C++ program to implement a stack using an array with push and pop operations. Find the top element of the stack and check if the stack is empty or not. Go to the editor
Test Data:
Create a stack object:
Check the stack is empty or not? 1
Insert some elements onto the stack:
Stack elements: 4 5 6 7
Remove an element from the stack! 4
Stack elements: 5 6 7
Top of the element of the stack:
5
Click me to see the sample solution

2. Write a C++ program to implement a stack using an array with push and pop operations. Check if the stack is full. Go to the editor
Test Data:
MAX_SIZE of the array: 5
Insert some elements onto the stack:
Stack elements: 1 2 5 6 9
Is the stack full? 1
Remove an element from the stack! 1
Stack elements: 2 5 6 9
Is the stack full? 0
Click me to see the sample solution

3. Write a C++ program to sort a given stack (using an array) using another stack. Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements: 0 1 5 2 4 7
Sort the elements in the stack:
Display the sorted elements of the stack:
Stack elements: 0 1 2 4 5 7
Remove two elements:
Stack elements: 2 4 5 7
Input two more elements
Stack elements: 10 -1 2 4 5 7
Sort the elements in the stack:
Display the sorted elements of the stack:
Stack elements: -1 2 4 5 7 10
Click me to see the sample solution

4. Write a C++ program that reverses the stack (using an array) elements. Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements: 0 1 5 2 4 7
Display the reverse elements of the stack:
Stack elements: 7 4 2 5 1 0
Remove two elements:
Stack elements: 2 5 1 0
Input two more elements
Stack elements: 10 -1 2 5 1 0
Display the reverse elements of the stack:
Stack elements: 0 1 5 2 -1 10
Click me to see the sample solution

5. Write a C++ program to calculate the average value of the stack (using an array) elements. Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements: 0 1 5 2 4 7
Average of the said stack values: 3.17
Remove two elements:
Stack elements: 5 2 4 7
Input two more elements
Stack elements: 10 -1 5 2 4 7
Average of the said stack values: 4.50
Click me to see the sample solution

6. Write a C++ program to find the maximum element in a stack (using an array). Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements: 0 1 5 2 4 7
Maximum value: 7
Remove two elements: Stack elements: 5 2 4 7
Input two more elements
Stack elements: 10 -1 5 2 4 7
Maximum value: 10
Click me to see the sample solution

7. Write a C++ program to find the minimum element in a stack (using an array). Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements: 0 1 5 2 4 7
Minimum value: 0
Remove two elements:
Stack elements: 5 2 4 7
Input two more elements
Stack elements: 10 -1 5 2 4 7
Minimum value: -1
Click me to see the sample solution

8. Write a C++ program to find the middle element of a stack (using an array). Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 5 2 4 7
Middle element of the stack: 2
Input three more elements
Stack elements are: -3 -2 -1 5 2 4 7
Middle element of the stack: 5
Click me to see the sample solution

9. Write a C++ program to delete the middle element of a stack (using an array). Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 5 2 4 7
Delete the middle element of the stack:
Stack elements are: 5 4 7
Input two more elements
Stack elements are: -2 -1 5 4 7
Delete the middle element of the stack:
Stack elements are: -2 -1 4 7
Click me to see the sample solution

10. Write a C++ program to find and remove the largest element in a stack. Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 5 2 4 7
Find and remove the largest element 7 from the stack.
Stack elements are: 5 2 4
Input two more elements
Stack elements are: 20 -1 5 2 4
Find and remove the largest element 20 from the stack.
Stack elements are: -1 5 2 4
Click me to see the sample solution

11. Write a C++ program to find and remove the lowest element in a stack. Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 5 2 4 7
Find and remove the lowest element 2 from the stack.
Stack elements are: 5 4 7
Input two more elements
Stack elements are: 2 -1 5 4 7
Find and remove the lowest element -1 from the stack.
Stack elements are: 2 5 4 7
Click me to see the sample solution

12. Write a C++ program to remove duplicates from a stack using arrays. Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 5 2 2 4 7
Remove duplicates from the said stack:
Stack elements are: 5 2 4 7
Input two more elements onto the stack:
Stack elements are: 5 7 5 2 4 7
Remove duplicates from the said stack:
Stack elements are: 5 7 2 4
Click me to see the sample solution

13. Write a C++ program to delete all occurrences of an item in a stack using arrays. Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 5 2 2 4 7
Remove 2 from the said stack:
Stack elements are: 5 4 7
Input two more elements onto the stack:
Stack elements are: 5 7 5 4 7
Remove 7 from the said stack:
Stack elements are: 5 5 4
Click me to see the sample solution

14. Write a C++ program to get the Kth element of a stack from top position. Go to the editor
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
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
Click me to see the sample solution

15. Write a C++ program to replace the kth element with new value in a stack (using an array). Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 5 2 2 4 7
Replace the kth element with new value:
k = 2 New val = 14
Stack elements are: 5 14 2 4 7
Replace the kth element with new value:
k = 5 New val = 56
Stack elements are: 5 14 2 4 56
Click me to see the sample solution

16. Write a C++ program to implement a stack using a linked list with push, pop operations. Go to the editor
Test Data:
Input some elements onto the stack (using linked list):
Stack elements are: 1 3 5 6
Remove 2 elements from the stack:
Stack elements are: 5 6
Input 2 more elements:
Stack elements are: 9 8 5 6
Click me to see the sample solution

17. Write a C++ program to check a stack's size and whether it is empty or not. The stack is implemented using a linked list. Go to the editor
Test Data:
Check a stack (using linked list) is empty or not!
Stack is empty!
Input some elements onto the stack:
Stack elements are: 0 1 3 5 6
Size of the stack is 5
Remove two elements form the said stack:
Stack elements are: 3 5 6
Size of the stack is 3
Click me to see the sample solution

18. Write a C++ program to reverse the elements of a stack (using a linked list). Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 0 1 3 5 6
Display the reverse elements of the stack:
Stack elements are: 6 5 3 1 0
Input two more elements:
Stack elements are: 45 35 6 5 3 1 0
Display the reverse elements of the stack:
Stack elements are: 0 1 3 5 6 35 45
Click me to see the sample solution

19. Write a C++ program to sort the elements of a stack (using a linked list). Go to the editor
Test Data:
Input some elements onto the stack:
Stack elements are: 0 1 3 5 6
Sorted elements of the said stack:
Stack elements are: 6 5 3 1 0
Input two more elements:
Stack elements are: 29 35 6 5 3 1 0
Sorted elements of the said stack:
Stack elements are: 35 29 6 5 3 1 0
Click me to see the sample solution

20. Write a C++ program to implement a stack using a dynamic array with push and pop operations. Find the top element of the stack and check if the stack is empty or not. Go to the editor
Test Data:
Input the size of the stack: 8 Is the stack empty? Yes Input some elements onto the stack: Stack elements are: 9 7 5 3 1 Is the stack full? No Remove two elements from the said stack: Stack elements are: 5 3 1 Top element is 5
Click me to see the sample solution

21. Write a C++ program that sorts the stack (using a dynamic array) elements. Go to the editor
Test Data:
Input the size of the stack: 5
Input some elements onto the stack:
Stack elements are: 4 7 5 3 1
After sorting the said stack elements:
Stack elements are: 1 3 4 5 7
Click me to see the sample solution

22. Write a C++ program that reverses the stack (using a dynamic array) elements. Go to the editor
Test Data:
Input the size of the stack: 8
Input some elements onto the stack:
Stack elements are: 4 7 5 3 1
Reverse the said stack elements:
Stack elements are: 1 3 5 7 4
Input 2 more elements onto the said stack:
Stack elements are: 40 70 1 3 5 7 4
Reverse the said stack elements:
Stack elements are: 4 7 5 3 1 70 40
Click me to see the sample solution

23. Write a C++ program to find the middle element of a stack (using a dynamic array). Go to the editor
Test Data:
Input the size of the stack: 8
Input some elements onto the stack:
Stack elements are: 11 10 9 7 5 3 1
Middle element = 7
Remove one element from the said stack:
Stack elements are: 10 9 7 5 3 1
Middle element = 5
Click me to see the sample solution

24. Write a C++ program to implement a stack using a vector with push and pop operations. Check if the stack is empty or not and find the top element of the stack. Go to the editor
Test Data:
Create a stack object:
Is the stack empty? Yes
Input and store (using vector) some elements onto the stack:
Stack elements are: 1 2 3 4 5
Top element is 5
Remove two elements from the said stack:
Stack elements are: 1 2 3
Top element is 3
Click me to see the sample solution

25. Write a C++ program that sorts the stack (using a vector) elements. Go to the editor
Test Data:
Create a stack object:
Input and store (using vector) some elements onto the stack:
Stack elements are: 1 3 2 6 5 -1 0
Sort the stack items in ascending order:
Stack elements are: -1 0 1 2 3 5 6
Remove two elements from the stack:
Stack elements are: -1 0 1 2 3
Input three elements onto the stack:
Stack elements are: -1 0 1 2 3 4 7 -2
Sort the said items in ascending order:
Stack elements are: -2 -1 0 1 2 3 4 7
Click me to see the sample solution

26. Write a C++ program that reverses the stack (using a vector) elements. Go to the editor
Test Data:
Create a stack object:
Input and store (using vector) some elements onto the stack:
Stack elements are: 1 3 2 6 5 -1 0
Reverse the elements of the said stack:
Stack elements are: 0 -1 5 6 2 3 1
Remove two elements from the stack:
Stack elements are: 0 -1 5 6 2
Input three elements onto the stack:
Stack elements are: 0 -1 5 6 2 4 7 -2
Reverse the elements of the said stack:
Stack elements are: -2 7 4 2 6 5 -1 0
Click me to see the sample solution

27. Write a C++ program to find the middle element(s) of a stack (using a vector). Go to the editor
Test Data:
Create a stack object:
Input and store (using vector) some elements onto the stack:
Stack elements are: 1 3 2 6 5 -1
Middle element(s) of the said stack elements:
2 6
Input one more element:
Stack elements are: 1 3 2 6 5 -1 0
Middle element(s) of the said stack elements:
6
Click me to see the sample solution

28. Write a C++ program to implement a stack using a Deque with push and pop operations. Check if the stack is empty or not and find the top element of the stack. Go to the editor
Test Data:
Initialize a stack using deque:
Is the stack empty? Yes
Input some elements onto the stack:
Deque elements are: 1 5 3 4 2
Top element is 2
Remove two elements from the stack:
Deque elements are: 1 5 3
Top element is 3
Input two more elements onto the stack:
Deque elements are: 1 5 3 8 9
Top element is 9
Click me to see the sample solution

29. Write a C++ program that sorts the stack (using a Deque) elements. Go to the editor
Test Data:
Create a stack object:
Input and store (using Deque) some elements onto the stack:
Stack elements are: 1 3 2 6 5 -1 0
Sort the stack items in ascending order:
Stack elements are: -1 0 1 2 3 5 6
Remove two elements from the stack:
Stack elements are: -1 0 1 2 3
Input three elements onto the stack:
Stack elements are: -1 0 1 2 3 4 7 -2
Sort the said items in ascending order:
Stack elements are: -2 -1 0 1 2 3 4 7
Click me to see the sample solution

30. Write a C++ program that reverse the stack (using a Deque) elements. Go to the editor
Test Data:
Create a stack object:
Input and store (using Deque) some elements onto the stack:
Stack elements are: 1 3 2 6 5 -1 0
Reverse the stack items in ascending order:
Stack elements are: 0 -1 5 6 2 3 1
Remove two elements from the stack:
Stack elements are: 0 -1 5 6 2
Input three elements onto the stack:
Stack elements are: 0 -1 5 6 2 4 7 -2
Reverse the said items in ascending order:
Stack elements are: -2 7 4 2 6 5 -1 0
Click me to see the sample solution

CPP Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

﻿

## C++ Programming: Tips of the Day

Can you remove elements from a std::list while iterating through it?

You have to increment the iterator first (with i++) and then remove the previous element (e.g., by using the returned value from i++). You can change the code to a while loop like so:

```std::list<item*>::iteratori = items.begin();
while (i != items.end())
{
boolisActive = (*i)->update();
if (!isActive)
{
items.erase(i++);  // alternatively, i = items.erase(i);
}
else
{
other_code_involving(*i);
++i;
}
}
```

Ref : https://bit.ly/3smQY4u