Implemented stack class

This commit is contained in:
Kevin 2024-06-18 01:14:29 -07:00
parent 67771502ef
commit 46f8a4c043
2 changed files with 59 additions and 0 deletions

56
Stack.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "Stack.h"
// Destructor
template<typename T>
Stack<T>::~Stack() {
while (!isEmpty()) {
pop();
}
}
// Push an element onto the stack
template<typename T>
void Stack<T>::push(const T &data) {
auto *newNode = new StackNode<T>(data);
newNode->next = top;
top = newNode;
count++;
}
// Pop the top element from the stack
template<typename T>
T Stack<T>::pop() {
if (isEmpty()) {
throw std::out_of_range("Stack is empty. Cannot pop.");
}
T data = top->data;
StackNode<T> *temp = top;
top = top->next;
delete temp;
count--;
return data;
}
// Get the top element of the stack without removing it
template<typename T>
T Stack<T>::peek() {
if (isEmpty()) {
throw std::out_of_range("Stack is empty. Cannot peek.");
}
return top->data;
}
// Get the number of elements in the stack
template<typename T>
int Stack<T>::getCount() const {
return count;
}
// Check if the stack is empty
template<typename T>
bool Stack<T>::isEmpty() const {
return count == 0;
}
// Explicit instantiation of the template class for the desired type(s)
template class Stack<CPU>;

View File

@ -3,7 +3,10 @@
template<typename T>
class StackNode {
T data;
StackNode<T> *next;
StackNode(const T &data) : data(data), next(nullptr) {}
};