I'm learning C++, so I wrote a stack with the help of templates. I'm wondering if there is anything I can improve or if I did something vastly wrong.
Stack.h
#pragma once
#include <ostream>
template <class Type>
struct Node {
Node(Type data, Node<Type>* next)
: data(data), next(next) {}
Node* next;
Type data;
};
template <class Type>
class Stack
{
public:
Stack() : length(0), topNode(NULL) {
}
~Stack() {
while (!isEmpty()) {
pop();
}
}
void push(Type data) {
Node<Type>* newNode = new Node<Type>(data, topNode);
topNode = newNode;
++length;
}
Type pop() {
if (!isEmpty()) {
Node<Type>* popped = topNode;
Type poppedData = popped->data;
topNode = popped->next;
--length;
delete popped;
return poppedData;
}
throw new std::exception("Stack underflow!");
}
bool isEmpty() {
return length == 0;
}
void print() const {
Node<Type>* tempTop = topNode;
while (tempTop != NULL) {
std::cout << tempTop->data << endl;
tempTop = tempTop->next;
}
}
int count() const {
return length;
}
private:
Node<Type>* topNode;
int length;
};
Main.cpp
#include <iostream>
#include "Stack.h"
using namespace std;
int main() {
Stack<int> myStack;
// Push some values
myStack.push(2);
myStack.push(4);
myStack.push(8);
myStack.push(16);
myStack.push(32);
// We pop the 32
myStack.pop();
// Display count after the pop
int lastPopped = myStack.pop();
cout << "Popped value: " << lastPopped << ", Count: " << myStack.count() << endl;
// Print whole stack
cout << endl << "Stack print: " << endl;
myStack.print();
// Exit program when the 'any' key is pressed.
system("PAUSE");
return 0;
}