Hej, mógłbym prosić o Code Review, wskazanie dobrych praktyk? Uczę się C++ od niedawna i nie chciałbym powielać swoich błędów.
#pragma once
#include <iostream>
template <class T>
struct stackItem
{
T value;
stackItem *previousItemPointer;
};
template <class T>
class stack
{
stackItem<T> *head;
void removeLast();
public:
stack()
{
stackItem<T> *head = new stackItem<T>;
head->previousItemPointer = nullptr;
this->head = head;
}
~stack()
{
delete head;
}
void push(T value);
T pop();
bool isEmpty();
};
template <class T>
T stack<T>::pop()
{
T value = head->value;
removeLast();
return value;
}
template <class T>
void stack<T>::push(T value)
{
stackItem<T> *newHead = new stackItem<T>;
if (newHead)
{
newHead->value = value;
newHead->previousItemPointer = head;
head = newHead;
}
else
{
std::cout << "Stack overflow";
}
}
template <class T>
bool stack<T>::isEmpty()
{
return head->previousItemPointer == nullptr;
}
template <class T>
void stack<T>::removeLast()
{
if (isEmpty())
{
std::cout << "Stack underflow";
}
else
{
stackItem<T> *tmpHead = head;
head = tmpHead->previousItemPointer;
delete tmpHead;
}
}