-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse stack.cpp
More file actions
45 lines (34 loc) · 1.22 KB
/
reverse stack.cpp
File metadata and controls
45 lines (34 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// ------------------------------------------------------
// you have a stack in main. code a reverse function which accepts the obj of
// stack and reverse the stack content where you are only allowed to use the stack
// data structure and its member functions, use outside the class approch for this problem.
// ------------------------------------------------------
// note* download the stack-array.h header file from the project and
// include it in this file to run the code successfully.
#include"stack-array.h"
template<typename T>
void reverseStack(Stack<T>& original, const int size)
{
Stack<T> temp1(size);
Stack<T> temp2(size);
// storing in reverse order in temp1 stack
while (!original.isEmpty())
temp1.push(original.pop());
// storing in original order in temp2 stack
while (!temp1.isEmpty())
temp2.push(temp1.pop());
// storing in reverse order in original stack
while (!temp2.isEmpty())
original.push(temp2.pop());
}
int main()
{
const int size = 5;
Stack<char> original(size);
for (int i = 0; i < 5; i++) // pushing data in stack
original.push('A' + i);
cout << "Before reverse: " << original << endl;
reverseStack<char>(original, size);
cout << "After reverse: " << original << endl;
return 0;
}