-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
55 lines (49 loc) · 899 Bytes
/
stack.cpp
File metadata and controls
55 lines (49 loc) · 899 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
47
48
49
50
51
52
53
54
55
// stack in c++
#include <iostream> // for cin and cout
#include <vector> // for vector or array
using namespace std;
// stack is a container adapter that gives the programmer the functionality of a stack - specifically, a LIFO(last-in first-out) data structure.
class Stack
{
private:
vector<int> v;
public:
void push(int data)
{
v.push_back(data);
}
bool empty()
{
return v.size() == 0;
}
void pop()
{
if (!empty())
{
v.pop_back();
}
}
int top()
{
return v[v.size() - 1];
}
int size()
{
return v.size();
}
};
int main()
{
Stack s;
for (int i = 1; i <= 5; i++)
{
s.push(i * i);
}
while (!s.empty())
{
cout << s.top() << endl;
s.pop();
}
cout << "Size of stack is " << s.size() << endl;
return 0;
}