This is my second implementation of the Stack in C++. I already implemented it as an array-based here.
I need a review for it to improve it and improve my coding skill. I also will put this implementation on my GitHub account
//======================================================
// Author : Omar_Hafez
// Created : 27 July 2022 (Wednesday) 11:02:46 AM
//======================================================
#include <iostream>
enum InsertStatus { FailedStackEmpty = -1, FailedStackFull = -2, OK = 0 };
template <class T>
class Stack {
private:
int size = 0;
public:
struct Node {
T value;
Node* next = nullptr;
};
Node* top;
Stack() { top = nullptr; }
Stack(int MAX_SIZE) {
// this constructor is to keep the consistency with the array based implementation
top = nullptr;
}
bool isEmpty() { return !top; }
bool isFull() { return 0; }
int getSize() const { return size; }
InsertStatus push(T const& t) {
Node* node = new Node;
node->value = t;
node->next = top;
top = node;
size++;
return OK;
}
InsertStatus pop() {
if (isEmpty()) return FailedStackEmpty;
Node* tmp_ptr = top;
top = top->next;
delete tmp_ptr;
size--;
return OK;
}
T stackTop() { return top->value; }
void clearStack() {
for (Node* tmp_ptr; top; tmp_ptr = top) {
top = top->next;
delete tmp_ptr;
}
size = 0;
}
};
newanddeletehas caused a bug. \$\endgroup\$