forked from Surabhi0910/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_reverse.cpp
More file actions
42 lines (40 loc) · 855 Bytes
/
Copy pathstack_reverse.cpp
File metadata and controls
42 lines (40 loc) · 855 Bytes
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
#include <iostream>
#include<stack>
#include<queue>
using namespace std;
//function to reverse stack
void reverseStack(stack<int>&input,stack<int>&extra){
if(input.size()==0){
return;
}
int lastElement=input.top();
input.pop();
reverseStack(input,extra);
while(!input.empty()){
int a=input.top();
input.pop();
extra.push(a);
}
input.push(lastElement);
//error was in the following loop
//fixed
while(!extra.empty()){
int b=extra.top();
extra.pop();
input.push(b);
}
}
//main function
int main(){
stack<int>input;
stack<int>extra;
input.push(10);
input.push(20);
input.push(30);
input.push(40);
input.push(50);
input.push(60);
cout<<input.top()<<endl;
reverseStack(input,extra);
cout<<input.top()<<endl;
}