﻿ C++ Reverse the stack (using a Deque) elements

# C++ Reverse the stack (using a Deque) elements

## C++ Stack: Exercise-30 with Solution

Write a C++ program that reverse the stack (using a Deque) elements.

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

Sample Solution:

C++ Code:

``````#include <iostream>
#include <deque>
#include<algorithm>

using namespace std;
class Stack {
private:
deque<int> elements; // Deque to store elements

public:
void push(int element) {
elements.push_back(element); // Add element to deque
}

void pop() {
if (elements.empty()) {
cout << "Stack underflow" << endl;
} else {
elements.pop_back(); // Remove last element from deque
}
}

int top() {
if (elements.empty()) {
cout << "Stack is empty" << endl;
return 0;
} else {
return elements.back(); // Return last element in deque
}
}

bool empty() {
return elements.empty(); // Check if deque is empty
}

void reverse_elements(){
if (elements.empty()) {
cout << "Stack is empty" << endl;
return;
}

// Reverse elements in ascending order
reverse(elements.begin(), elements.end());
}

void display() {
deque<int> d = elements;
if (d.empty()) {
cout << "Stack is empty" << endl;
return;
}
cout << "Stack elements are: ";
for (int i = 0; i < d.size(); i++) {
cout << d[i] << " ";
}
cout << endl;
}

};

int main() {
Stack stack;
//Initialize a stack
cout << "Create a stack object:\n";
cout << "\nInput and store (using Deque) some elements onto the stack:\n";
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(6);
stack.push(5);
stack.push(-1);
stack.push(0);
stack.display();
cout << "\nReverse the stack items in ascending order:\n";
stack.reverse_elements();
stack.display();
cout << "\nRemove two elements from the stack:\n";
stack.pop();
stack.pop();
stack.display();
cout << "\nInput three elements onto the stack:\n";
stack.push(4);
stack.push(7);
stack.push(-2);
stack.display();
cout << "\nReverse the said items in ascending order:\n";
stack.reverse_elements();
stack.display();
return 0;
}
``````

Sample Output:

```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
```

Flowchart:

CPP Code Editor:

Previous C++ Exercise: Sort the stack (using a Deque) elements.

What is the difficulty level of this exercise?

﻿

## 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