forked from AhmadEnan/Data-Structures-Project-01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
50 lines (41 loc) · 1.04 KB
/
Copy pathStack.cpp
File metadata and controls
50 lines (41 loc) · 1.04 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
#include "Stack.h"
#include <iostream>
// ===== Member 2: StackArray ================================================
// Implement StackArray methods in this section.
StackArray::StackArray(int capacity) {
this->capacity = capacity;
this->arr = new int[capacity];
this->topIndex = -1;
}
StackArray::~StackArray() {
delete[] arr;
}
void StackArray::push(int value) {
if (isFull()) {
std::cout << "Stack Overflow\n";
return;
}
arr[++topIndex] = value;
}
int StackArray::pop() {
if (isEmpty()) {
std::cout << "Stack Underflow\n";
return -1;
}
return arr[topIndex--];
}
int StackArray::peek() const {
if (isEmpty()) {
std::cout << "Stack is empty\n";
return -1;
}
return arr[topIndex];
}
bool StackArray::isEmpty() const {
return topIndex == -1;
}
bool StackArray::isFull() const {
return topIndex == capacity - 1;
}
// ===== Member 3: StackLinkedList ===========================================
// Implement StackLinkedList methods in this section.