Skip to content

Instantly share code, notes, and snippets.

@willeccles
Last active April 11, 2017 17:29
Show Gist options
  • Save willeccles/1e7224db8d91d9f0c193c3da6a045fa5 to your computer and use it in GitHub Desktop.
Save willeccles/1e7224db8d91d9f0c193c3da6a045fa5 to your computer and use it in GitHub Desktop.
Basic link-based stack implementation in C++.
#ifndef STACK_H
#define STACK_H
#include <iostream>
template <class T>
class stack {
private:
// the node class to be used in the stack
template <class nT = T>
class node {
private:
nT _value;
node<nT>* _next;
public:
node(nT value): _value(value) { _next = 0; };
node(nT value, node<nT>* next): _value(value), _next(next) {};
nT getValue() { return _value; };
void setValue(nT newValue) { _value = newValue; };
node<nT>* getNext() { return _next; };
};
node<T>* _top; // shouldn't change or we lose the list
size_t _numItems = 0;
public:
// constructors
stack() {
_top = nullptr; // set _top to nullptr
};
stack(T firstVal) {
_top = new node<T>(firstVal);
_numItems = 1;
};
// push a new value to the stack
void push(T val) {
if (_top == nullptr) {
_top = new node<T>(val);
} else {
node<T>* newNext = _top;
_top = new node<T>(val, newNext);
}
_numItems++;
};
// pop a value from the stack
T pop() {
if (_top == nullptr) {
return static_cast<int>(NULL);
} else {
node<T>* old = _top;
_top = _top->getNext();
_numItems--;
T rval = old->getValue();
delete old;
return rval;
}
};
// remove all the items from the stack
void popAll() {
node<T>* curr = _top;
while (curr != nullptr) {
node<T>* next = curr->getNext();
delete curr;
curr = next;
}
_top = nullptr;
_numItems = 0;
}
// peek at the top of the stack
T peek() {
return _top->getValue();
};
// for printing the stack out
void print() {
if (isEmpty()) {
std::cout << "Stack is empty." << std::endl;
} else {
node<T>* curr = _top;
while (curr != nullptr) {
std::cout << curr->getValue() << ' ';
curr = curr->getNext();
}
std::cout << std::endl;
}
}
// if you want to loop through and apply an operation to each value in the stack
void doForEach(T (*op)(T)) {
node<T>* curr = _top;
while (curr != nullptr) {
curr->setValue(op(curr->getValue()));
curr = curr->getNext();
}
};
// getSize and isEmpty are pretty self-explanatory
size_t getSize() { return _numItems; };
bool isEmpty() { return !_numItems; };
// destructor because we like having memory amirite
~stack() {
// gotta get rid of all them pointers cuz muh memury
node<T>* curr = _top;
while (curr != nullptr) {
node<T>* next = curr->getNext();
delete curr;
curr = next;
}
_top = nullptr;
};
};
#endif
#include "stack.h"
#include <iostream>
stack<int> _stack;
int main(void) {
_stack.push(12);
_stack.push(20);
std::cout << "size: " << _stack.getSize() << std::endl; // prints "size: 2"
std::cout << "stack: "; _stack.print(); // prints "stack: 20 12"
// batch operation, in this case multiply by two
_stack.doForEach([](int x) -> int {
return x*2;
});
std::cout << "stack: "; _stack.print(); // prints "stack: 40 24" since we multiplied all by two
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment