-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack1.cpp
More file actions
43 lines (42 loc) · 808 Bytes
/
stack1.cpp
File metadata and controls
43 lines (42 loc) · 808 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;
class stack
{
private:
vector <int> v; //declaring a vector of type int
public:
void push(int data)
{
v.push_back(data); //function to add data in vector
}
bool empty()
{
return v.size()==0; //checking the size of the vector
}
void pop()
{
if(!empty())
{
v.pop_back(); //removing the elements from the vector
}
}
int top()
{
return v[v.size()-1]; //returning the top most element from the vector
}
};
int main()
{
stack s;
for(int i = 0 ; i<=5 ; i++)
{
s.push(i*i); //adding element in the stack
}
while(!s.empty())
{
cout<<s.top()<<endl; //returning the top most element in the stack
s.pop();
}
return 0;
}