-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackImplementation.cpp
More file actions
43 lines (39 loc) · 865 Bytes
/
StackImplementation.cpp
File metadata and controls
43 lines (39 loc) · 865 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
#include <iostream>
#include <vector>
using namespace std;
//implementation of stack using vector
template <typename T> //T tells us what type of data the stack will have when it is created
class Stack{
private:
vector<T> v;
public:
void push(T data){
v.push_back(data);
}
bool empty(){
if(v.size()==0){ //v.size() gives the no. of elements
return true;
}
return false;
}
void pop(){
if(!empty()){
v.pop_back(); //removes last element
}
}
T top(){
return v[v.size()-1]; //last element
}
};
int main(){
Stack<int> s; //we gave T as int
for(int i=1;i<=5;i++){
s.push(i*i);
}
//Print the content of the stack by popping each element
while(!s.empty()){
cout << s.top() << endl;
s.pop();
}
return 0;
}