-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
executable file
·63 lines (52 loc) · 983 Bytes
/
Stack.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
#include<iostream>
#include<vector>
using namespace std;
class Stack {
private:
int maxSize;
vector<double> stackVect;
int top;
public:
Stack(int size) {
this->maxSize = size;
this->stackVect.reserve(this->maxSize);
this->top = -1;
}
void push(double j) {
if (!(this->isFull())) {
this->stackVect[++this->top] = j;
} else {
cout << "stack is full";
}
}
double pop() {
if (!(this->isEmpty())) {
return this->stackVect[this->top--];
} else {
cout << "stack is empty";
}
}
double peak() {
return this->stackVect[this->top];
}
bool isEmpty() {
return (this->top == -1);
}
bool isFull() {
return (this->top == this->maxSize - 1);
}
};
int main() {
//create object of Stack ADT
Stack theStack(10);
//push some elements in stack
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.push(40);
theStack.push(60);
//pop elements from stack
while (!theStack.isEmpty()) {
cout << theStack.pop() << " ";
}
}