-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStacks.cpp
74 lines (56 loc) · 1.18 KB
/
Stacks.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Stack implementation using inbuilt STL stack
#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
//Stack in C++ Standard Template Library
int main() {
stack<string> s;
s.push("Apple");
s.push("Mango");
s.push("Guava");
while(!s.empty()){
cout<<s.top() <<endl;
s.pop();
}
return 0;
}
*/
//Implementation of Stack Data Structure using Vector and template
#include <iostream>
#include <vector>
using namespace std;
template<typename T,typename U> // U not used here
class Stack{
private:
vector<T> v;
vector<U> v2; // not used here
public:
void push(T data){
v.push_back(data);
}
bool empty(){
return v.size()==0;
}
void pop(){
if(!empty()){
v.pop_back();
}
}
T top(){
return v[v.size()-1];
}
};
int main() {
Stack<char,int> s;
for(int i=65;i<=70;i++){
s.push(i);
}
//Try to print the content of the stack by popping each element
while(!s.empty()){
cout<<s.top()<<endl;
s.pop();
}
return 0;
}