-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path51_Stack_Array_ADT.cpp
More file actions
77 lines (66 loc) · 1.39 KB
/
51_Stack_Array_ADT.cpp
File metadata and controls
77 lines (66 loc) · 1.39 KB
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
75
76
77
#include <iostream>
class StackADT {
private:
int size;
int top;
int* S;
public:
StackADT(int size) {
this->size = size;
top = -1;
S = new int[size];
}
void push(int x);
int pop();
int peek(int index);
int stackTop();
bool isEmpty();
bool isFull();
void display();
};
void StackADT::push(int x) {
if (top == size - 1) {
std::cout << "Stack Overflow" << std::endl;
return;
}
S[top++] = x;
}
int StackADT::pop() {
if (top == -1) {
std::cout << "Stack Underflow" << std::endl;
return -1;
}
return S[top--];
}
int StackADT::peek(int index) {
return (index >= size) ? -1 : S[index];
}
int StackADT::stackTop() {
return (top == -1) ? -1 : S[top];
}
bool StackADT::isEmpty() {
return top == -1;
}
bool StackADT::isFull() {
return top == size - 1;
}
void StackADT::display() {
for (int i = 0; i <= top; i++) {
std::cout << S[i] << " ";
}
std::cout << std::endl;
}
int main() {
StackADT stack = StackADT(5);
std::cout << stack.isEmpty() << std::endl;
stack.push(1);
stack.push(2);
stack.push(3);
std::cout << stack.isFull() << std::endl;
stack.push(4);
stack.push(5);
std::cout << stack.isFull() << std::endl;
std::cout << stack.stackTop() << std::endl;
std::cout << stack.peek(4) << std::endl;
stack.display();
}