-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmergeStack.cpp
More file actions
46 lines (40 loc) · 928 Bytes
/
mergeStack.cpp
File metadata and controls
46 lines (40 loc) · 928 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
43
44
45
46
#include<iostream>
#include<stack>
// #include<bits/stdc++.h>
using namespace std;
void Merge(stack<int>& s1, stack<int>& s2)
{
stack<int> mergedStack;
while (!s1.empty()) {
mergedStack.push(s1.top());
s1.pop();
}
while (!s2.empty()) {
mergedStack.push(s2.top());
s2.pop();
}
while (!mergedStack.empty()) {
cout << mergedStack.top() << " ";
mergedStack.pop();
}
}
void input(stack<int>& s){
int n, val;
cin>>n;
for(int i=0;i<n;i++){
cout<<"Enter value" <<i+1<<" : ";
cin>>val;
s.push(val);
}
}
int main()
{
stack<int> s1;
stack<int> s2;
cout<<"Enter the no of elements in First stack : ";
input(s1);
cout<<"Enter the no of elements in Second stack : ";
input(s2);
cout<<"Merged Stack : ";
Merge(s1, s2);
}