Skip to content

Instantly share code, notes, and snippets.

@timoheijne
Created March 16, 2018 13:53
Show Gist options
  • Save timoheijne/74a4f948e8c57492afeac40c163b5dc9 to your computer and use it in GitHub Desktop.
Save timoheijne/74a4f948e8c57492afeac40c163b5dc9 to your computer and use it in GitHub Desktop.
Timo's Stack Stuff
#include <iostream>
#include "stack.h"
int main() {
Stack<int> stackTest = Stack<int>();
std::cout << std::endl << "Massive Push Test" << std::endl;
for (int i = 0; i < 50; ++i) {
stackTest.Push(i);
std::cout << i << " - ";
}
stackTest.Print();
std::cout << std::endl << "Single Push Test" << std::endl;
stackTest.Push(1000);
stackTest.Print();
std::cout << std::endl << "Single Pop Test" << std::endl;
stackTest.Pop();
stackTest.Print();
return 0;
}
//
// Created by Heijne on 16-3-2018.
//
#ifndef FUCKTHESTACKOVERFLOWERROR_STACK_H
#define FUCKTHESTACKOVERFLOWERROR_STACK_H
template <typename T> class Stack {
public:
Stack();
~Stack();
void Push(T entry);
void Pop();
void Print();
int Count;
int Capacity = 0;
private:
void Resize(int amount);
T* array;
};
template<typename T>
void Stack<T>::Push(T entry) {
Resize(1);
T* temp = array;
array = new T[Capacity];
array = temp;
array[Capacity - 1] = entry;
}
template<typename T>
void Stack<T>::Pop() {
Resize(-1);
T* temp = array;
array = new T[Capacity];
for (int i = 0; i < Capacity; ++i) {
array[i] = temp[i + 1];
}
}
template<typename T>
void Stack<T>::Resize(int amount) {
Capacity += amount;
}
template<typename T>
Stack<T>::Stack() {
array = new T[Capacity];
}
template<typename T>
Stack<T>::~Stack() {
}
template<typename T>
void Stack<T>::Print() {
std::cout << "Capacity: " << Capacity << std::endl;
for (int i = 0; i < Capacity; ++i) {
std::cout << array[i] << " - ";
}
}
#endif //FUCKTHESTACKOVERFLOWERROR_STACK_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment