-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProj4Stack.h
90 lines (88 loc) · 2.28 KB
/
Proj4Stack.h
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
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class T>
class Stack {
private:
int top;
int size;
int capacity;
T* stack = new T[10];
public:
Stack() {
this->top = -1;
this->size = 0;
this->capacity = 1;
}
// Push elements onto the stack
void push(T value) {
if (isFull()) {
capacity++;
T* temp = new T[capacity];
for (int i = 0; i < capacity; i++) {
temp[i] = T(); // Allocate memory for temp
}
for (int i = 0; i < size; i++) {
temp[i] = stack[i]; // Copy the values from the original stack to temp
}
delete[] stack; // Free memory for the old array
stack = temp; // Copy temp to stack
}
top++; // Increment the index position
stack[top] = value; // Assign a new value to that position in the stack
size++;
}
// Pop the top element of the stack
T pop() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
}
else {
T popValue = stack[top]; // Store the value that will be removed
stack[top] = T(); // removed the value by assigning it to 0
top--; // Decrement the index position
return popValue; // Return the removed value
}
}
// Peek the top element of the stack
T peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return T();
}
else {
return stack[top];
}
}
// Check if the stack is full
bool isFull() {
if (this->top == size) {
return true;
}
else {
return false;
}
}
// Check if the stack is empty
bool isEmpty() {
if (top == -1) {
return true;
}
else {
return false;
}
}
// Return the number of elements in the stack
int count() {
return (top + 1);
}
// Display all the values in the stack
void display() {
cout << "All the values in the stack are: " << endl;
for (int i = top; i >= 0; i--)
{
cout << stack[i] << endl;
}
}
};